Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Database/SqlPreprocessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class SqlPreprocessor
{
use Nette\SmartObject;

/** @var array */
private const MODE_LIST = ['and', 'or', 'set', 'values', 'order'];

/** @var Connection */
private $connection;

Expand Down Expand Up @@ -162,6 +165,9 @@ private function formatValue($value, $mode = NULL)

if ($mode === 'values') { // (key, key, ...) VALUES (value, value, ...)
if (array_key_exists(0, $value)) { // multi-insert
if (!is_array($value[0])) {
throw new Nette\InvalidArgumentException('Automaticaly detected multi-insert, but values aren\'t array. If you need try to change mode like "?[' . implode('|', self::MODE_LIST) . ']". Mode "' . $mode . '" was used.');
}
foreach ($value[0] as $k => $v) {
$kx[] = $this->delimite($k);
}
Expand Down Expand Up @@ -228,7 +234,7 @@ private function formatValue($value, $mode = NULL)
throw new Nette\InvalidArgumentException("Unknown placeholder ?$mode.");
}

} elseif (in_array($mode, ['and', 'or', 'set', 'values', 'order'], TRUE)) {
} elseif (in_array($mode, self::MODE_LIST, TRUE)) {
$type = gettype($value);
throw new Nette\InvalidArgumentException("Placeholder ?$mode expects array or Traversable object, $type given.");

Expand Down
7 changes: 7 additions & 0 deletions tests/Database/SqlPreprocessor.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,13 @@ test(function () use ($preprocessor) { // ?values
});


test(function () use ($preprocessor) { // automatic detection faild
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.');
});


test(function () use ($preprocessor) { // multi insert
[$sql, $params] = $preprocessor->process(['INSERT INTO author', [
['name' => 'Catelyn Stark', 'born' => new DateTime('2011-11-11')],
Expand Down