diff --git a/CHANGELOG.md b/CHANGELOG.md index ed07da7d..15a7b5b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ - New #307: Add parameters `$ifExists` and `$cascade` to `CommandInterface::dropTable()` and `DDLQueryBuilderInterface::dropTable()` methods (@vjik) - Chg #310: Remove usage of `hasLimit()` and `hasOffset()` methods of `DQLQueryBuilder` class (@Tigrov) +- New #311: Add `caseSensitive` option to like condition (@vjik) ## 1.3.0 March 21, 2024 diff --git a/rector.php b/rector.php index 7ee7d903..83a3a47f 100644 --- a/rector.php +++ b/rector.php @@ -4,6 +4,7 @@ use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector; use Rector\Config\RectorConfig; +use Rector\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector; use Rector\Php73\Rector\String_\SensitiveHereNowDocRector; use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector; use Rector\Php81\Rector\Property\ReadOnlyPropertyRector; @@ -30,5 +31,6 @@ NullToStrictStringFuncCallArgRector::class, ReadOnlyPropertyRector::class, SensitiveHereNowDocRector::class, + RemoveParentCallWithoutParentRector::class, ]); }; diff --git a/src/Builder/LikeConditionBuilder.php b/src/Builder/LikeConditionBuilder.php index 8652dbfe..9b550223 100644 --- a/src/Builder/LikeConditionBuilder.php +++ b/src/Builder/LikeConditionBuilder.php @@ -5,6 +5,7 @@ namespace Yiisoft\Db\Oracle\Builder; use Exception; +use Yiisoft\Db\Expression\ExpressionInterface; use Yiisoft\Db\QueryBuilder\Condition\Interface\LikeConditionInterface; use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; use Yiisoft\Db\Schema\Quoter; @@ -49,6 +50,32 @@ public function build(LikeConditionInterface $expression, array &$params = []): return parent::build($expression, $params); } + protected function prepareColumn(LikeConditionInterface $expression, array &$params): string + { + $column = parent::prepareColumn($expression, $params); + + if ($expression->getCaseSensitive() === false) { + $column = 'LOWER(' . $column . ')'; + } + + return $column; + } + + protected function preparePlaceholderName( + string|ExpressionInterface $value, + LikeConditionInterface $expression, + ?array $escape, + array &$params, + ): string { + $placeholderName = parent::preparePlaceholderName($value, $expression, $escape, $params); + + if ($expression->getCaseSensitive() === false) { + $placeholderName = 'LOWER(' . $placeholderName . ')'; + } + + return $placeholderName; + } + /** * @return string Character used to escape special characters in `LIKE` conditions. By default, it's assumed to be * `!`. diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index 8835b095..37d460f9 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -5,7 +5,9 @@ namespace Yiisoft\Db\Oracle\Tests\Provider; use Exception; +use Yiisoft\Db\Command\Param; use Yiisoft\Db\Constant\ColumnType; +use Yiisoft\Db\Constant\DataType; use Yiisoft\Db\Constant\PseudoType; use Yiisoft\Db\Constant\ReferentialAction; use Yiisoft\Db\Constraint\ForeignKeyConstraint; @@ -75,14 +77,14 @@ public static function buildCondition(): array { $buildCondition = parent::buildCondition(); - $buildCondition['like-custom-1'] = [['like', 'a', 'b'], '"a" LIKE :qp0 ESCAPE \'!\'', [':qp0' => '%b%']]; + $buildCondition['like-custom-1'] = [['like', 'a', 'b'], '"a" LIKE :qp0 ESCAPE \'!\'', [':qp0' => new Param('%b%', DataType::STRING)]]; $buildCondition['like-custom-2'] = [ ['like', 'a', new Expression(':qp0', [':qp0' => '%b%'])], '"a" LIKE :qp0 ESCAPE \'!\'', [':qp0' => '%b%'], ]; $buildCondition['like-custom-3'] = [ - ['like', new Expression('CONCAT(col1, col2)'), 'b'], 'CONCAT(col1, col2) LIKE :qp0 ESCAPE \'!\'', [':qp0' => '%b%'], + ['like', new Expression('CONCAT(col1, col2)'), 'b'], 'CONCAT(col1, col2) LIKE :qp0 ESCAPE \'!\'', [':qp0' => new Param('%b%', DataType::STRING)], ]; return $buildCondition; diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 14db8739..d8933abe 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -128,14 +128,7 @@ public function testBatchInsert( parent::testBatchInsert($table, $rows, $columns, $expected, $expectedParams); } - /** - * @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\QueryBuilderProvider::buildCondition - * - * @throws Exception - * @throws InvalidConfigException - * @throws InvalidArgumentException - * @throws NotSupportedException - */ + #[DataProviderExternal(QueryBuilderProvider::class, 'buildCondition')] public function testBuildCondition( array|ExpressionInterface|string $condition, string|null $expected,