From 499da8f9b8e07e8b78be3b90303df82357cff254 Mon Sep 17 00:00:00 2001 From: zYne Date: Mon, 9 Oct 2006 16:32:37 +0000 Subject: [PATCH] Fixes #156 Ticket: 156 --- lib/Doctrine/Query.php | 29 ++++++++++++++++++----------- tests/QueryTestCase.php | 11 ++++++++++- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/lib/Doctrine/Query.php b/lib/Doctrine/Query.php index 570e255a0..fd6061833 100644 --- a/lib/Doctrine/Query.php +++ b/lib/Doctrine/Query.php @@ -423,17 +423,22 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { * first splits the query in parts and then uses individual * parsers for each part * - * @param string $query DQL query + * @param string $query DQL query + * @throws Doctrine_Query_Exception if some generic parsing error occurs * @return Doctrine_Query */ public function parseQuery($query) { $this->clear(); + + $query = trim($query); + $query = str_replace("\n"," ",$query); + $query = str_replace("\r"," ",$query); + $e = self::sqlExplode($query," ","(",")"); - - $parts = array(); - foreach($e as $k=>$part): - switch(strtolower($part)): + foreach($e as $k=>$part) { + $part = trim($part); + switch(strtolower($part)) { case "select": case "from": case "where": @@ -449,24 +454,26 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { if(isset($e[$i]) && strtolower($e[$i]) === "by") { $p = $part; $parts[$part] = array(); - } else + } else $parts[$p][] = $part; break; case "by": continue; default: + if( ! isset($p)) + throw new Doctrine_Query_Exception("Couldn't parse query."); + $parts[$p][] = $part; - endswitch; - endforeach; + } + } foreach($parts as $k => $part) { $part = implode(" ",$part); - switch(strtoupper($k)): + switch(strtoupper($k)) { case "SELECT": $this->parseSelect($part); break; case "FROM": - $class = "Doctrine_Query_".ucwords(strtolower($k)); $parser = new $class($this); $parser->parse($part); @@ -488,7 +495,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { case "OFFSET": $this->parts["offset"] = trim($part); break; - endswitch; + } } return $this; diff --git a/tests/QueryTestCase.php b/tests/QueryTestCase.php index 94077f1c8..bbe45fccd 100644 --- a/tests/QueryTestCase.php +++ b/tests/QueryTestCase.php @@ -66,9 +66,18 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { $this->assertTrue($users[2]->getState(), Doctrine_Record::STATE_CLEAN); $this->assertEqual($count + 3, count($this->dbh)); } + public function testMultilineParsing() { + $dql = " + FROM User u + WHERE User.id = ? + "; + $q = new Doctrine_Query(); + + $q->parseQuery($dql); - + $this->assertEqual($q->getQuery(), "SELECT entity.id AS entity__id, entity.name AS entity__name, entity.loginname AS entity__loginname, entity.password AS entity__password, entity.type AS entity__type, entity.created AS entity__created, entity.updated AS entity__updated, entity.email_id AS entity__email_id FROM entity WHERE entity.id = ? AND (entity.type = 0)"); + } public function testUnknownFunction() { $q = new Doctrine_Query(); $f = false;