Passed
Push — move-escapesql-drivers ( fe0e5b...cf4f5a )
by Wilmer
27:19
created

LikeConditionBuilder::getEscapeSql()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Oracle\Builder;
6
7
use Exception;
8
use Yiisoft\Db\QueryBuilder\Conditions\Builder\LikeConditionBuilder as AbstractLikeConditionBuilder;
9
use Yiisoft\Db\QueryBuilder\Conditions\Interface\LikeConditionInterface;
10
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
11
use Yiisoft\Db\Schema\Quoter;
12
13
use function substr;
14
15
final class LikeConditionBuilder extends AbstractLikeConditionBuilder
16
{
17
    private string|null $escapeCharacter = '!';
18
19
    /**
20
     * `\` is initialized in {@see buildLikeCondition()} method since we need to choose replacement value based on
21
     * {@see Quoter::quoteValue()}.
22
     */
23
    protected array $escapingReplacements = [
24
        '%' => '!%',
25
        '_' => '!_',
26
        '!' => '!!',
27
    ];
28
29 31
    public function __construct(private QueryBuilderInterface $queryBuilder)
30
    {
31 31
        parent::__construct($queryBuilder, $this->getEscapeSql());
32
    }
33
34
    /**
35
     * @throws Exception
36
     */
37 31
    public function build(LikeConditionInterface $expression, array &$params = []): string
38
    {
39 31
        if (!isset($this->escapingReplacements['\\'])) {
40
            /*
41
             * Different pdo_oci8 versions may or may not implement PDO::quote(), so {@see Quoter::quoteValue()} may or
42
             * may not quote \.
43
             */
44 31
            $this->escapingReplacements['\\'] = substr((string) $this->queryBuilder->quoter()->quoteValue('\\'), 1, -1);
45
        }
46
47 31
        return parent::build($expression, $params);
48
    }
49
50
    /**
51
     * @return string character used to escape special characters in LIKE conditions.
52
     * By default, it's assumed to be `!`.
53
     */
54 31
    private function getEscapeSql(): string
55
    {
56 31
        if ($this->escapeCharacter !== null) {
57 31
            return " ESCAPE '{$this->escapeCharacter}'";
58
        }
59
60
        return '';
61
    }
62
}
63