Skip to content

Commit

Permalink
Fix handling null in SELECT, WHERE and HAVING
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Dec 23, 2020
1 parent 4d4693c commit 8a6a8dd
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 24 deletions.
17 changes: 0 additions & 17 deletions phpstan-baseline.neon

This file was deleted.

3 changes: 0 additions & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,3 @@ parameters:
ignoreErrors:
# Adds unnecessary maintanence overhead. We rather rely on PHPStan telling us the method returns unhandled FALSE
- "~Class DateTime(Immutable)? is unsafe to use. Its methods can return FALSE instead of throwing an exception. Please add 'use Safe\\\\DateTime(Immutable)?;' at the beginning of the file to use the variant provided by the 'thecodingmachine/safe' library~"

includes:
- phpstan-baseline.neon
14 changes: 14 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.3.1@2feba22a005a18bf31d4c7b9bdb9252c73897476">
<file src="src/Sql/SqlFactory.php">
<InvalidReturnStatement occurrences="1">
<code>$query</code>
</InvalidReturnStatement>
<InvalidReturnType occurrences="1">
<code>string</code>
</InvalidReturnType>
<PossiblyInvalidArgument occurrences="1">
<code>$query</code>
</PossiblyInvalidArgument>
</file>
</files>
1 change: 1 addition & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorBaseline="psalm-baseline.xml"
>
<projectFiles>
<directory name="src"/>
Expand Down
4 changes: 3 additions & 1 deletion src/Sql/SqlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace SimPod\ClickHouseClient\Sql;

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

Expand All @@ -30,6 +30,8 @@ public function createWithParameters(string $query, array $parameters) : string
);
}

$query = preg_replace('~ ?=([\s]*?)IS NULL~', '$1IS NULL', $query);

return $query;
}
}
11 changes: 9 additions & 2 deletions src/Sql/ValueFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ public function format($value, ?string $paramName = null, ?string $sql = null) :
}

if ($value === null) {
return 'IS NULL';
if (
$paramName !== null && $sql !== null
&& preg_match(sprintf('~(HAVING|WHERE)[\s\S]*?=\s*?:%s~', $paramName), $sql) === 1
) {
return 'IS NULL';
}

return 'NULL';
}

if ($value instanceof DateTimeImmutable) {
Expand All @@ -72,7 +79,7 @@ public function format($value, ?string $paramName = null, ?string $sql = null) :
if (is_array($value)) {
if (
$paramName !== null && $sql !== null
&& preg_match(sprintf('~\s+IN\s+\\(:%s\\)~', $paramName), $sql) === 1
&& preg_match(sprintf('~\s+?IN\s+?\\(:%s\\)~', $paramName), $sql) === 1
) {
return implode(
',',
Expand Down
10 changes: 10 additions & 0 deletions tests/Sql/SqlFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ public function providerCreateWithParameters() : iterable
],
];

yield 'null filter' => [
<<<CLICKHOUSE
SELECT 1 FROM system.one WHERE dummy IS NULL
CLICKHOUSE,
<<<CLICKHOUSE
SELECT 1 FROM system.one WHERE dummy = :null
CLICKHOUSE,
['null' => null],
];

yield 'escape backslash' => [
<<<CLICKHOUSE
SELECT toIPv6('x\\\\')
Expand Down
16 changes: 15 additions & 1 deletion tests/Sql/ValueFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,21 @@ public function providerFormat() : iterable
yield 'float .5' => ['1.5', 1.5];
yield 'string' => ["'ping'", 'ping'];
yield 'string escaped' => ["'ping\\\\n'", 'ping\n'];
yield 'null' => ['IS NULL', null];
yield 'null' => ['NULL', null];
yield 'null with WHERE' => ['IS NULL', null, 'null', 'SELECT 1 FROM table WHERE x = :null'];
yield 'null with multiline WHERE' => [
'IS NULL',
null,
'null',
<<<SQL
SELECT 1 FROM table WHERE
1 = 1
AND x = :null
SQL,
];

yield 'null with HAVING' => ['IS NULL', null, 'null', 'SELECT 1 FROM table HAVING x = :null'];
yield 'null with SELECT' => ['NULL', null, 'SELECT :null'];
yield 'array' => ["['a','b','c']", ['a', 'b', 'c']];
yield 'array in array' => ["[['a']]", [['a']]];
yield 'array with null' => ['[NULL]', [null]];
Expand Down

0 comments on commit 8a6a8dd

Please sign in to comment.