Skip to content

Commit 7f03090

Browse files
committed
SqlPreprocessor: added braces around fixed bugs in
1 parent 56293fd commit 7f03090

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/Database/SqlPreprocessor.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,9 @@ private function formatWhere(array $items, string $mode): string
341341
}
342342
}
343343

344-
return '(' . implode(') ' . strtoupper($mode) . ' (', $res ?: [$default]) . ')';
344+
return count($res) > 1
345+
? '((' . implode(') ' . strtoupper($mode) . ' (', $res) . '))'
346+
: '(' . ($res[0] ?? $default) . ')';
345347
}
346348

347349

tests/Database/SqlPreprocessor.phpt

+11-11
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,15 @@ test('Auto-detects operator in WHERE conditions', function () use ($preprocessor
131131
'col_arr' => [1, 2],
132132
]]);
133133

134-
Assert::same(reformat('SELECT id FROM tbl WHERE ([col_null] IS NULL) AND ([x].[col_val] = ?) AND ([col_arr] IN (?, ?))'), $sql);
134+
Assert::same(reformat('SELECT id FROM tbl WHERE (([col_null] IS NULL) AND ([x].[col_val] = ?) AND ([col_arr] IN (?, ?)))'), $sql);
135135

136136
[$sql, $params] = $preprocessor->process(['SELECT id FROM tbl WHERE', [
137137
'col_null NOT' => null,
138138
'x.col_val NOT' => 'a',
139139
'col_arr NOT' => [1, 2],
140140
]]);
141141

142-
Assert::same(reformat('SELECT id FROM tbl WHERE ([col_null] IS NOT NULL) AND ([x].[col_val] != ?) AND ([col_arr] NOT IN (?, ?))'), $sql);
142+
Assert::same(reformat('SELECT id FROM tbl WHERE (([col_null] IS NOT NULL) AND ([x].[col_val] != ?) AND ([col_arr] NOT IN (?, ?)))'), $sql);
143143
});
144144

145145

@@ -153,7 +153,7 @@ test('Supports explicit operators in WHERE conditions', function () use ($prepro
153153
'col_arr =' => [1, 2],
154154
]]);
155155

156-
Assert::same(reformat('SELECT id FROM tbl WHERE ([col_is] = ?) AND ([col_not] <> ?) AND ([col_like] LIKE ?) AND ([col_like] NOT LIKE ?) AND ([col_null] = NULL) AND ([col_arr] = (?, ?))'), $sql);
156+
Assert::same(reformat('SELECT id FROM tbl WHERE (([col_is] = ?) AND ([col_not] <> ?) AND ([col_like] LIKE ?) AND ([col_like] NOT LIKE ?) AND ([col_null] = NULL) AND ([col_arr] = (?, ?)))'), $sql);
157157
});
158158

159159

@@ -230,14 +230,14 @@ test('WHERE conditions with indexed items', function () use ($preprocessor) {
230230
'foo',
231231
'bar',
232232
]]);
233-
Assert::same(reformat('SELECT id FROM tbl WHERE (?) AND (?)'), $sql);
233+
Assert::same(reformat('SELECT id FROM tbl WHERE ((?) AND (?))'), $sql);
234234
Assert::same(['foo', 'bar'], $params); // not useful
235235
});
236236

237237

238238
test('Combines WHERE conditions with array & direct SQL', function () use ($preprocessor) {
239239
[$sql, $params] = $preprocessor->process(['SELECT id FROM tbl WHERE id=?', 10, 'AND ?and', ['c1' => null, 'c2' => 2], 'AND ?or', ['c3' => null, 'c4' => 4]]);
240-
Assert::same(reformat('SELECT id FROM tbl WHERE id=? AND ([c1] IS NULL) AND ([c2] = ?) AND ([c3] IS NULL) OR ([c4] = ?)'), $sql); // is not properly clamped
240+
Assert::same(reformat('SELECT id FROM tbl WHERE id=? AND (([c1] IS NULL) AND ([c2] = ?)) AND (([c3] IS NULL) OR ([c4] = ?))'), $sql);
241241
Assert::same([10, 2, 4], $params);
242242
});
243243

@@ -284,7 +284,7 @@ test('WHERE conditions with ORDER BY', function () use ($preprocessor) {
284284
'name' => false,
285285
]]);
286286

287-
Assert::same(reformat('SELECT id FROM author WHERE ([id] = ?) AND ([web] = ?) ORDER BY [name] DESC'), $sql);
287+
Assert::same(reformat('SELECT id FROM author WHERE (([id] = ?) AND ([web] = ?)) ORDER BY [name] DESC'), $sql);
288288
Assert::same([1, 'web'], $params);
289289
});
290290

@@ -361,7 +361,7 @@ test('WHERE conditions with SQL literals', function () use ($preprocessor) {
361361
'web' => new SqlLiteral('NOW()'),
362362
]]);
363363

364-
Assert::same(reformat('SELECT id FROM author WHERE ([id] IS NULL) AND ([born] IN (?, ?, 3+1)) AND ([web] = NOW())'), $sql);
364+
Assert::same(reformat('SELECT id FROM author WHERE (([id] IS NULL) AND ([born] IN (?, ?, 3+1)) AND ([web] = NOW()))'), $sql);
365365
Assert::same([1, 2], $params);
366366
});
367367

@@ -380,7 +380,7 @@ test('AND operator in WHERE conditions', function () use ($preprocessor) {
380380
'born' => [1, 2],
381381
]]);
382382

383-
Assert::same(reformat('SELECT id FROM author WHERE ([id] IS NULL) AND ([born] IN (?, ?))'), $sql);
383+
Assert::same(reformat('SELECT id FROM author WHERE (([id] IS NULL) AND ([born] IN (?, ?)))'), $sql);
384384
Assert::same([1, 2], $params);
385385
});
386386

@@ -391,7 +391,7 @@ test('OR operator in WHERE conditions', function () use ($preprocessor) {
391391
'born' => [1, 2],
392392
]]);
393393

394-
Assert::same(reformat('SELECT id FROM author WHERE ([id] IS NULL) OR ([born] IN (?, ?))'), $sql);
394+
Assert::same(reformat('SELECT id FROM author WHERE (([id] IS NULL) OR ([born] IN (?, ?)))'), $sql);
395395
Assert::same([1, 2], $params);
396396
});
397397

@@ -616,7 +616,7 @@ test('nested SQL literals with special placeholders', function () use ($preproce
616616
new SqlLiteral('max > ?', [10]),
617617
new SqlLiteral('min < ?', [20]),
618618
]]);
619-
Assert::same(reformat('SELECT id FROM author WHERE (max > ?) OR (min < ?)'), $sql);
619+
Assert::same(reformat('SELECT id FROM author WHERE ((max > ?) OR (min < ?))'), $sql);
620620
Assert::same([10, 20], $params);
621621
});
622622

@@ -626,7 +626,7 @@ test('complex nested conditions with SQL literals', function () use ($preprocess
626626
new SqlLiteral('?and', [['a' => 1, 'b' => 2]]),
627627
new SqlLiteral('?and', [['c' => 3, 'd' => 4]]),
628628
]])]);
629-
Assert::same(reformat('SELECT id FROM author WHERE (([a] = ?) AND ([b] = ?)) OR (([c] = ?) AND ([d] = ?))'), $sql);
629+
Assert::same(reformat('SELECT id FROM author WHERE (((([a] = ?) AND ([b] = ?))) OR ((([c] = ?) AND ([d] = ?))))'), $sql);
630630
Assert::same([1, 2, 3, 4], $params);
631631
});
632632

0 commit comments

Comments
 (0)