Skip to content

Commit

Permalink
SqlPreprocessor: INSERT with multiple arrays is key-dependent [Closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 14, 2019
1 parent 5b88ca3 commit 4c0e8d0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Database/SqlPreprocessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ private function formatValue($value, string $mode = null): string
}
foreach ($value as $val) {
$vx2 = [];
foreach ($val as $v) {
$vx2[] = $this->formatValue($v);
foreach ($value[0] as $k => $foo) {
$vx2[] = $this->formatValue($val[$k]);
}
$vx[] = implode(', ', $vx2);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Database/SqlPreprocessor.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,21 @@ test(function () use ($preprocessor) { // multi insert
});


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

Assert::same(reformat([
'sqlite' => 'INSERT INTO author ([name], [born]) SELECT ?, 1320966000 UNION ALL SELECT ?, 1636585200',
'sqlsrv' => "INSERT INTO author ([name], [born]) VALUES (?, '2011-11-11T00:00:00'), (?, '2021-11-11T00:00:00')",
"INSERT INTO author ([name], [born]) VALUES (?, '2011-11-11 00:00:00'), (?, '2021-11-11 00:00:00')",
]), $sql);
Assert::same(['Catelyn Stark', 'Sansa Stark'], $params);
});


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

0 comments on commit 4c0e8d0

Please sign in to comment.