diff --git a/src/Database/SqlPreprocessor.php b/src/Database/SqlPreprocessor.php index dcaad0ca1..000f29454 100644 --- a/src/Database/SqlPreprocessor.php +++ b/src/Database/SqlPreprocessor.php @@ -97,7 +97,6 @@ public function process(array $params, bool $useParams = false): array if (($this->counter === 2 && count($params) === 2) || !is_scalar($param)) { $res[] = $this->formatValue($param, self::MODE_AUTO); - $this->arrayMode = null; } elseif (is_string($param) && $this->counter > $prev + 1) { $prev = $this->counter; @@ -197,7 +196,7 @@ private function formatValue($value, string $mode = null): string if ($mode && is_array($value)) { $vx = $kx = []; if ($mode === self::MODE_AUTO) { - $mode = $this->arrayMode; + $mode = $this->arrayMode ?? self::MODE_LIST; } if ($mode === self::MODE_VALUES) { // (key, key, ...) VALUES (value, value, ...) @@ -226,10 +225,10 @@ private function formatValue($value, string $mode = null): string } return '(' . implode(', ', $kx) . ') VALUES (' . implode(', ', $vx) . ')'; - } elseif (!$mode || $mode === self::MODE_SET) { + } elseif ($mode === self::MODE_SET) { foreach ($value as $k => $v) { - if (is_int($k)) { // value, value, ... OR (1, 2), (3, 4) - $vx[] = is_array($v) ? '(' . $this->formatValue($v, self::MODE_LIST) . ')' : $this->formatValue($v); + if (is_int($k)) { // value, value, ... + $vx[] = $this->formatValue($v); } elseif (substr($k, -1) === '=') { // key+=value, key-=value, ... $k2 = $this->delimite(substr($k, 0, -2)); $vx[] = $k2 . '=' . $k2 . ' ' . substr($k, -2, 1) . ' ' . $this->formatValue($v); diff --git a/tests/Database/SqlPreprocessor.phpt b/tests/Database/SqlPreprocessor.phpt index 02fe938e8..db3385e4f 100644 --- a/tests/Database/SqlPreprocessor.phpt +++ b/tests/Database/SqlPreprocessor.phpt @@ -332,7 +332,7 @@ test(function () use ($preprocessor) { // insert [$sql, $params] = $preprocessor->process(['/* comment */ INSERT INTO author', ['name' => 'Catelyn Stark'], ]); - Assert::same(reformat("/* comment */ INSERT INTO author [name]='Catelyn Stark'"), $sql); // autodetection not used + Assert::same(reformat("/* comment */ INSERT INTO author 'Catelyn Stark'"), $sql); // autodetection not used Assert::same([], $params); }); @@ -440,10 +440,10 @@ test(function () use ($preprocessor) { // update Assert::same([12, 'John Doe'], $params); - [$sql, $params] = $preprocessor->process(['UPDATE author SET a=1,', + [$sql, $params] = $preprocessor->process(['UPDATE author SET a=1,', // autodetection not used ['id' => 12, 'name' => 'John Doe'], ]); - Assert::same(reformat('UPDATE author SET a=1, [id]=?, [name]=?'), $sql); + Assert::same(reformat('UPDATE author SET a=1, ?, ?'), $sql); Assert::same([12, 'John Doe'], $params); });