Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions src/Builder/LikeConditionBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Pgsql\Builder;

use Yiisoft\Db\QueryBuilder\Condition\Interface\LikeConditionInterface;

/**
* Build an object of {@see LikeConditionInterface} into SQL expressions for PostgreSQL Server.
*/
final class LikeConditionBuilder extends \Yiisoft\Db\QueryBuilder\Condition\Builder\LikeConditionBuilder
{
protected function parseOperator(LikeConditionInterface $expression): array
{
[$andor, $not, $operator] = parent::parseOperator($expression);

$operator = match ($expression->getCaseSensitive()) {
true => 'LIKE',
false => 'ILIKE',
default => $operator,
};

return [$andor, $not, $operator];
}
}
2 changes: 2 additions & 0 deletions src/DQLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,6 +63,7 @@ protected function defaultExpressionBuilders(): array
JsonOverlapsCondition::class => JsonOverlapsConditionBuilder::class,
StructuredExpression::class => StructuredExpressionBuilder::class,
Expression::class => ExpressionBuilder::class,
LikeCondition::class => LikeConditionBuilder::class,
];
}
}
18 changes: 10 additions & 8 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down
4 changes: 1 addition & 3 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading