Skip to content

Commit 4c0e8d0

Browse files
committed
SqlPreprocessor: INSERT with multiple arrays is key-dependent [Closes #238]
1 parent 5b88ca3 commit 4c0e8d0

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/Database/SqlPreprocessor.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ private function formatValue($value, string $mode = null): string
196196
}
197197
foreach ($value as $val) {
198198
$vx2 = [];
199-
foreach ($val as $v) {
200-
$vx2[] = $this->formatValue($v);
199+
foreach ($value[0] as $k => $foo) {
200+
$vx2[] = $this->formatValue($val[$k]);
201201
}
202202
$vx[] = implode(', ', $vx2);
203203
}

tests/Database/SqlPreprocessor.phpt

+15
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 respects keys
359+
[$sql, $params] = $preprocessor->process(['INSERT INTO author', [
360+
['name' => 'Catelyn Stark', 'born' => new DateTime('2011-11-11')],
361+
['born' => new DateTime('2021-11-11'), 'name' => 'Sansa Stark'],
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 ?values
359374
[$sql, $params] = $preprocessor->process(['INSERT INTO author ?values', [
360375
['name' => 'Catelyn Stark', 'born' => new DateTime('2011-11-11')],

0 commit comments

Comments
 (0)