Commit 001d5b16 by Klimov Paul

Fixed `yii\sphinx\QueryBuilder` does not support comparison operators (>,<,>=…

Fixed `yii\sphinx\QueryBuilder` does not support comparison operators (>,<,>= etc) in where specification
parent ec48a61c
......@@ -5,6 +5,7 @@ Yii Framework 2 sphinx extension Change Log
-----------------------
- Bug #5601: Simple conditions in Query::where() and ActiveQuery::where() did not allow `yii\db\Expression` to be used as the value (cebe, stevekr)
- Bug #5634: Fixed `yii\sphinx\QueryBuilder` does not support comparison operators (>,<,>= etc) in where specification (klimov-paul)
- Enh #5223: Query builder now supports selecting sub-queries as columns (qiangxue)
......
......@@ -956,6 +956,41 @@ class QueryBuilder extends Object
}
/**
* Creates an SQL expressions like `"column" operator value`.
* @param IndexSchema[] $indexes list of indexes, which affected by query
* @param string $operator the operator to use. Anything could be used e.g. `>`, `<=`, etc.
* @param array $operands contains two column names.
* @param array $params the binding parameters to be populated
* @return string the generated SQL expression
* @throws InvalidParamException if count($operands) is not 2
*/
public function buildSimpleCondition($indexes, $operator, $operands, &$params)
{
if (count($operands) !== 2) {
throw new InvalidParamException("Operator '$operator' requires two operands.");
}
list($column, $value) = $operands;
if (strpos($column, '(') === false) {
$column = $this->db->quoteColumnName($column);
}
if ($value === null) {
return "$column $operator NULL";
} elseif ($value instanceof Expression) {
foreach ($value->params as $n => $v) {
$params[$n] = $v;
}
return "$column $operator {$value->expression}";
} else {
$phName = self::PARAM_PREFIX . count($params);
$params[$phName] = $value;
return "$column $operator $phName";
}
}
/**
* @param array $columns
* @return string the ORDER BY clause built from [[query]].
*/
......@@ -1067,40 +1102,6 @@ class QueryBuilder extends Object
}
/**
* Creates an SQL expressions like `"column" operator value`.
* @param string $operator the operator to use. Anything could be used e.g. `>`, `<=`, etc.
* @param array $operands contains two column names.
* @param array $params the binding parameters to be populated
* @return string the generated SQL expression
* @throws InvalidParamException if count($operands) is not 2
*/
public function buildSimpleCondition($operator, $operands, &$params)
{
if (count($operands) !== 2) {
throw new InvalidParamException("Operator '$operator' requires two operands.");
}
list($column, $value) = $operands;
if (strpos($column, '(') === false) {
$column = $this->db->quoteColumnName($column);
}
if ($value === null) {
return "$column $operator NULL";
} elseif ($value instanceof Expression) {
foreach ($value->params as $n => $v) {
$params[$n] = $v;
}
return "$column $operator {$value->expression}";
} else {
$phName = self::PARAM_PREFIX . count($params);
$params[$phName] = $value;
return "$column $operator $phName";
}
}
/**
* @param array $indexes index names.
* @return IndexSchema[] index schemas.
*/
......
......@@ -58,6 +58,11 @@ class ActiveRecordTest extends SphinxTestCase
$this->assertTrue($article instanceof ArticleIndex);
$this->assertEquals(2, $article->id);
// find by comparison
$article = ArticleIndex::find()->where(['>', 'author_id', 1])->one();
$this->assertTrue($article instanceof ArticleIndex);
$this->assertEquals(2, $article->id);
// find custom column
$article = ArticleIndex::find()->select(['*', '(5*2) AS custom_column'])
->where(['author_id' => 1])->one();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment