Skip to content

Commit

Permalink
SqlPreprocessor: default array mode is 'set' [Closes #268]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 12, 2021
1 parent 8b03ebf commit 17bc9b2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Database/SqlPreprocessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ private function formatValue($value, string $mode = null): string
if ($mode && is_array($value)) {
$vx = $kx = [];
if ($mode === self::MODE_AUTO) {
$mode = $this->arrayMode ?? self::MODE_LIST;
$mode = $this->arrayMode ?? self::MODE_SET;
}

if ($mode === self::MODE_VALUES) { // (key, key, ...) VALUES (value, value, ...)
Expand Down
13 changes: 10 additions & 3 deletions tests/Database/SqlPreprocessor.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ test('', function () use ($preprocessor) {
});


test('content after WHERE', function () use ($preprocessor) {
[$sql, $params] = $preprocessor->process(['SELECT id FROM author WHERE id = ? AND ?', 12, ['a' => 2]]);
Assert::same(reformat('SELECT id FROM author WHERE id = ? AND [a]=?'), $sql);
Assert::same([12, 2], $params);
});


test('IN', function () use ($preprocessor) {
[$sql, $params] = $preprocessor->process(['SELECT id FROM author WHERE id IN (?)', [10, 11]]);
Assert::same('SELECT id FROM author WHERE id IN (?, ?)', $sql);
Expand Down Expand Up @@ -334,7 +341,7 @@ test('insert', function () use ($preprocessor) {
[$sql, $params] = $preprocessor->process(['/* comment */ INSERT INTO author',
['name' => 'Catelyn Stark'],
]);
Assert::same(reformat("/* comment */ INSERT INTO author 'Catelyn Stark'"), $sql); // autodetection not used
Assert::same(reformat("/* comment */ INSERT INTO author [name]='Catelyn Stark'"), $sql); // autodetection not used
Assert::same([], $params);
});

Expand Down Expand Up @@ -439,10 +446,10 @@ test('update', function () use ($preprocessor) {
Assert::same(reformat('UPDATE author SET [id]=?, [name]=?'), $sql);
Assert::same([12, 'John Doe'], $params);

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

Expand Down

0 comments on commit 17bc9b2

Please sign in to comment.