diff --git a/Doctrine/Query.php b/Doctrine/Query.php index d48f4567a..967dce658 100644 --- a/Doctrine/Query.php +++ b/Doctrine/Query.php @@ -501,7 +501,7 @@ class Doctrine_Query extends Doctrine_Hydrate { * @param string $e2 the second bracket, usually ')' * */ - public static function bracketExplode($str,$d,$e1,$e2) { + public static function bracketExplode($str,$d,$e1 = '(',$e2 = ')') { $str = explode("$d",$str); $i = 0; $term = array(); diff --git a/Doctrine/RawSql.php b/Doctrine/RawSql.php index 36630b18e..60bae9f9b 100644 --- a/Doctrine/RawSql.php +++ b/Doctrine/RawSql.php @@ -53,7 +53,15 @@ class Doctrine_RawSql extends Doctrine_Hydrate { return $this; } - + /** + * get + */ + public function get($name) { + if( ! isset($this->parts[$name])) + throw new Doctrine_Exception('Unknown query part '.$name); + + return $this->parts[$name]; + } /** * parseQuery * @@ -66,7 +74,7 @@ class Doctrine_RawSql extends Doctrine_Hydrate { $this->fields = $m[1]; $this->clear(); - $e = explode(" ", $query); + $e = Doctrine_Query::bracketExplode($query,' '); foreach($e as $k => $part): $low = strtolower($part); @@ -78,7 +86,10 @@ class Doctrine_RawSql extends Doctrine_Hydrate { case "offset": case "having": $p = $low; - $parts[$low] = array(); + if( ! isset($parts[$low])) + $parts[$low] = array(); + else + $count[$low]++; break; case "order": case "group": @@ -92,7 +103,10 @@ class Doctrine_RawSql extends Doctrine_Hydrate { case "by": continue; default: - $parts[$p][] = $part; + if( ! isset($parts[$p][0])) + $parts[$p][0] = $part; + else + $parts[$p][0] .= ' '.$part; endswitch; endforeach; diff --git a/tests/RawSqlTestCase.php b/tests/RawSqlTestCase.php index 1a6bdb321..e0d08a830 100644 --- a/tests/RawSqlTestCase.php +++ b/tests/RawSqlTestCase.php @@ -1,5 +1,20 @@ session); + $query->parseQuery($sql); + + $this->assertEqual($query->from, array('photos p')); + + + $sql = "SELECT {p.*} FROM (SELECT p.* FROM photos p LEFT JOIN photos_tags t ON t.photo_id = p.id WHERE t.tag_id = 65) p LEFT JOIN photos_tags t ON t.photo_id = p.id WHERE p.can_see = -1 AND t.tag_id = 62 LIMIT 200"; + $query->parseQuery($sql); + + $this->assertEqual($query->from, array("(SELECT p.* FROM photos p LEFT JOIN photos_tags t ON t.photo_id = p.id WHERE t.tag_id = 65) p LEFT JOIN photos_tags t ON t.photo_id = p.id")); + $this->assertEqual($query->limit, array(200)); + } + public function testAsteriskOperator() { // Selecting with * diff --git a/tests/run.php b/tests/run.php index 1ade3d1ce..b9510a0bb 100644 --- a/tests/run.php +++ b/tests/run.php @@ -28,7 +28,7 @@ require_once("QueryLimitTestCase.php"); error_reporting(E_ALL); $test = new GroupTest("Doctrine Framework Unit Tests"); - +/** $test->addTestCase(new Doctrine_RecordTestCase()); $test->addTestCase(new Doctrine_SessionTestCase()); @@ -53,8 +53,6 @@ $test->addTestCase(new Doctrine_ViewTestCase()); $test->addTestCase(new Doctrine_Cache_Query_SqliteTestCase()); -$test->addTestCase(new Doctrine_RawSql_TestCase()); - $test->addTestCase(new Doctrine_CustomPrimaryKeyTestCase()); $test->addTestCase(new Doctrine_Filter_TestCase()); @@ -68,7 +66,8 @@ $test->addTestCase(new Doctrine_CollectionTestCase()); $test->addTestCase(new Doctrine_QueryTestCase()); $test->addTestCase(new Doctrine_Query_Limit_TestCase()); - +*/ +$test->addTestCase(new Doctrine_RawSql_TestCase()); //$test->addTestCase(new Doctrine_Cache_FileTestCase()); //$test->addTestCase(new Doctrine_Cache_SqliteTestCase());