Skip to content

Commit 0b2c521

Browse files
authored
Case sensitive option in LikeCondition (#311)
1 parent 3d7c8dd commit 0b2c521

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
- New #307: Add parameters `$ifExists` and `$cascade` to `CommandInterface::dropTable()` and
3939
`DDLQueryBuilderInterface::dropTable()` methods (@vjik)
4040
- Chg #310: Remove usage of `hasLimit()` and `hasOffset()` methods of `DQLQueryBuilder` class (@Tigrov)
41+
- New #311: Add `caseSensitive` option to like condition (@vjik)
4142

4243
## 1.3.0 March 21, 2024
4344

rector.php

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
66
use Rector\Config\RectorConfig;
7+
use Rector\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector;
78
use Rector\Php73\Rector\String_\SensitiveHereNowDocRector;
89
use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector;
910
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
@@ -30,5 +31,6 @@
3031
NullToStrictStringFuncCallArgRector::class,
3132
ReadOnlyPropertyRector::class,
3233
SensitiveHereNowDocRector::class,
34+
RemoveParentCallWithoutParentRector::class,
3335
]);
3436
};

src/Builder/LikeConditionBuilder.php

+27
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Yiisoft\Db\Oracle\Builder;
66

77
use Exception;
8+
use Yiisoft\Db\Expression\ExpressionInterface;
89
use Yiisoft\Db\QueryBuilder\Condition\Interface\LikeConditionInterface;
910
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
1011
use Yiisoft\Db\Schema\Quoter;
@@ -49,6 +50,32 @@ public function build(LikeConditionInterface $expression, array &$params = []):
4950
return parent::build($expression, $params);
5051
}
5152

53+
protected function prepareColumn(LikeConditionInterface $expression, array &$params): string
54+
{
55+
$column = parent::prepareColumn($expression, $params);
56+
57+
if ($expression->getCaseSensitive() === false) {
58+
$column = 'LOWER(' . $column . ')';
59+
}
60+
61+
return $column;
62+
}
63+
64+
protected function preparePlaceholderName(
65+
string|ExpressionInterface $value,
66+
LikeConditionInterface $expression,
67+
?array $escape,
68+
array &$params,
69+
): string {
70+
$placeholderName = parent::preparePlaceholderName($value, $expression, $escape, $params);
71+
72+
if ($expression->getCaseSensitive() === false) {
73+
$placeholderName = 'LOWER(' . $placeholderName . ')';
74+
}
75+
76+
return $placeholderName;
77+
}
78+
5279
/**
5380
* @return string Character used to escape special characters in `LIKE` conditions. By default, it's assumed to be
5481
* `!`.

tests/Provider/QueryBuilderProvider.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
namespace Yiisoft\Db\Oracle\Tests\Provider;
66

77
use Exception;
8+
use Yiisoft\Db\Command\Param;
89
use Yiisoft\Db\Constant\ColumnType;
10+
use Yiisoft\Db\Constant\DataType;
911
use Yiisoft\Db\Constant\PseudoType;
1012
use Yiisoft\Db\Constant\ReferentialAction;
1113
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
@@ -75,14 +77,14 @@ public static function buildCondition(): array
7577
{
7678
$buildCondition = parent::buildCondition();
7779

78-
$buildCondition['like-custom-1'] = [['like', 'a', 'b'], '"a" LIKE :qp0 ESCAPE \'!\'', [':qp0' => '%b%']];
80+
$buildCondition['like-custom-1'] = [['like', 'a', 'b'], '"a" LIKE :qp0 ESCAPE \'!\'', [':qp0' => new Param('%b%', DataType::STRING)]];
7981
$buildCondition['like-custom-2'] = [
8082
['like', 'a', new Expression(':qp0', [':qp0' => '%b%'])],
8183
'"a" LIKE :qp0 ESCAPE \'!\'',
8284
[':qp0' => '%b%'],
8385
];
8486
$buildCondition['like-custom-3'] = [
85-
['like', new Expression('CONCAT(col1, col2)'), 'b'], 'CONCAT(col1, col2) LIKE :qp0 ESCAPE \'!\'', [':qp0' => '%b%'],
87+
['like', new Expression('CONCAT(col1, col2)'), 'b'], 'CONCAT(col1, col2) LIKE :qp0 ESCAPE \'!\'', [':qp0' => new Param('%b%', DataType::STRING)],
8688
];
8789

8890
return $buildCondition;

tests/QueryBuilderTest.php

+1-8
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,7 @@ public function testBatchInsert(
128128
parent::testBatchInsert($table, $rows, $columns, $expected, $expectedParams);
129129
}
130130

131-
/**
132-
* @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\QueryBuilderProvider::buildCondition
133-
*
134-
* @throws Exception
135-
* @throws InvalidConfigException
136-
* @throws InvalidArgumentException
137-
* @throws NotSupportedException
138-
*/
131+
#[DataProviderExternal(QueryBuilderProvider::class, 'buildCondition')]
139132
public function testBuildCondition(
140133
array|ExpressionInterface|string $condition,
141134
string|null $expected,

0 commit comments

Comments
 (0)