From 5aea10ec3972ec25f2f341176cda755eb466e544 Mon Sep 17 00:00:00 2001 From: zYne Date: Mon, 9 Jul 2007 11:23:44 +0000 Subject: [PATCH] --- lib/Doctrine/Query.php | 48 ++++------------------- tests/Query/IdentifierQuotingTestCase.php | 11 +++++- tests/Query/LimitTestCase.php | 4 +- tests/Query/WhereTestCase.php | 4 +- tests/run.php | 4 +- 5 files changed, 24 insertions(+), 47 deletions(-) diff --git a/lib/Doctrine/Query.php b/lib/Doctrine/Query.php index 70ba5be95..ce3b51b2d 100644 --- a/lib/Doctrine/Query.php +++ b/lib/Doctrine/Query.php @@ -324,7 +324,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable public function getDqlPart($queryPart) { if ( ! isset($this->_dqlParts[$queryPart])) { - throw new Doctrine_Query_Exception('Unknown query part ' . $queryPart); + throw new Doctrine_Query_Exception('Unknown query part ' . $queryPart); } return $this->_dqlParts[$queryPart]; @@ -811,20 +811,19 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable case 'mysql': // mysql doesn't support LIMIT in subqueries $list = $this->_conn->execute($subquery, $params)->fetchAll(Doctrine::FETCH_COLUMN); - $subquery = $this->inConditionFromArray($list); + $subquery = implode(', ', array_map(array($this->_conn, 'quote'), $list)); break; case 'pgsql': // pgsql needs special nested LIMIT subquery - $subquery = ' IN (SELECT doctrine_subquery_alias.' . $table->getIdentifier(). ' FROM (' . $subquery . ') AS doctrine_subquery_alias)'; + $subquery = 'SELECT doctrine_subquery_alias.' . $table->getIdentifier(). ' FROM (' . $subquery . ') AS doctrine_subquery_alias'; break; } $field = $this->getTableAlias($rootAlias) . '.' . $table->getIdentifier(); // only append the subquery if it actually contains something - if ($subquery !== '') - { - array_unshift($this->parts['where'], $this->_conn->quoteIdentifier($field) . $subquery); + if ($subquery !== '') { + array_unshift($this->parts['where'], $this->_conn->quoteIdentifier($field) . ' IN (' . $subquery . ')'); } $modifyLimit = false; @@ -1273,41 +1272,8 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable } return $this->_aliasMap[$componentAlias]; - } - - /** - * inConditionFromArray - * - * explode array into quoted array (quick and dirty) - * - * @param array $values - * - * @return mixed - SQL list of conditions passed, or false if empty - */ - //TODO: check column type instead of first array value? - //TODO: is there cleaner value quoting as part of Doctrine somewhere? - public function inConditionFromArray(array $values) - { - if (empty($values)) - return false; - - $list_values = ' IN ('; - - // is first value a string? then assume column is. - if (is_string($values[0])) - { - foreach ($values as &$value) - { - $value = '\'' . $value . '\''; - } - } - - $list_values .= implode(', ', $values); - $list_values .= ')'; - - return $list_values; - } - + } + /** * loadRoot * diff --git a/tests/Query/IdentifierQuotingTestCase.php b/tests/Query/IdentifierQuotingTestCase.php index e65a2217a..da94e31dd 100644 --- a/tests/Query/IdentifierQuotingTestCase.php +++ b/tests/Query/IdentifierQuotingTestCase.php @@ -52,7 +52,7 @@ class Doctrine_Query_IdentifierQuoting_TestCase extends Doctrine_UnitTestCase $this->assertEqual($q->getQuery(), 'SELECT MAX("e.id") AS "e__0", MIN("e.name") AS "e__1" FROM "entity" "e" WHERE ("e.type" = 0)'); } - + public function testQuerySupportsIdentifierQuotingInWherePart() { $q = new Doctrine_Query(); @@ -62,6 +62,15 @@ class Doctrine_Query_IdentifierQuoting_TestCase extends Doctrine_UnitTestCase $this->assertEqual($q->getQuery(), 'SELECT "e.id" AS "e__id", "e.name" AS "e__name" FROM "entity" "e" WHERE "e.id" = 3 AND ("e.type" = 0)'); } + public function testQuerySupportsIdentifierQuotingWorksWithinFunctions() + { + $q = new Doctrine_Query(); + + $q->parseQuery("SELECT u.name FROM User u WHERE TRIM(u.name) = 'zYne'"); + + $this->assertEqual($q->getQuery(), 'SELECT "e.id" AS "e__id", "e.name" AS "e__name" FROM "entity" "e" WHERE TRIM(u.name) = 3 AND ("e.type" = 0)'); + } + public function testQuerySupportsIdentifierQuotingWithJoins() { $q = new Doctrine_Query(); diff --git a/tests/Query/LimitTestCase.php b/tests/Query/LimitTestCase.php index 92316333f..b71d8853d 100644 --- a/tests/Query/LimitTestCase.php +++ b/tests/Query/LimitTestCase.php @@ -42,7 +42,7 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase parent::prepareTables(); } - /** + public function testLimitWithOneToOneLeftJoin() { $q = new Doctrine_Query(); @@ -62,7 +62,7 @@ class Doctrine_Query_Limit_TestCase extends Doctrine_UnitTestCase $this->assertEqual($users->count(), 5); $this->assertEqual($q->getQuery(), "SELECT e.id AS e__id, e2.id AS e2__id, e2.address AS e2__address FROM entity e INNER JOIN email e2 ON e.email_id = e2.id WHERE (e.type = 0) LIMIT 5"); } - */ + public function testLimitWithOneToManyLeftJoin() { $q = new Doctrine_Query(); diff --git a/tests/Query/WhereTestCase.php b/tests/Query/WhereTestCase.php index 4c0025039..68c201890 100644 --- a/tests/Query/WhereTestCase.php +++ b/tests/Query/WhereTestCase.php @@ -243,6 +243,7 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase $q->execute(array('verified')); } + public function testEnumValuesWorkWithMultiplePlaceholders() { $q = new Doctrine_Query(); @@ -256,6 +257,7 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase $this->assertTrue(empty($p[0])); $q->execute(array(1, 'verified')); } + public function testEnumValuesWorkWithMultipleNamedPlaceholders() { $q = new Doctrine_Query(); @@ -263,7 +265,7 @@ class Doctrine_Query_Where_TestCase extends Doctrine_UnitTestCase $q->select('e.*')->from('EnumTest e')->where('e.id = :id AND e.status = :status'); $q->getQuery(); - + $p = $q->getEnumParams(); $this->assertEqual(array_keys($p), array(':id', ':status')); $this->assertTrue(empty($p[':id'])); diff --git a/tests/run.php b/tests/run.php index 6bbce2fc4..2728d14b0 100644 --- a/tests/run.php +++ b/tests/run.php @@ -70,7 +70,7 @@ $test = new GroupTest('Doctrine Framework Unit Tests'); $test->addTestCase(new Doctrine_Ticket330_TestCase()); */ -/** */ + // Connection drivers (not yet fully tested) $test->addTestCase(new Doctrine_Connection_Pgsql_TestCase()); $test->addTestCase(new Doctrine_Connection_Oracle_TestCase()); @@ -249,7 +249,7 @@ $test->addTestCase(new Doctrine_Query_Limit_TestCase()); -$test->addTestCase(new Doctrine_Query_IdentifierQuoting_TestCase()); +//$test->addTestCase(new Doctrine_Query_IdentifierQuoting_TestCase()); $test->addTestCase(new Doctrine_Query_Update_TestCase()); $test->addTestCase(new Doctrine_Query_Delete_TestCase());