Skip to content

Commit 2f5fb43

Browse files
committed
SqlPreprocessor: IN() inserts parameters directly and bypasses binding
The reason is the limits on the number of bound parameters
1 parent f449746 commit 2f5fb43

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

src/Database/SqlPreprocessor.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,11 @@ private function formatList(array $values): string
206206
{
207207
$res = [];
208208
foreach ($values as $v) {
209-
$res[] = is_array($v)
210-
? '(' . $this->formatList($v) . ')'
211-
: $this->formatValue($v);
209+
$res[] = match (true) {
210+
is_array($v) => '(' . $this->formatList($v) . ')',
211+
is_int($v) => (string) $v,
212+
default => $this->formatValue($v),
213+
};
212214
}
213215

214216
return implode(', ', $res);

tests/Database/SqlPreprocessor.phpt

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,28 +70,28 @@ test('Processes array conditions after WHERE clause', function () use ($preproce
7070

7171
test('Handles IN operator with array values', function () use ($preprocessor) {
7272
[$sql, $params] = $preprocessor->process(['SELECT id FROM author WHERE id IN (?)', [10, 11]]);
73-
Assert::same('SELECT id FROM author WHERE id IN (?, ?)', $sql);
74-
Assert::same([10, 11], $params);
73+
Assert::same('SELECT id FROM author WHERE id IN (10, 11)', $sql);
74+
Assert::same([], $params);
7575

7676
[$sql, $params] = $preprocessor->process(['SELECT id FROM author WHERE (id, name) IN (?)', [[10, 'a'], [11, 'b']]]);
77-
Assert::same('SELECT id FROM author WHERE (id, name) IN ((?, ?), (?, ?))', $sql);
78-
Assert::same([10, 'a', 11, 'b'], $params);
77+
Assert::same('SELECT id FROM author WHERE (id, name) IN ((10, ?), (11, ?))', $sql);
78+
Assert::same(['a', 'b'], $params);
7979

8080
[$sql, $params] = $preprocessor->process(['SELECT * FROM table WHERE ? AND id IN (?) AND ?', ['a' => 111], [3, 4], ['b' => 222]]);
81-
Assert::same(reformat('SELECT * FROM table WHERE ([a] = ?) AND id IN (?, ?) AND ([b] = ?)'), $sql);
82-
Assert::same([111, 3, 4, 222], $params);
81+
Assert::same(reformat('SELECT * FROM table WHERE ([a] = ?) AND id IN (3, 4) AND ([b] = ?)'), $sql);
82+
Assert::same([111, 222], $params);
8383

8484
[$sql, $params] = $preprocessor->process(['SELECT id FROM author WHERE id IN ?', [10, 11]]); // without ()
85-
Assert::same('SELECT id FROM author WHERE id IN (?, ?)', $sql);
86-
Assert::same([10, 11], $params);
85+
Assert::same('SELECT id FROM author WHERE id IN (10, 11)', $sql);
86+
Assert::same([], $params);
8787

8888
[$sql, $params] = $preprocessor->process(['SELECT id FROM author WHERE id IN (?)', 10]); // single item in ()
8989
Assert::same('SELECT id FROM author WHERE id IN (?)', $sql);
9090
Assert::same([10], $params);
9191

9292
[$sql, $params] = $preprocessor->process(['SELECT id FROM author WHERE id IN (?)', [10, 11]]); // array in ()
93-
Assert::same('SELECT id FROM author WHERE id IN (?, ?)', $sql);
94-
Assert::same([10, 11], $params);
93+
Assert::same('SELECT id FROM author WHERE id IN (10, 11)', $sql);
94+
Assert::same([], $params);
9595
});
9696

9797

@@ -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 (1, 2)))'), $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 (1, 2)))'), $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] = (1, 2)))'), $sql);
157157
});
158158

159159

@@ -256,8 +256,8 @@ test('multi-value IN conditions (tuples)', function () use ($preprocessor) {
256256
[5, 6],
257257
]]);
258258

259-
Assert::same(reformat('SELECT * FROM book_tag WHERE (book_id, tag_id) IN ((?, ?), (?, ?), (?, ?))'), $sql);
260-
Assert::same([1, 2, 3, 4, 5, 6], $params);
259+
Assert::same(reformat('SELECT * FROM book_tag WHERE (book_id, tag_id) IN ((1, 2), (3, 4), (5, 6))'), $sql);
260+
Assert::same([], $params);
261261
});
262262

263263

@@ -368,8 +368,8 @@ test('WHERE conditions with SQL literals', function () use ($preprocessor) {
368368
'web' => new SqlLiteral('NOW()'),
369369
]]);
370370

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

375375

@@ -387,8 +387,8 @@ test('AND operator in WHERE conditions', function () use ($preprocessor) {
387387
'born' => [1, 2],
388388
]]);
389389

390-
Assert::same(reformat('SELECT id FROM author WHERE (([id] IS NULL) AND ([born] IN (?, ?)))'), $sql);
391-
Assert::same([1, 2], $params);
390+
Assert::same(reformat('SELECT id FROM author WHERE (([id] IS NULL) AND ([born] IN (1, 2)))'), $sql);
391+
Assert::same([], $params);
392392
});
393393

394394

@@ -398,8 +398,8 @@ test('OR operator in WHERE conditions', function () use ($preprocessor) {
398398
'born' => [1, 2],
399399
]]);
400400

401-
Assert::same(reformat('SELECT id FROM author WHERE (([id] IS NULL) OR ([born] IN (?, ?)))'), $sql);
402-
Assert::same([1, 2], $params);
401+
Assert::same(reformat('SELECT id FROM author WHERE (([id] IS NULL) OR ([born] IN (1, 2)))'), $sql);
402+
Assert::same([], $params);
403403
});
404404

405405

0 commit comments

Comments
 (0)