Skip to content

Commit

Permalink
SqlPreprocessor: generates IS NOT NULL (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
nargotik authored and dg committed Apr 23, 2019
1 parent 8b6230d commit 9eeebcc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/Database/SqlPreprocessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
11 changes: 10 additions & 1 deletion tests/Database/SqlPreprocessor.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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 (?)', [
Expand Down

0 comments on commit 9eeebcc

Please sign in to comment.