Skip to content

Commit a32ae78

Browse files
committed
SqlPreprocessor: added mode ?list
1 parent 393a34a commit a32ae78

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/Database/SqlPreprocessor.php

Lines changed: 11 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

@@ -240,6 +241,14 @@ private function formatValue($value, string $mode = null): string
240241
}
241242
return implode(', ', $vx);
242243

244+
} elseif ($mode === self::MODE_LIST) { // value, value, ... | (tuple), (tuple), ...
245+
foreach ($value as $k => $v) {
246+
$vx[] = is_array($v)
247+
? '(' . $this->formatValue($v, self::MODE_LIST) . ')'
248+
: $this->formatValue($v);
249+
}
250+
return implode(', ', $vx);
251+
243252
} elseif ($mode === self::MODE_AND || $mode === self::MODE_OR) { // (key [operator] value) AND ...
244253
foreach ($value as $k => $v) {
245254
if (is_int($k)) {

tests/Database/SqlPreprocessor.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ test('?values', function () use ($preprocessor) {
344344
test('automatic detection failed', function () use ($preprocessor) {
345345
Assert::exception(function () use ($preprocessor) {
346346
$preprocessor->process(['INSERT INTO author (name) SELECT name FROM user WHERE id IN (?)', [11, 12]]);
347-
}, 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.');
347+
}, 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.');
348348
});
349349

350350

0 commit comments

Comments
 (0)