Skip to content

Commit a46aa0a

Browse files
authored
Case sensitive option in LikeCondition (#391)
1 parent 8f567c9 commit a46aa0a

File tree

5 files changed

+40
-11
lines changed

5 files changed

+40
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
- Chg #388: Change supported PHP versions to `8.1 - 8.4` (@Tigrov)
3535
- Enh #388: Minor refactoring (@Tigrov)
3636
- Chg #390: Remove `yiisoft/json` dependency (@Tigrov)
37+
- New #391: Add `caseSensitive` option to like condition (@vjik)
3738

3839
## 1.3.0 March 21, 2024
3940

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Db\Pgsql\Builder;
6+
7+
use Yiisoft\Db\QueryBuilder\Condition\Interface\LikeConditionInterface;
8+
9+
/**
10+
* Build an object of {@see LikeConditionInterface} into SQL expressions for PostgreSQL Server.
11+
*/
12+
final class LikeConditionBuilder extends \Yiisoft\Db\QueryBuilder\Condition\Builder\LikeConditionBuilder
13+
{
14+
protected function parseOperator(LikeConditionInterface $expression): array
15+
{
16+
[$andor, $not, $operator] = parent::parseOperator($expression);
17+
18+
$operator = match ($expression->getCaseSensitive()) {
19+
true => 'LIKE',
20+
false => 'ILIKE',
21+
default => $operator,
22+
};
23+
24+
return [$andor, $not, $operator];
25+
}
26+
}

src/DQLQueryBuilder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Yiisoft\Db\Pgsql\Builder\ArrayExpressionBuilder;
1313
use Yiisoft\Db\Pgsql\Builder\ArrayOverlapsConditionBuilder;
1414
use Yiisoft\Db\Pgsql\Builder\JsonOverlapsConditionBuilder;
15+
use Yiisoft\Db\Pgsql\Builder\LikeConditionBuilder;
1516
use Yiisoft\Db\Pgsql\Builder\StructuredExpressionBuilder;
1617
use Yiisoft\Db\Pgsql\Builder\ExpressionBuilder;
1718
use Yiisoft\Db\Pgsql\Builder\JsonExpressionBuilder;
@@ -62,6 +63,7 @@ protected function defaultExpressionBuilders(): array
6263
JsonOverlapsCondition::class => JsonOverlapsConditionBuilder::class,
6364
StructuredExpression::class => StructuredExpressionBuilder::class,
6465
Expression::class => ExpressionBuilder::class,
66+
LikeCondition::class => LikeConditionBuilder::class,
6567
];
6668
}
6769
}

tests/Provider/QueryBuilderProvider.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Yiisoft\Db\Pgsql\Tests\Provider;
66

7+
use Yiisoft\Db\Command\Param;
8+
use Yiisoft\Db\Constant\DataType;
79
use Yiisoft\Db\Constant\PseudoType;
810
use Yiisoft\Db\Expression\ArrayExpression;
911
use Yiisoft\Db\Expression\Expression;
@@ -67,30 +69,30 @@ public static function buildCondition(): array
6769
[['or not ilike', 'name', []], '', []],
6870

6971
/* simple ilike */
70-
[['ilike', 'name', 'heyho'], '"name" ILIKE :qp0', [':qp0' => '%heyho%']],
71-
[['not ilike', 'name', 'heyho'], '"name" NOT ILIKE :qp0', [':qp0' => '%heyho%']],
72-
[['or ilike', 'name', 'heyho'], '"name" ILIKE :qp0', [':qp0' => '%heyho%']],
73-
[['or not ilike', 'name', 'heyho'], '"name" NOT ILIKE :qp0', [':qp0' => '%heyho%']],
72+
[['ilike', 'name', 'heyho'], '"name" ILIKE :qp0', [':qp0' => new Param('%heyho%', DataType::STRING)]],
73+
[['not ilike', 'name', 'heyho'], '"name" NOT ILIKE :qp0', [':qp0' => new Param('%heyho%', DataType::STRING)]],
74+
[['or ilike', 'name', 'heyho'], '"name" ILIKE :qp0', [':qp0' => new Param('%heyho%', DataType::STRING)]],
75+
[['or not ilike', 'name', 'heyho'], '"name" NOT ILIKE :qp0', [':qp0' => new Param('%heyho%', DataType::STRING)]],
7476

7577
/* ilike for many values */
7678
[
7779
['ilike', 'name', ['heyho', 'abc']],
7880
'"name" ILIKE :qp0 AND "name" ILIKE :qp1',
79-
[':qp0' => '%heyho%', ':qp1' => '%abc%'],
81+
[':qp0' => new Param('%heyho%', DataType::STRING), ':qp1' => new Param('%abc%', DataType::STRING)],
8082
],
8183
[
8284
['not ilike', 'name', ['heyho', 'abc']],
8385
'"name" NOT ILIKE :qp0 AND "name" NOT ILIKE :qp1',
84-
[':qp0' => '%heyho%', ':qp1' => '%abc%'],
86+
[':qp0' => new Param('%heyho%', DataType::STRING), ':qp1' => new Param('%abc%', DataType::STRING)],
8587
],
8688
[
8789
['or ilike', 'name', ['heyho', 'abc']],
88-
'"name" ILIKE :qp0 OR "name" ILIKE :qp1', [':qp0' => '%heyho%', ':qp1' => '%abc%'],
90+
'"name" ILIKE :qp0 OR "name" ILIKE :qp1', [':qp0' => new Param('%heyho%', DataType::STRING), ':qp1' => new Param('%abc%', DataType::STRING)],
8991
],
9092
[
9193
['or not ilike', 'name', ['heyho', 'abc']],
9294
'"name" NOT ILIKE :qp0 OR "name" NOT ILIKE :qp1',
93-
[':qp0' => '%heyho%', ':qp1' => '%abc%'],
95+
[':qp0' => new Param('%heyho%', DataType::STRING), ':qp1' => new Param('%abc%', DataType::STRING)],
9496
],
9597

9698
/* Checks to verity that operators work correctly */

tests/QueryBuilderTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,7 @@ public function testBatchInsert(
111111
parent::testBatchInsert($table, $rows, $columns, $expected, $expectedParams);
112112
}
113113

114-
/**
115-
* @dataProvider \Yiisoft\Db\Pgsql\Tests\Provider\QueryBuilderProvider::buildCondition
116-
*/
114+
#[DataProviderExternal(QueryBuilderProvider::class, 'buildCondition')]
117115
public function testBuildCondition(
118116
array|ExpressionInterface|string $condition,
119117
string|null $expected,

0 commit comments

Comments
 (0)