Skip to content

Commit

Permalink
Fix 7: Move escapeSql to driver implementations. (#390)
Browse files Browse the repository at this point in the history
Move escapeSql to driver implementations.
  • Loading branch information
terabytesoftw committed Nov 21, 2022
1 parent 8e2631c commit 763c33f
Showing 1 changed file with 6 additions and 19 deletions.
25 changes: 6 additions & 19 deletions src/QueryBuilder/Conditions/Builder/LikeConditionBuilder.php
Expand Up @@ -23,8 +23,10 @@
*/
class LikeConditionBuilder implements ExpressionBuilderInterface
{
public function __construct(private QueryBuilderInterface $queryBuilder)
{
public function __construct(
private QueryBuilderInterface $queryBuilder,
private string|null $escapeSql = null
) {
}

/**
Expand All @@ -36,7 +38,6 @@ public function __construct(private QueryBuilderInterface $queryBuilder)
'_' => '\_',
'\\' => '\\\\',
];
protected string|null $escapeCharacter = null;

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

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

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

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

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

return [$andor, $not, $operator];
}

/**
* @return string character used to escape special characters in LIKE conditions. By default,
* it's assumed to be `\`.
*/
private function getEscapeSql(): string
{
if ($this->escapeCharacter !== null) {
return " ESCAPE '{$this->escapeCharacter}'";
}

return '';
}
}

0 comments on commit 763c33f

Please sign in to comment.