diff --git a/CHANGELOG.md b/CHANGELOG.md index 6877ac743..cd4437cc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ - Chg #388: Change supported PHP versions to `8.1 - 8.4` (@Tigrov) - Enh #388: Minor refactoring (@Tigrov) - Chg #390: Remove `yiisoft/json` dependency (@Tigrov) +- New #391: Add `caseSensitive` option to like condition (@vjik) ## 1.3.0 March 21, 2024 diff --git a/src/Builder/LikeConditionBuilder.php b/src/Builder/LikeConditionBuilder.php new file mode 100644 index 000000000..12d585947 --- /dev/null +++ b/src/Builder/LikeConditionBuilder.php @@ -0,0 +1,26 @@ +getCaseSensitive()) { + true => 'LIKE', + false => 'ILIKE', + default => $operator, + }; + + return [$andor, $not, $operator]; + } +} diff --git a/src/DQLQueryBuilder.php b/src/DQLQueryBuilder.php index a41acd8a7..20a3845ff 100644 --- a/src/DQLQueryBuilder.php +++ b/src/DQLQueryBuilder.php @@ -12,6 +12,7 @@ use Yiisoft\Db\Pgsql\Builder\ArrayExpressionBuilder; use Yiisoft\Db\Pgsql\Builder\ArrayOverlapsConditionBuilder; use Yiisoft\Db\Pgsql\Builder\JsonOverlapsConditionBuilder; +use Yiisoft\Db\Pgsql\Builder\LikeConditionBuilder; use Yiisoft\Db\Pgsql\Builder\StructuredExpressionBuilder; use Yiisoft\Db\Pgsql\Builder\ExpressionBuilder; use Yiisoft\Db\Pgsql\Builder\JsonExpressionBuilder; @@ -62,6 +63,7 @@ protected function defaultExpressionBuilders(): array JsonOverlapsCondition::class => JsonOverlapsConditionBuilder::class, StructuredExpression::class => StructuredExpressionBuilder::class, Expression::class => ExpressionBuilder::class, + LikeCondition::class => LikeConditionBuilder::class, ]; } } diff --git a/tests/Provider/QueryBuilderProvider.php b/tests/Provider/QueryBuilderProvider.php index 0d65e3940..a8b392df7 100644 --- a/tests/Provider/QueryBuilderProvider.php +++ b/tests/Provider/QueryBuilderProvider.php @@ -4,6 +4,8 @@ namespace Yiisoft\Db\Pgsql\Tests\Provider; +use Yiisoft\Db\Command\Param; +use Yiisoft\Db\Constant\DataType; use Yiisoft\Db\Constant\PseudoType; use Yiisoft\Db\Expression\ArrayExpression; use Yiisoft\Db\Expression\Expression; @@ -67,30 +69,30 @@ public static function buildCondition(): array [['or not ilike', 'name', []], '', []], /* simple ilike */ - [['ilike', 'name', 'heyho'], '"name" ILIKE :qp0', [':qp0' => '%heyho%']], - [['not ilike', 'name', 'heyho'], '"name" NOT ILIKE :qp0', [':qp0' => '%heyho%']], - [['or ilike', 'name', 'heyho'], '"name" ILIKE :qp0', [':qp0' => '%heyho%']], - [['or not ilike', 'name', 'heyho'], '"name" NOT ILIKE :qp0', [':qp0' => '%heyho%']], + [['ilike', 'name', 'heyho'], '"name" ILIKE :qp0', [':qp0' => new Param('%heyho%', DataType::STRING)]], + [['not ilike', 'name', 'heyho'], '"name" NOT ILIKE :qp0', [':qp0' => new Param('%heyho%', DataType::STRING)]], + [['or ilike', 'name', 'heyho'], '"name" ILIKE :qp0', [':qp0' => new Param('%heyho%', DataType::STRING)]], + [['or not ilike', 'name', 'heyho'], '"name" NOT ILIKE :qp0', [':qp0' => new Param('%heyho%', DataType::STRING)]], /* ilike for many values */ [ ['ilike', 'name', ['heyho', 'abc']], '"name" ILIKE :qp0 AND "name" ILIKE :qp1', - [':qp0' => '%heyho%', ':qp1' => '%abc%'], + [':qp0' => new Param('%heyho%', DataType::STRING), ':qp1' => new Param('%abc%', DataType::STRING)], ], [ ['not ilike', 'name', ['heyho', 'abc']], '"name" NOT ILIKE :qp0 AND "name" NOT ILIKE :qp1', - [':qp0' => '%heyho%', ':qp1' => '%abc%'], + [':qp0' => new Param('%heyho%', DataType::STRING), ':qp1' => new Param('%abc%', DataType::STRING)], ], [ ['or ilike', 'name', ['heyho', 'abc']], - '"name" ILIKE :qp0 OR "name" ILIKE :qp1', [':qp0' => '%heyho%', ':qp1' => '%abc%'], + '"name" ILIKE :qp0 OR "name" ILIKE :qp1', [':qp0' => new Param('%heyho%', DataType::STRING), ':qp1' => new Param('%abc%', DataType::STRING)], ], [ ['or not ilike', 'name', ['heyho', 'abc']], '"name" NOT ILIKE :qp0 OR "name" NOT ILIKE :qp1', - [':qp0' => '%heyho%', ':qp1' => '%abc%'], + [':qp0' => new Param('%heyho%', DataType::STRING), ':qp1' => new Param('%abc%', DataType::STRING)], ], /* Checks to verity that operators work correctly */ diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index d41fbf6fd..ab8b36949 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -111,9 +111,7 @@ public function testBatchInsert( parent::testBatchInsert($table, $rows, $columns, $expected, $expectedParams); } - /** - * @dataProvider \Yiisoft\Db\Pgsql\Tests\Provider\QueryBuilderProvider::buildCondition - */ + #[DataProviderExternal(QueryBuilderProvider::class, 'buildCondition')] public function testBuildCondition( array|ExpressionInterface|string $condition, string|null $expected,