Skip to content

Commit

Permalink
SqlPreprocessor: default array mode is items (possible BC break)
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 6, 2020
1 parent 9316672 commit 53fceda
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
9 changes: 4 additions & 5 deletions src/Database/SqlPreprocessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, ...)
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions tests/Database/SqlPreprocessor.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ test('insert', function () use ($preprocessor) {
[$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);
});

Expand Down Expand Up @@ -440,10 +440,10 @@ test('update', function () use ($preprocessor) {
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);
});

Expand Down

0 comments on commit 53fceda

Please sign in to comment.