From 8755e5ab50bc58ee6495d705b67cb3b88dbdb869 Mon Sep 17 00:00:00 2001 From: zYne Date: Mon, 21 Aug 2006 10:43:44 +0000 Subject: [PATCH] DQL - SQL conversion LIMIT clause parsing examples added --- Doctrine/Form.php | 1 + ...ponents - Query - DQL - SQL conversion.php | 42 +++++++++++++++++-- manual/documentation.php | 3 +- tests/QueryLimitTestCase.php | 1 + 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/Doctrine/Form.php b/Doctrine/Form.php index 31c6e3e42..0ccacd474 100644 --- a/Doctrine/Form.php +++ b/Doctrine/Form.php @@ -50,6 +50,7 @@ class Doctrine_Form implements Iterator { } else { $elements[$column] = "\n"; } + } return $elements[$column]; } else { diff --git a/manual/docs/Basic Components - Query - DQL - SQL conversion.php b/manual/docs/Basic Components - Query - DQL - SQL conversion.php index 1229ccf80..5fcb1dbe0 100644 --- a/manual/docs/Basic Components - Query - DQL - SQL conversion.php +++ b/manual/docs/Basic Components - Query - DQL - SQL conversion.php @@ -24,7 +24,7 @@ DQL QUERY: FROM User(id) WHERE User.Group.Phonenumber.phonenumber LIKE '123 123' SQL QUERY: SELECT entity.id AS entity__id FROM entity LEFT JOIN groupuser ON entity.id = groupuser.user_id LEFT JOIN entity AS entity2 ON entity2.id = groupuser.group_id LEFT JOIN phonenumber ON entity2.id = phonenumber.entity_id WHERE phonenumber.phonenumber LIKE '123 123' AND (entity.type = 0 AND (entity2.type = 1 OR entity2.type IS NULL)) "; - +function renderQueries($str) { $e = explode("\n",$str); $color = "367FAC"; @@ -37,16 +37,50 @@ foreach($e as $line) { $l = str_replace("SELECT","
SELECT",$line); $l = str_replace("FROM","
FROM",$l); $l = str_replace("LEFT JOIN","
LEFT JOIN",$l); + $l = str_replace("INNER JOIN","
INNER JOIN",$l); $l = str_replace("WHERE","
WHERE",$l); $l = str_replace("AS","AS",$l); $l = str_replace("ON","ON",$l); $l = str_replace("ORDER BY","ORDER BY",$l); $l = str_replace("LIMIT","LIMIT",$l); $l = str_replace("OFFSET","OFFSET",$l); + $l = str_replace("DISTINCT","DISTINCT",$l); $l = str_replace(" ","
",$l); - if(substr($l,0,3) == "DQL") print "
"; + print $l."
"; - + if(substr($l,0,3) == "SQL") print "
"; } - +} +renderQueries($str); ?> +Propably the most complex feature DQL parser has to offer is its LIMIT clause parser. In pure +sql the limit clause limits the number of rows returned. So for example when fetching users and their +phonenumbers using limit 20 you might get anything between 1-20 users, since the first user might have 20 phonenumbers and +hence the record set would consist of 20 rows. +

+DQL overcomes this problem with subqueries and with complex but efficient subquery analysis. In the next example +we are going to fetch first 20 users and all their phonenumbers with single efficient query. Notice how the DQL parser is smart enough +to use column aggregation inheritance even in the subquery. +

+ +In the next example +we are going to fetch first 20 users and all their phonenumbers and only those users that actually have phonenumbers (hence the usage of colon operator = INNER JOIN) with single efficient query. +Notice how the DQL parser is smart enough to use the INNER JOIN in the subquery. +
+ + + diff --git a/manual/documentation.php b/manual/documentation.php index 83efafd29..cd31f95ee 100644 --- a/manual/documentation.php +++ b/manual/documentation.php @@ -166,12 +166,13 @@ $menu = array("Getting started" => "Using SQL", "Adding components", "Method overloading"), + /** "Statement - UNDER CONSTRUCTION" => array("Introduction", "Setting parameters", "Getting parameters", "Getting row count", "Executing the statement"), - + */ "Exceptions" => array( "Overview", "List of exceptions" diff --git a/tests/QueryLimitTestCase.php b/tests/QueryLimitTestCase.php index 7990944f1..33c159fb7 100644 --- a/tests/QueryLimitTestCase.php +++ b/tests/QueryLimitTestCase.php @@ -76,6 +76,7 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase { $this->query->from("User(id):Phonenumber"); $this->query->limit(5); + $sql = $this->query->getQuery(); $users = $this->query->execute();