Skip to content

Commit

Permalink
SqlPreprocessor: added mode ?list
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 27, 2020
1 parent 1eb6b00 commit 6c2a0a5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/Database/SqlPreprocessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ class SqlPreprocessor
MODE_SET = 'set', // key=value, key=value, ...
MODE_VALUES = 'values', // (key, key, ...) VALUES (value, value, ...)
MODE_ORDER = 'order', // key, key DESC, ...
MODE_LIST = 'list', // value, value, ... | (tuple), (tuple), ...
MODE_AUTO = 'auto'; // arrayMode for arrays

private const MODES = [self::MODE_AND, self::MODE_OR, self::MODE_SET, self::MODE_VALUES, self::MODE_ORDER];
private const MODES = [self::MODE_AND, self::MODE_OR, self::MODE_SET, self::MODE_VALUES, self::MODE_ORDER, self::MODE_LIST];

private const ARRAY_MODES = [
'INSERT' => self::MODE_VALUES,
Expand Down Expand Up @@ -67,7 +68,7 @@ class SqlPreprocessor
/** @var bool */
private $useParams;

/** @var string|null values|set|and|order */
/** @var string|null values|set|and|order|items */
private $arrayMode;


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

} elseif ($mode === self::MODE_LIST) { // value, value, ... | (tuple), (tuple), ...
foreach ($value as $k => $v) {
$vx[] = is_array($v) ? '(' . $this->formatValue($v, self::MODE_LIST) . ')' : $this->formatValue($v);
}
return implode(', ', $vx);

} elseif ($mode === self::MODE_AND || $mode === self::MODE_OR) { // (key [operator] value) AND ...
foreach ($value as $k => $v) {
if (is_int($k)) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/SqlPreprocessor.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,10 @@ test(function () use ($preprocessor) { // ?values
});


test(function () use ($preprocessor) { // automatic detection faild
test(function () use ($preprocessor) { // automatic detection failed
Assert::exception(function () use ($preprocessor) {
$preprocessor->process(['INSERT INTO author (name) SELECT name FROM user WHERE id IN (?)', [11, 12]]);
}, 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.');
}, 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.');
});


Expand Down

0 comments on commit 6c2a0a5

Please sign in to comment.