Skip to content

Commit 9eeebcc

Browse files
nargotikdg
authored andcommitted
SqlPreprocessor: generates IS NOT NULL (#226)
1 parent 8b6230d commit 9eeebcc

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/Database/SqlPreprocessor.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,10 @@ private function formatValue($value, string $mode = null): string
242242
}
243243
} else {
244244
$v = $this->formatValue($v);
245-
$vx[] = $k . ' ' . ($operator ?: ($v === 'NULL' ? 'IS' : '=')) . ' ' . $v;
245+
$operator = $v === 'NULL'
246+
? ($operator === 'NOT' ? 'IS NOT' : ($operator ?: 'IS'))
247+
: ($operator ?: '=');
248+
$vx[] = $k . ' ' . $operator . ' ' . $v;
246249
}
247250
}
248251
return $value ? '(' . implode(') ' . strtoupper($mode) . ' (', $vx) . ')' : '1=1';

tests/Database/SqlPreprocessor.phpt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,23 @@ test(function () use ($preprocessor) { // where
102102
[$sql, $params] = $preprocessor->process(['SELECT id FROM author WHERE', [
103103
'id' => null,
104104
'x.name <>' => 'a',
105+
'born NOT' => null,
105106
'born' => [null, 1, 2, 3],
106107
'web' => [],
107108
]]);
108109

109-
Assert::same(reformat('SELECT id FROM author WHERE ([id] IS NULL) AND ([x].[name] <> ?) AND ([born] IN (NULL, ?, ?, ?)) AND (1=0)'), $sql);
110+
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);
110111
Assert::same(['a', 1, 2, 3], $params);
111112
});
112113

114+
test(function () use ($preprocessor) { // where is not null
115+
[$sql, $params] = $preprocessor->process(['SELECT id FROM author WHERE', [
116+
'id NOT' => null,
117+
]]);
118+
119+
Assert::same(reformat('SELECT id FROM author WHERE ([id] IS NOT NULL)'), $sql);
120+
Assert::same([], $params);
121+
});
113122

114123
test(function () use ($preprocessor) { // tuples
115124
[$sql, $params] = $preprocessor->process(['SELECT * FROM book_tag WHERE (book_id, tag_id) IN (?)', [

0 commit comments

Comments
 (0)