Skip to content

Commit

Permalink
Escape backslash while replacing query parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Oct 8, 2020
1 parent de7032c commit e9c14c5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Sql/SqlFactory.php
Expand Up @@ -6,6 +6,7 @@

use function preg_replace;
use function Safe\sprintf;
use function str_replace;

/** @internal */
final class SqlFactory
Expand All @@ -22,7 +23,11 @@ public function createWithParameters(string $query, array $parameters) : string
{
/** @var mixed $value */
foreach ($parameters as $name => $value) {
$query = preg_replace(sprintf('~:%s(?!\w)~', $name), $this->valueFormatter->format($value, $name, $query), $query);
$query = preg_replace(
sprintf('~:%s(?!\w)~', $name),
str_replace('\\', '\\\\', $this->valueFormatter->format($value, $name, $query)),
$query
);
}

return $query;
Expand Down
13 changes: 13 additions & 0 deletions tests/Sql/SqlFactoryTest.php
Expand Up @@ -4,6 +4,7 @@

namespace SimPod\ClickHouseClient\Tests\Sql;

use SimPod\ClickHouseClient\Sql\Expression;
use SimPod\ClickHouseClient\Sql\SqlFactory;
use SimPod\ClickHouseClient\Sql\ValueFormatter;
use SimPod\ClickHouseClient\Tests\TestCaseBase;
Expand Down Expand Up @@ -58,5 +59,17 @@ public function providerCreateWithParameters() : iterable
'pingpong' => 2,
],
];

yield 'escape backslash' => [
<<<CLICKHOUSE
SELECT toIPv6('x\\\\')
CLICKHOUSE,
<<<CLICKHOUSE
SELECT :ping
CLICKHOUSE,
[
'ping' => Expression::new("toIPv6('x\\\\')"),
],
];
}
}

0 comments on commit e9c14c5

Please sign in to comment.