From 4cb332fba4d907bfa97ac5f4d95a66c0cbd3d80a Mon Sep 17 00:00:00 2001 From: hansbrix Date: Mon, 9 Jul 2007 07:55:45 +0000 Subject: [PATCH] added method to properly quote IN (...) strings Ticket: 383 --- lib/Doctrine/Query.php | 46 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/lib/Doctrine/Query.php b/lib/Doctrine/Query.php index eff3cbb87..70ba5be95 100644 --- a/lib/Doctrine/Query.php +++ b/lib/Doctrine/Query.php @@ -811,19 +811,20 @@ 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 = implode(', ', $list); + $subquery = $this->inConditionFromArray($list); break; case 'pgsql': // pgsql needs special nested LIMIT subquery - $subquery = 'SELECT doctrine_subquery_alias.' . $table->getIdentifier(). ' FROM (' . $subquery . ') AS doctrine_subquery_alias'; + $subquery = ' IN (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) . ' IN (' . $subquery . ')'); + if ($subquery !== '') + { + array_unshift($this->parts['where'], $this->_conn->quoteIdentifier($field) . $subquery); } $modifyLimit = false; @@ -983,7 +984,6 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable } $subquery = implode(' ', $parts); - return $subquery; } /** @@ -1273,7 +1273,41 @@ 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 *