Skip to content

Commit a0c8712

Browse files
committed
SqlPreprocessor: added mode ?list
1 parent edffeb6 commit a0c8712

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/Database/SqlPreprocessor.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ class SqlPreprocessor
2525
MODE_SET = 'set', // key=value, key=value, ...
2626
MODE_VALUES = 'values', // (key, key, ...) VALUES (value, value, ...)
2727
MODE_ORDER = 'order', // key, key DESC, ...
28+
MODE_LIST = 'list', // value, value, ... | (tuple), (tuple), ...
2829
MODE_AUTO = 'auto'; // arrayMode for arrays
2930

30-
private const MODES = [self::MODE_AND, self::MODE_OR, self::MODE_SET, self::MODE_VALUES, self::MODE_ORDER];
31+
private const MODES = [self::MODE_AND, self::MODE_OR, self::MODE_SET, self::MODE_VALUES, self::MODE_ORDER, self::MODE_LIST];
3132

3233
private const ARRAY_MODES = [
3334
'INSERT' => self::MODE_VALUES,
@@ -67,7 +68,7 @@ class SqlPreprocessor
6768
/** @var bool */
6869
private $useParams;
6970

70-
/** @var string|null values|set|and|order */
71+
/** @var string|null values|set|and|order|items */
7172
private $arrayMode;
7273

7374

@@ -238,6 +239,12 @@ private function formatValue($value, string $mode = null): string
238239
}
239240
return implode(', ', $vx);
240241

242+
} elseif ($mode === self::MODE_LIST) { // value, value, ... | (tuple), (tuple), ...
243+
foreach ($value as $k => $v) {
244+
$vx[] = is_array($v) ? '(' . $this->formatValue($v, self::MODE_LIST) . ')' : $this->formatValue($v);
245+
}
246+
return implode(', ', $vx);
247+
241248
} elseif ($mode === self::MODE_AND || $mode === self::MODE_OR) { // (key [operator] value) AND ...
242249
foreach ($value as $k => $v) {
243250
if (is_int($k)) {

tests/Database/SqlPreprocessor.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,10 @@ test(function () use ($preprocessor) { // ?values
347347
});
348348

349349

350-
test(function () use ($preprocessor) { // automatic detection faild
350+
test(function () use ($preprocessor) { // automatic detection failed
351351
Assert::exception(function () use ($preprocessor) {
352352
$preprocessor->process(['INSERT INTO author (name) SELECT name FROM user WHERE id IN (?)', [11, 12]]);
353-
}, Nette\InvalidArgumentException::class, 'Automaticaly detected multi-insert, but values aren\'t array. If you need try to change mode like "?[and|or|set|values|order]". Mode "values" was used.');
353+
}, Nette\InvalidArgumentException::class, 'Automaticaly detected multi-insert, but values aren\'t array. If you need try to change mode like "?[and|or|set|values|order|list]". Mode "values" was used.');
354354
});
355355

356356

0 commit comments

Comments
 (0)