LikeConditionBuilder   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
eloc 10
c 3
b 0
f 0
dl 0
loc 42
ccs 8
cts 8
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 11 2
A getEscapeSql() 0 3 2
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Oracle\Builder;
6
7
use Exception;
8
use Yiisoft\Db\QueryBuilder\Condition\Interface\LikeConditionInterface;
9
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
10
use Yiisoft\Db\Schema\Quoter;
11
12
use function substr;
13
14
/**
15
 * Build an object of {@see `\Yiisoft\Db\QueryBuilder\Condition\LikeCondition`} into SQL expressions for Oracle Server.
16
 */
17
final class LikeConditionBuilder extends \Yiisoft\Db\QueryBuilder\Condition\Builder\LikeConditionBuilder
18
{
19
    private string $escapeCharacter = '!';
20
21
    /**
22
     * `\` is initialized in {@see buildLikeCondition()} method since there is a need to choose replacement value
23
     * based on {@see Quoter::quoteValue()}.
24
     */
25
    protected array $escapingReplacements = [
26
        '%' => '!%',
27
        '_' => '!_',
28
        '!' => '!!',
29
    ];
30
31 33
    public function __construct(private QueryBuilderInterface $queryBuilder)
32
    {
33 33
        parent::__construct($queryBuilder, $this->getEscapeSql());
34
    }
35
36
    /**
37
     * @throws Exception
38
     */
39 33
    public function build(LikeConditionInterface $expression, array &$params = []): string
40
    {
41 33
        if (!isset($this->escapingReplacements['\\'])) {
42
            /*
43
             * Different pdo_oci8 versions may or may not implement `PDO::quote()`, so {@see Quoter::quoteValue()} may or
44
             * may not quote `\`.
45
             */
46 33
            $this->escapingReplacements['\\'] = substr((string) $this->queryBuilder->quoter()->quoteValue('\\'), 1, -1);
47
        }
48
49 33
        return parent::build($expression, $params);
50
    }
51
52
    /**
53
     * @return string Character used to escape special characters in `LIKE` conditions. By default, it's assumed to be
54
     * `!`.
55
     */
56 33
    private function getEscapeSql(): string
57
    {
58 33
        return $this->escapeCharacter !== '' ? " ESCAPE '$this->escapeCharacter'" : '';
59
    }
60
}
61