Skip to content

Commit 9acf825

Browse files
committed
SqlPreprocessor: default array mode is items (possible BC break)
1 parent 0a203f0 commit 9acf825

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

src/Database/SqlPreprocessor.php

+4-5
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,10 +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) ? '(' . $this->formatValue($v, self::MODE_LIST) . ')' : $this->formatValue($v);
230+
if (is_int($k)) { // value, value, ...
231+
$vx[] = $this->formatValue($v);
233232
} elseif (substr($k, -1) === '=') { // key+=value, key-=value, ...
234233
$k2 = $this->delimite(substr($k, 0, -2));
235234
$vx[] = $k2 . '=' . $k2 . ' ' . substr($k, -2, 1) . ' ' . $this->formatValue($v);

tests/Database/SqlPreprocessor.phpt

+3-3
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ test(function () use ($preprocessor) { // insert
332332
[$sql, $params] = $preprocessor->process(['/* comment */ INSERT INTO author',
333333
['name' => 'Catelyn Stark'],
334334
]);
335-
Assert::same(reformat("/* comment */ INSERT INTO author [name]='Catelyn Stark'"), $sql); // autodetection not used
335+
Assert::same(reformat("/* comment */ INSERT INTO author 'Catelyn Stark'"), $sql); // autodetection not used
336336
Assert::same([], $params);
337337
});
338338

@@ -440,10 +440,10 @@ test(function () use ($preprocessor) { // update
440440
Assert::same([12, 'John Doe'], $params);
441441

442442

443-
[$sql, $params] = $preprocessor->process(['UPDATE author SET a=1,',
443+
[$sql, $params] = $preprocessor->process(['UPDATE author SET a=1,', // autodetection not used
444444
['id' => 12, 'name' => 'John Doe'],
445445
]);
446-
Assert::same(reformat('UPDATE author SET a=1, [id]=?, [name]=?'), $sql);
446+
Assert::same(reformat('UPDATE author SET a=1, ?, ?'), $sql);
447447
Assert::same([12, 'John Doe'], $params);
448448
});
449449

0 commit comments

Comments
 (0)