Skip to content

Commit 3a12769

Browse files
committed
SqlPreprocessor: default array mode is items (possible BC break)
1 parent d355d59 commit 3a12769

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

src/Database/SqlPreprocessor.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ public function process(array $params, bool $useParams = false): array
9797

9898
if (($this->counter === 2 && count($params) === 2) || !is_scalar($param)) {
9999
$res[] = $this->formatValue($param, self::MODE_AUTO);
100-
$this->arrayMode = null;
101100

102101
} elseif (is_string($param) && $this->counter > $prev + 1) {
103102
$prev = $this->counter;
@@ -197,7 +196,7 @@ private function formatValue($value, string $mode = null): string
197196
if ($mode && is_array($value)) {
198197
$vx = $kx = [];
199198
if ($mode === self::MODE_AUTO) {
200-
$mode = $this->arrayMode;
199+
$mode = $this->arrayMode ?? self::MODE_LIST;
201200
}
202201

203202
if ($mode === self::MODE_VALUES) { // (key, key, ...) VALUES (value, value, ...)
@@ -226,12 +225,10 @@ private function formatValue($value, string $mode = null): string
226225
}
227226
return '(' . implode(', ', $kx) . ') VALUES (' . implode(', ', $vx) . ')';
228227

229-
} elseif (!$mode || $mode === self::MODE_SET) {
228+
} elseif ($mode === self::MODE_SET) {
230229
foreach ($value as $k => $v) {
231-
if (is_int($k)) { // value, value, ... OR (1, 2), (3, 4)
232-
$vx[] = is_array($v)
233-
? '(' . $this->formatValue($v, self::MODE_LIST) . ')'
234-
: $this->formatValue($v);
230+
if (is_int($k)) { // value, value, ...
231+
$vx[] = $this->formatValue($v);
235232
} elseif (substr($k, -1) === '=') { // key+=value, key-=value, ...
236233
$k2 = $this->delimite(substr($k, 0, -2));
237234
$vx[] = $k2 . '=' . $k2 . ' ' . substr($k, -2, 1) . ' ' . $this->formatValue($v);

tests/Database/SqlPreprocessor.phpt

+3-3
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ test('insert', function () use ($preprocessor) {
326326
[$sql, $params] = $preprocessor->process(['/* comment */ INSERT INTO author',
327327
['name' => 'Catelyn Stark'],
328328
]);
329-
Assert::same(reformat("/* comment */ INSERT INTO author [name]='Catelyn Stark'"), $sql); // autodetection not used
329+
Assert::same(reformat("/* comment */ INSERT INTO author 'Catelyn Stark'"), $sql); // autodetection not used
330330
Assert::same([], $params);
331331
});
332332

@@ -431,10 +431,10 @@ test('update', function () use ($preprocessor) {
431431
Assert::same(reformat('UPDATE author SET [id]=?, [name]=?'), $sql);
432432
Assert::same([12, 'John Doe'], $params);
433433

434-
[$sql, $params] = $preprocessor->process(['UPDATE author SET a=1,',
434+
[$sql, $params] = $preprocessor->process(['UPDATE author SET a=1,', // autodetection not used
435435
['id' => 12, 'name' => 'John Doe'],
436436
]);
437-
Assert::same(reformat('UPDATE author SET a=1, [id]=?, [name]=?'), $sql);
437+
Assert::same(reformat('UPDATE author SET a=1, ?, ?'), $sql);
438438
Assert::same([12, 'John Doe'], $params);
439439
});
440440

0 commit comments

Comments
 (0)