Skip to content

Commit ce231f5

Browse files
committed
SqlPreprocessor: multi-insert accepts Row objects [Closes #247]
1 parent 848b816 commit ce231f5

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

src/Database/SqlPreprocessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ private function formatValue($value, string $mode = null): string
188188

189189
if ($mode === 'values') { // (key, key, ...) VALUES (value, value, ...)
190190
if (array_key_exists(0, $value)) { // multi-insert
191-
if (!is_array($value[0])) {
191+
if (!is_array($value[0]) && !$value[0] instanceof Row) {
192192
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.');
193193
}
194194
foreach ($value[0] as $k => $v) {

tests/Database/SqlPreprocessor.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,21 @@ test(function () use ($preprocessor) { // multi insert
355355
});
356356

357357

358+
test(function () use ($preprocessor) { // multi insert & Rows
359+
[$sql, $params] = $preprocessor->process(['INSERT INTO author', [
360+
Nette\Database\Row::from(['name' => 'Catelyn Stark', 'born' => new DateTime('2011-11-11')]),
361+
Nette\Database\Row::from(['name' => 'Sansa Stark', 'born' => new DateTime('2021-11-11')]),
362+
]]);
363+
364+
Assert::same(reformat([
365+
'sqlite' => 'INSERT INTO author ([name], [born]) SELECT ?, 1320966000 UNION ALL SELECT ?, 1636585200',
366+
'sqlsrv' => "INSERT INTO author ([name], [born]) VALUES (?, '2011-11-11T00:00:00'), (?, '2021-11-11T00:00:00')",
367+
"INSERT INTO author ([name], [born]) VALUES (?, '2011-11-11 00:00:00'), (?, '2021-11-11 00:00:00')",
368+
]), $sql);
369+
Assert::same(['Catelyn Stark', 'Sansa Stark'], $params);
370+
});
371+
372+
358373
test(function () use ($preprocessor) { // multi insert respects keys
359374
[$sql, $params] = $preprocessor->process(['INSERT INTO author', [
360375
['name' => 'Catelyn Stark', 'born' => new DateTime('2011-11-11')],

0 commit comments

Comments
 (0)