Skip to content
Permalink
Browse files

SqlPreprocessor: generates IS NOT NULL (#226)

  • Loading branch information...
nargotik authored and dg committed Apr 23, 2019
1 parent 8b6230d commit 9eeebcc9217d8910143c183d5c1b66a7562f27ea
Showing with 14 additions and 2 deletions.
  1. +4 −1 src/Database/SqlPreprocessor.php
  2. +10 −1 tests/Database/SqlPreprocessor.phpt
@@ -242,7 +242,10 @@ private function formatValue($value, string $mode = null): string
}
} else {
$v = $this->formatValue($v);
$vx[] = $k . ' ' . ($operator ?: ($v === 'NULL' ? 'IS' : '=')) . ' ' . $v;
$operator = $v === 'NULL'
? ($operator === 'NOT' ? 'IS NOT' : ($operator ?: 'IS'))
: ($operator ?: '=');
$vx[] = $k . ' ' . $operator . ' ' . $v;
}
}
return $value ? '(' . implode(') ' . strtoupper($mode) . ' (', $vx) . ')' : '1=1';
@@ -102,14 +102,23 @@ test(function () use ($preprocessor) { // where
[$sql, $params] = $preprocessor->process(['SELECT id FROM author WHERE', [
'id' => null,
'x.name <>' => 'a',
'born NOT' => null,
'born' => [null, 1, 2, 3],
'web' => [],
]]);
Assert::same(reformat('SELECT id FROM author WHERE ([id] IS NULL) AND ([x].[name] <> ?) AND ([born] IN (NULL, ?, ?, ?)) AND (1=0)'), $sql);
Assert::same(reformat('SELECT id FROM author WHERE ([id] IS NULL) AND ([x].[name] <> ?) AND ([born] IS NOT NULL) AND ([born] IN (NULL, ?, ?, ?)) AND (1=0)'), $sql);
Assert::same(['a', 1, 2, 3], $params);
});
test(function () use ($preprocessor) { // where is not null
[$sql, $params] = $preprocessor->process(['SELECT id FROM author WHERE', [
'id NOT' => null,
]]);
Assert::same(reformat('SELECT id FROM author WHERE ([id] IS NOT NULL)'), $sql);
Assert::same([], $params);
});
test(function () use ($preprocessor) { // tuples
[$sql, $params] = $preprocessor->process(['SELECT * FROM book_tag WHERE (book_id, tag_id) IN (?)', [

0 comments on commit 9eeebcc

Please sign in to comment.
You can’t perform that action at this time.