diff --git a/framework/db/dao/Query.php b/framework/db/dao/Query.php index d134753..3d330be 100644 --- a/framework/db/dao/Query.php +++ b/framework/db/dao/Query.php @@ -163,9 +163,22 @@ class Query extends \yii\base\Object * specifying the values to be bound to the query. * * The $condition parameter should be either a string (e.g. 'id=1') or an array. - * If the latter, it must be in the format of `array(operator, operand1, operand2, ...)`, - * where the operator can be one of the followings, and the possible operands depend on the corresponding - * operator: + * If the latter, it must be in one of the following two formats: + * + * - hash format: `array('column1' => value1, 'column2' => value2, ...)` + * - operator format: `array(operator, operand1, operand2, ...)` + * + * A condition in hash format represents the following SQL expression in general: + * `column1=value1 AND column2=value2 AND ...`. In case when a value is an array, + * an `IN` expression will be generated. And if a value is null, `IS NULL` will be used + * in the generated expression. Below are some examples: + * + * - `array('type'=>1, 'status'=>2)` generates `(type=1) AND (status=2)`. + * - `array('id'=>array(1,2,3), 'status'=>2)` generates `(id IN (1,2,3)) AND (status=2)`. + * - `array('status'=>null) generates `status IS NULL`. + * + * A condition in operator format generates the SQL expression according to the specified operator, which + * can be one of the followings: * * - `and`: the operands should be concatenated together using `AND`. For example, * `array('and', 'id=1', 'id=2')` will generate `id=1 AND id=2`. If an operand is an array, diff --git a/framework/db/dao/QueryBuilder.php b/framework/db/dao/QueryBuilder.php index 81c0ae1..80b2b92 100644 --- a/framework/db/dao/QueryBuilder.php +++ b/framework/db/dao/QueryBuilder.php @@ -476,87 +476,163 @@ class QueryBuilder extends \yii\base\Object } /** - * Generates a SQL condition from the given PHP representation. - * @param string|array $condition the PHP representation of the SQL condition - * @return string the generated SQL condition. + * Parses the condition specification and generates the corresponding SQL expression. + * @param string|array $condition the condition specification. Please refer to [[Query::where()]] + * on how to specify a condition. + * @return string the generated SQL expression + * @throws \yii\db\Exception if the condition is in bad format */ public function buildCondition($condition) { + static $builders = array( + 'and' => 'buildAndCondition', + 'or' => 'buildAndCondition', + 'between' => 'buildBetweenCondition', + 'not between' => 'buildBetweenCondition', + 'in' => 'buildInCondition', + 'not in' => 'buildInCondition', + 'like' => 'buildLikeCondition', + 'not like' => 'buildLikeCondition', + 'or like' => 'buildLikeCondition', + 'or not like' => 'buildLikeCondition', + ); + if (!is_array($condition)) { return $condition; } elseif ($condition === array()) { return ''; } + if (isset($condition[0])) { // operator format: operator, operand 1, operand 2, ... + $operator = $condition[0]; + if (isset($builders[$operator])) { + $method = $builders[$operator]; + array_shift($condition); + return $this->$method($operator, $condition); + } else { + throw new Exception('Found unknown operator in query: ' . $operator); + } + } else { // hash format: 'column1'=>'value1', 'column2'=>'value2', ... + return $this->buildHashCondition($condition); + } + } - $n = count($condition); - $operator = strtoupper($condition[0]); - if ($operator === 'OR' || $operator === 'AND') { - $parts = array(); - for ($i = 1; $i < $n; ++$i) { - $part = $this->buildCondition($condition[$i]); - if ($part !== '') { - $parts[] = '(' . $part . ')'; + private function buildHashCondition($condition) + { + $parts = array(); + foreach ($condition as $column => $value) { + if (is_array($value)) { // IN condition + $parts[] = $this->buildInCondition('in', array($column, $value)); + } else { + if (strpos($column, '(') === false) { + $column = $this->quoteColumnName($column); + } + if ($value === null) { + $parts[] = "$column IS NULL"; + } elseif (is_string($value)) { + $parts[] = "$column=" . $this->connection->quoteValue($value); + } else { + $parts[] = "$column=$value"; } } - return $parts === array() ? '' : implode(' ' . $operator . ' ', $parts); } + return '(' . implode(') AND (', $parts) . ')'; + } - if (!isset($condition[1], $condition[2])) { - throw new Exception("Operator $operator requires at least two operands."); + private function buildAndCondition($operator, $operands) + { + $parts = array(); + foreach ($operands as $operand) { + if (is_array($operand)) { + $operand = $this->buildCondition($operand); + } + if ($operand !== '') { + $parts[] = $operand; + } } + if ($parts !== array()) { + return '(' . implode(") $operator (", $parts) . ')'; + } else { + return ''; + } + } + + private function buildBetweenCondition($operator, $operands) + { + if (!isset($operands[0], $operands[1], $operands[2])) { + throw new Exception("Operator '$operator' requires three operands."); + } + + list($column, $value1, $value2) = $operands; - $column = $condition[1]; if (strpos($column, '(') === false) { $column = $this->quoteColumnName($column); } + $value1 = is_string($value1) ? $this->connection->quoteValue($value1) : (string)$value1; + $value2 = is_string($value2) ? $this->connection->quoteValue($value2) : (string)$value2; - if ($operator === 'BETWEEN' || $operator === 'NOT BETWEEN') { - if (!isset($condition[3])) { - throw new Exception("Operator $operator requires three operands."); - } - $value1 = is_string($condition[2]) ? $this->connection->quoteValue($condition[2]) : (string)$condition[2]; - $value2 = is_string($condition[3]) ? $this->connection->quoteValue($condition[3]) : (string)$condition[3]; - return "$column $operator $value1 AND $value2"; + return "$column $operator $value1 AND $value2"; + } + + private function buildInCondition($operator, $operands) + { + if (!isset($operands[0], $operands[1])) { + throw new Exception("Operator '$operator' requires two operands."); } - $values = $condition[2]; + list($column, $values) = $operands; + if (!is_array($values)) { $values = array($values); } - if ($operator === 'IN' || $operator === 'NOT IN') { - if ($values === array()) { - return $operator === 'IN' ? '0=1' : ''; - } - foreach ($values as $i => $value) { - if (is_string($value)) { - $values[$i] = $this->connection->quoteValue($value); - } else { - $values[$i] = (string)$value; - } - } - return $column . ' ' . $operator . ' (' . implode(', ', $values) . ')'; + if ($values === array()) { + return $operator === 'in' ? '0=1' : ''; } - if ($operator === 'LIKE' || $operator === 'NOT LIKE' || $operator === 'OR LIKE' || $operator === 'OR NOT LIKE') { - if ($values === array()) { - return $operator === 'LIKE' || $operator === 'OR LIKE' ? '0=1' : ''; - } + foreach ($values as $i => $value) { + $values[$i] = is_string($value) ? $this->connection->quoteValue($value) : (string)$value; + } - if ($operator === 'LIKE' || $operator === 'NOT LIKE') { - $andor = ' AND '; - } else { - $andor = ' OR '; - $operator = $operator === 'OR LIKE' ? 'LIKE' : 'NOT LIKE'; - } - $expressions = array(); - foreach ($values as $value) { - $expressions[] = $column . ' ' . $operator . ' ' . $this->connection->quoteValue($value); - } - return implode($andor, $expressions); + if (strpos($column, '(') === false) { + $column = $this->quoteColumnName($column); + } + + return "$column $operator (" . implode(', ', $values) . ')'; + } + + private function buildLikeCondition($operator, $operands) + { + if (!isset($operands[0], $operands[1])) { + throw new Exception("Operator '$operator' requires two operands."); + } + + list($column, $values) = $operands; + + if (!is_array($values)) { + $values = array($values); + } + + if ($values === array()) { + return $operator === 'like' || $operator === 'or like' ? '0=1' : ''; + } + + if ($operator === 'like' || $operator === 'not like') { + $andor = ' and '; + } else { + $andor = ' or '; + $operator = $operator === 'or like' ? 'like' : 'not like'; + } + + if (strpos($column, '(') === false) { + $column = $this->quoteColumnName($column); + } + + $parts = array(); + foreach ($values as $value) { + $parts[] = "$column $operator " . $this->connection->quoteValue($value); } - throw new Exception('Unknown operator: ' . $operator); + return implode($andor, $parts); } /**