Skip to content

Commit 17bc9b2

Browse files
committed
SqlPreprocessor: default array mode is 'set' [Closes #268]
1 parent 8b03ebf commit 17bc9b2

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/Database/SqlPreprocessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ private function formatValue($value, string $mode = null): string
211211
if ($mode && is_array($value)) {
212212
$vx = $kx = [];
213213
if ($mode === self::MODE_AUTO) {
214-
$mode = $this->arrayMode ?? self::MODE_LIST;
214+
$mode = $this->arrayMode ?? self::MODE_SET;
215215
}
216216

217217
if ($mode === self::MODE_VALUES) { // (key, key, ...) VALUES (value, value, ...)

tests/Database/SqlPreprocessor.phpt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ test('', function () use ($preprocessor) {
6161
});
6262

6363

64+
test('content after WHERE', function () use ($preprocessor) {
65+
[$sql, $params] = $preprocessor->process(['SELECT id FROM author WHERE id = ? AND ?', 12, ['a' => 2]]);
66+
Assert::same(reformat('SELECT id FROM author WHERE id = ? AND [a]=?'), $sql);
67+
Assert::same([12, 2], $params);
68+
});
69+
70+
6471
test('IN', function () use ($preprocessor) {
6572
[$sql, $params] = $preprocessor->process(['SELECT id FROM author WHERE id IN (?)', [10, 11]]);
6673
Assert::same('SELECT id FROM author WHERE id IN (?, ?)', $sql);
@@ -334,7 +341,7 @@ test('insert', function () use ($preprocessor) {
334341
[$sql, $params] = $preprocessor->process(['/* comment */ INSERT INTO author',
335342
['name' => 'Catelyn Stark'],
336343
]);
337-
Assert::same(reformat("/* comment */ INSERT INTO author 'Catelyn Stark'"), $sql); // autodetection not used
344+
Assert::same(reformat("/* comment */ INSERT INTO author [name]='Catelyn Stark'"), $sql); // autodetection not used
338345
Assert::same([], $params);
339346
});
340347

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

442-
[$sql, $params] = $preprocessor->process(['UPDATE author SET a=1,', // autodetection not used
449+
[$sql, $params] = $preprocessor->process(['UPDATE author SET a=1,',
443450
['id' => 12, 'name' => 'John Doe'],
444451
]);
445-
Assert::same(reformat('UPDATE author SET a=1, ?, ?'), $sql);
452+
Assert::same(reformat('UPDATE author SET a=1, [id]=?, [name]=?'), $sql);
446453
Assert::same([12, 'John Doe'], $params);
447454
});
448455

0 commit comments

Comments
 (0)