Browse Source

15630 fixed like escaping (#15642)

* Tests for like escaping.

* Fix for like condition escaping

* Fixed PHPDocs

* Simplify tests
tags/2.0.14
Alexander Makarov 7 years ago committed by GitHub
parent
commit
5e8fd58ddb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      framework/db/conditions/InCondition.php
  2. 7
      framework/db/conditions/LikeCondition.php
  3. 5
      framework/db/conditions/LikeConditionBuilder.php
  4. 6
      tests/framework/db/QueryBuilderTest.php
  5. 28
      tests/framework/db/QueryTest.php

2
framework/db/conditions/InCondition.php

@ -5,7 +5,7 @@ namespace yii\db\conditions;
use yii\base\InvalidArgumentException;
/**
* Class LikeCondition represents `IN` condition.
* Class InCondition represents `IN` condition.
*
* @author Dmytro Naumenko <d.naumenko.a@gmail.com>
* @since 2.0.14

7
framework/db/conditions/LikeCondition.php

@ -13,8 +13,9 @@ use yii\base\InvalidArgumentException;
class LikeCondition extends SimpleCondition
{
/**
* @var array map of chars to their replacements.
* By default it's set to `null` meaning responsibility is fully on condition builder.
* @var array|false map of chars to their replacements, false if characters should not be escaped
* or either null or empty array if escaping is condition builder responsibility.
* By default it's set to `null`.
*/
protected $escapingReplacements;
@ -44,7 +45,7 @@ class LikeCondition extends SimpleCondition
}
/**
* @return array
* @return array|false
*/
public function getEscapingReplacements()
{

5
framework/db/conditions/LikeConditionBuilder.php

@ -45,7 +45,10 @@ class LikeConditionBuilder implements ExpressionBuilderInterface
$operator = $expression->getOperator();
$column = $expression->getColumn();
$values = $expression->getValue();
$escape = $expression->getEscapingReplacements() ?: $this->escapingReplacements;
$escape = $expression->getEscapingReplacements();
if ($escape === null || $escape === []) {
$escape = $this->escapingReplacements;
}
list($andor, $not, $operator) = $this->parseOperator($operator);

6
tests/framework/db/QueryBuilderTest.php

@ -2286,6 +2286,12 @@ abstract class QueryBuilderTest extends DatabaseTestCase
[['not like', 'name', [new Expression('CONCAT("test", name, "%")'), '\ab_c']], '[[name]] NOT LIKE CONCAT("test", name, "%") AND [[name]] NOT LIKE :qp0', [':qp0' => '%\\\ab\_c%']],
[['or like', 'name', [new Expression('CONCAT("test", name, "%")'), '\ab_c']], '[[name]] LIKE CONCAT("test", name, "%") OR [[name]] LIKE :qp0', [':qp0' => '%\\\ab\_c%']],
[['or not like', 'name', [new Expression('CONCAT("test", name, "%")'), '\ab_c']], '[[name]] NOT LIKE CONCAT("test", name, "%") OR [[name]] NOT LIKE :qp0', [':qp0' => '%\\\ab\_c%']],
// @see https://github.com/yiisoft/yii2/issues/15630
[
['like', 'location.title_ru', 'vi%', false],
'[[location]].[[title_ru]] LIKE :qp0',
[':qp0' => 'vi%'],
],
];
// adjust dbms specific escaping

28
tests/framework/db/QueryTest.php

@ -153,34 +153,6 @@ abstract class QueryTest extends DatabaseTestCase
$this->assertEquals($condition, $query->where);
}
/**
* @see https://github.com/yiisoft/yii2/issues/15630
*/
public function testLikeEscaping()
{
$query = new Query();
$q = 'vi';
$query->andWhere(['OR',
['like', 'location.title_ru', $q . '%', false],
['like', 'location.title_en', $q . '%', false],
]);
$this->assertEquals([
0 => 'OR',
1 => [
0 => 'like',
1 => 'location.title_ru',
2 => 'vi%',
3 => false,
],
2 => [
0 => 'like',
1 => 'location.title_en',
2 => 'vi%',
3 => false,
],
], $query->where);
}
public function testFilterHavingWithHashFormat()
{
$query = new Query();

Loading…
Cancel
Save