Skip to content

Commit dbaa470

Browse files
authored
Case sensitive option in LikeCondition (#349)
1 parent 0a0af8e commit dbaa470

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
- New #345: Add parameters `$ifExists` and `$cascade` to `CommandInterface::dropTable()` and
3434
`DDLQueryBuilderInterface::dropTable()` methods (@vjik)
3535
- Chg #348: Remove usage of `hasLimit()` and `hasOffset()` methods of `DQLQueryBuilder` class (@Tigrov)
36+
- New #349: Add `caseSensitive` option to like condition (@vjik)
3637

3738
## 1.2.0 March 21, 2024
3839

src/Builder/LikeConditionBuilder.php

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace Yiisoft\Db\Mssql\Builder;
66

7+
use Yiisoft\Db\Exception\NotSupportedException;
8+
use Yiisoft\Db\QueryBuilder\Condition\Interface\LikeConditionInterface;
9+
710
/**
811
* Build an object of {@see \Yiisoft\Db\QueryBuilder\Condition\LikeCondition} into SQL expressions for MSSQL Server.
912
*/
@@ -20,4 +23,12 @@ final class LikeConditionBuilder extends \Yiisoft\Db\QueryBuilder\Condition\Buil
2023
']' => '[]]',
2124
'\\' => '[\\]',
2225
];
26+
27+
public function build(LikeConditionInterface $expression, array &$params = []): string
28+
{
29+
if ($expression->getCaseSensitive() === true) {
30+
throw new NotSupportedException('MSSQL doesn\'t support case-sensitive "LIKE" conditions.');
31+
}
32+
return parent::build($expression, $params);
33+
}
2334
}

tests/QueryTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
namespace Yiisoft\Db\Mssql\Tests;
66

7+
use PHPUnit\Framework\Attributes\DataProvider;
78
use Throwable;
89
use Yiisoft\Db\Exception\Exception;
910
use Yiisoft\Db\Exception\InvalidConfigException;
11+
use Yiisoft\Db\Exception\NotSupportedException;
1012
use Yiisoft\Db\Mssql\Tests\Support\TestTrait;
1113
use Yiisoft\Db\Query\Query;
1214
use Yiisoft\Db\Tests\Common\CommonQueryTest;
@@ -59,4 +61,19 @@ public function testUnion(): void
5961

6062
$this->assertCount(4, $data);
6163
}
64+
65+
#[DataProvider('dataLikeCaseSensitive')]
66+
public function testLikeCaseSensitive(mixed $expected, string $value): void
67+
{
68+
$db = $this->getConnection(true);
69+
70+
$query = (new Query($db))
71+
->select('name')
72+
->from('customer')
73+
->where(['like', 'name', $value, 'caseSensitive' => true]);
74+
75+
$this->expectException(NotSupportedException::class);
76+
$this->expectExceptionMessage('MSSQL doesn\'t support case-sensitive "LIKE" conditions.');
77+
$query->scalar();
78+
}
6279
}

0 commit comments

Comments
 (0)