Skip to content

Commit 763c33f

Browse files
Fix 7: Move escapeSql to driver implementations. (#390)
Move escapeSql to driver implementations.
1 parent 8e2631c commit 763c33f

1 file changed

Lines changed: 6 additions & 19 deletions

File tree

src/QueryBuilder/Conditions/Builder/LikeConditionBuilder.php

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
*/
2424
class LikeConditionBuilder implements ExpressionBuilderInterface
2525
{
26-
public function __construct(private QueryBuilderInterface $queryBuilder)
27-
{
26+
public function __construct(
27+
private QueryBuilderInterface $queryBuilder,
28+
private string|null $escapeSql = null
29+
) {
2830
}
2931

3032
/**
@@ -36,7 +38,6 @@ public function __construct(private QueryBuilderInterface $queryBuilder)
3638
'_' => '\_',
3739
'\\' => '\\\\',
3840
];
39-
protected string|null $escapeCharacter = null;
4041

4142
/**
4243
* @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException
@@ -68,7 +69,6 @@ public function build(LikeConditionInterface $expression, array &$params = []):
6869
$column = $this->queryBuilder->quoter()->quoteColumnName($column);
6970
}
7071

71-
$escapeSql = $this->getEscapeSql();
7272
$parts = [];
7373

7474
/** @psalm-var string[] $values */
@@ -81,7 +81,7 @@ public function build(LikeConditionInterface $expression, array &$params = []):
8181
$params
8282
);
8383
}
84-
$parts[] = "{$column} {$operator} {$phName}{$escapeSql}";
84+
$parts[] = "{$column} {$operator} {$phName}{$this->escapeSql}";
8585
}
8686

8787
return implode($andor, $parts);
@@ -95,7 +95,7 @@ public function build(LikeConditionInterface $expression, array &$params = []):
9595
protected function parseOperator(string $operator): array
9696
{
9797
if (!preg_match('/^(AND |OR |)((NOT |)I?LIKE)/', $operator, $matches)) {
98-
throw new InvalidArgumentException("Invalid operator '$operator'.");
98+
throw new InvalidArgumentException("Invalid operator in like condition: \"{$operator}\"");
9999
}
100100

101101
$andor = ' ' . (!empty($matches[1]) ? $matches[1] : 'AND ');
@@ -104,17 +104,4 @@ protected function parseOperator(string $operator): array
104104

105105
return [$andor, $not, $operator];
106106
}
107-
108-
/**
109-
* @return string character used to escape special characters in LIKE conditions. By default,
110-
* it's assumed to be `\`.
111-
*/
112-
private function getEscapeSql(): string
113-
{
114-
if ($this->escapeCharacter !== null) {
115-
return " ESCAPE '{$this->escapeCharacter}'";
116-
}
117-
118-
return '';
119-
}
120107
}

0 commit comments

Comments
 (0)