-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathsyntax-error-in-prepared-statement.php
More file actions
331 lines (279 loc) · 12.2 KB
/
syntax-error-in-prepared-statement.php
File metadata and controls
331 lines (279 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
namespace SyntaxErrorInPreparedStatementMethodRuleTest;
use staabm\PHPStanDba\Tests\Fixture\Connection;
use staabm\PHPStanDba\Tests\Fixture\PreparedStatement;
class Foo
{
public function syntaxError(Connection $connection)
{
$connection->preparedQuery('SELECT email adaid WHERE gesperrt freigabe1u1 FROM ada', []);
}
public function syntaxErrorInConstruct()
{
$stmt = new PreparedStatement('SELECT email adaid WHERE gesperrt freigabe1u1 FROM ada', []);
}
public function syntaxErrorOnKnownParamType(Connection $connection, int $i, bool $bool)
{
$connection->preparedQuery('
SELECT email adaid
WHERE gesperrt = ? AND email LIKE ?
FROM ada
LIMIT 1
', [$i, '%@example.com']);
$connection->preparedQuery('
SELECT email adaid
WHERE gesperrt = ? AND email LIKE ?
FROM ada
LIMIT 1
', [$bool, '%@example.com']);
}
public function noErrorOnMixedParams(Connection $connection, $unknownType)
{
$connection->preparedQuery('
SELECT email, adaid
FROM ada
WHERE gesperrt = ? AND email LIKE ?
LIMIT 1
', [1, $unknownType]);
}
public function noErrorOnPlaceholderInLimit(Connection $connection, int $limit)
{
$connection->preparedQuery('
SELECT email, adaid
FROM ada
WHERE gesperrt = ?
LIMIT ?
', [1, $limit]);
$connection->preparedQuery('
SELECT email, adaid
FROM ada
WHERE gesperrt = :gesperrt
LIMIT :limit
', [':gesperrt' => 1, ':limit' => $limit]);
}
public function noErrorOnPlaceholderInOffsetAndLimit(Connection $connection, int $offset, int $limit)
{
$connection->preparedQuery('
SELECT email, adaid
FROM ada
WHERE gesperrt = ?
LIMIT ?, ?
', [1, $offset, $limit]);
$connection->preparedQuery('
SELECT email, adaid
FROM ada
WHERE gesperrt = :gesperrt
LIMIT :offset, :limit
', [':gesperrt' => 1, ':offset' => $offset, ':limit' => $limit]);
}
public function preparedParams(Connection $connection)
{
$connection->preparedQuery('SELECT email, adaid FROM ada WHERE gesperrt = ?', [1]);
$connection->preparedQuery('
SELECT email, adaid
FROM ada
WHERE gesperrt = ? AND email LIKE ?
LIMIT 1
', [1, '%@example%']);
}
public function preparedNamedParams(Connection $connection)
{
$connection->preparedQuery('SELECT email, adaid FROM ada WHERE gesperrt = :gesperrt', ['gesperrt' => 1]);
}
public function camelCase(Connection $connection)
{
$connection->preparedQuery('SELECT email, adaid FROM ada WHERE gesperrt = :myGesperrt', ['myGesperrt' => 1]);
}
public function syntaxErrorInDoctrineDbal(\Doctrine\DBAL\Connection $conn, $types, \Doctrine\DBAL\Cache\QueryCacheProfile $qcp)
{
$conn->executeQuery('SELECT email adaid WHERE gesperrt freigabe1u1 FROM ada', []);
$conn->executeCacheQuery('SELECT email adaid WHERE gesperrt freigabe1u1 FROM ada', [], $types, $qcp);
$conn->executeStatement('SELECT email adaid WHERE gesperrt freigabe1u1 FROM ada', []);
}
public function conditionalSyntaxError(Connection $connection)
{
$query = 'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada';
if (rand(0, 1)) {
// valid condition
$query .= ' WHERE gesperrt=?';
} else {
// unknown column
$query .= ' WHERE asdsa=?';
}
$connection->preparedQuery($query, [1]);
}
public function placeholderValidation(Connection $connection)
{
$query = 'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada';
if (rand(0, 1)) {
// valid condition
$query .= ' WHERE gesperrt=?';
} else {
// unknown column
$query .= ' WHERE asdsa=?';
}
$connection->preparedQuery($query, [':gesperrt' => 1]);
}
public function samePlaceholderMultipleTimes(Connection $connection)
{
$query = 'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada
WHERE (gesperrt=:gesperrt AND freigabe1u1=1) OR (gesperrt=:gesperrt AND freigabe1u1=0)';
$connection->preparedQuery($query, [':gesperrt' => 1]);
}
public function noErrorOnTraillingSemicolon(Connection $connection)
{
$query = 'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada;';
$connection->preparedQuery($query, []);
}
public function noErrorOnPlaceholderInData(Connection $connection)
{
$query = "SELECT adaid FROM ada WHERE email LIKE 'hello?%'";
$connection->preparedQuery($query, []);
$query = "SELECT adaid FROM ada WHERE email LIKE '%questions ?%'";
$connection->preparedQuery($query, []);
$query = 'SELECT adaid FROM ada WHERE email LIKE ":gesperrt%"';
$connection->preparedQuery($query, []);
$query = "SELECT adaid FROM ada WHERE email LIKE ':gesperrt%'";
$connection->preparedQuery($query, []);
$query = "SELECT adaid FROM ada WHERE email LIKE 'some strange string - :gesperrt it is'";
$connection->preparedQuery($query, []);
}
public function arrayParam(Connection $connection)
{
$query = 'SELECT email FROM ada WHERE adaid IN (:adaids)';
$connection->preparedQuery($query, ['adaids' => [1, 2, 3]]);
}
public function noErrorInBug156(Connection $connection, array $idsToUpdate, string $time)
{
$query = 'UPDATE package SET indexedAt=:indexed WHERE id IN (:ids) AND (indexedAt IS NULL OR indexedAt <= crawledAt)';
$connection->preparedQuery($query, [
'ids' => $idsToUpdate,
'indexed' => $time,
]);
}
public function noErrorInBug94(Connection $connection)
{
// XXX with proper sql parsing, we should better detect the placeholders and therefore could validate this query
$sql = "
INSERT IGNORE INTO `s_articles_supplier` (`id`, `name`, `img`, `link`, `changed`) VALUES (:supplierId, 'TestSupplier', '', '', '2019-12-09 10:42:10');
INSERT INTO `s_articles` (`id`, `supplierID`, `name`, `datum`, `taxID`, `changetime`, `pricegroupID`, `pricegroupActive`, `filtergroupID`, `laststock`, `crossbundlelook`, `notification`, `template`, `mode`) VALUES
(:productId, :supplierId, 'SwagTest', '2020-03-20', '1', '2020-03-20 10:42:10', NULL, '0', NULL, '0', '0', '0', '', '0');
INSERT IGNORE INTO `s_order` (`id`, `ordernumber`, `userID`, `invoice_amount`, `invoice_amount_net`, `invoice_shipping`, `invoice_shipping_net`, `ordertime`, `status`, `cleared`, `paymentID`, `transactionID`, `comment`, `customercomment`, `internalcomment`, `net`, `taxfree`, `partnerID`, `temporaryID`, `referer`, `cleareddate`, `trackingcode`, `language`, `dispatchID`, `currency`, `currencyFactor`, `subshopID`, `remote_addr`) VALUES
(:orderId, '29996', 1, 126.82, 106.57, 3.9, 3.28, '2013-07-10 08:17:20', 0, 17, 5, '', '', '', '', 0, 0, '', '', '', NULL, '', '1', 9, 'EUR', 1, 1, '172.16.10.71');
INSERT IGNORE INTO `s_order_details` (`id`, `orderID`, `ordernumber`, `articleID`, `articleordernumber`, `price`, `quantity`, `name`, `status`, `shipped`, `shippedgroup`, `releasedate`, `modus`, `esdarticle`, `taxID`, `tax_rate`, `config`) VALUES
(15315352, :orderId, '20003', :productId, 'SW10178', 19.95, 1, 'Strandtuch Ibiza', 0, 0, 0, '0000-00-00', 0, 0, 1, 19, ''),
(15315353, :orderId, '20003', 177, 'SW10177', 34.99, 1, 'Strandtuch Stripes für Kinder', 0, 0, 0, '0000-00-00', 0, 0, 1, 19, ''),
(15315354, :orderId, '20003', 173, 'SW10173', 39.99, 1, 'Strandkleid Flower Power', 0, 0, 0, '0000-00-00', 0, 0, 1, 19, ''),
(15315355, :orderId, '20003', 160, 'SW10160.1', 29.99, 1, 'Sommer Sandale Ocean Blue 36', 0, 0, 0, '0000-00-00', 0, 0, 1, 19, ''),
(15315356, :orderId, '20003', 0, 'SHIPPINGDISCOUNT', -2, 1, 'Warenkorbrabatt', 0, 0, 0, '0000-00-00', 4, 0, 0, 19, '');
";
$supplierId = '81729';
$productId = 91829002;
$orderId = 15315351;
$connection->preparedQuery($sql, ['orderId' => $orderId, 'productId' => $productId, 'supplierId' => $supplierId]);
}
public function noErrorOnBug175(Connection $connection, int $limit, int $offset)
{
$connection->preparedQuery('
SELECT email, adaid
FROM ada
WHERE gesperrt = ?
LIMIT ?
OFFSET '.$offset.'
', [1, $limit]);
$connection->preparedQuery('
SELECT email, adaid
FROM ada
WHERE gesperrt = ?
LIMIT ?
OFFSET '.((int) $offset).'
', [1, $limit]);
}
public function noErrorOnOffsetAfterLimit(Connection $connection, int $limit, int $offset)
{
$connection->preparedQuery('
SELECT email, adaid
FROM ada
WHERE gesperrt = ?
LIMIT ?
OFFSET ?
', [1, $limit, $offset]);
$connection->preparedQuery('
SELECT email, adaid
FROM ada
WHERE gesperrt = :gesperrt
LIMIT :limit
OFFSET :offset
', [':gesperrt' => 1, ':limit' => $limit, ':offset' => $offset]);
}
public function noErrorOnLockedRead(Connection $connection, int $limit, int $offset)
{
$connection->preparedQuery('
SELECT email, adaid
FROM ada
WHERE gesperrt = ?
FOR UPDATE
', [1]);
$connection->preparedQuery('
SELECT email, adaid
FROM ada
WHERE gesperrt = ?
LIMIT ?
FOR UPDATE
', [1, $limit]);
$connection->preparedQuery('
SELECT email, adaid
FROM ada
WHERE gesperrt = ?
LIMIT ?
OFFSET ?
FOR UPDATE
', [1, $limit, $offset]);
$connection->preparedQuery('
SELECT email, adaid
FROM ada
WHERE gesperrt = :gesperrt
LIMIT :limit
OFFSET :offset
FOR SHARE
', [':gesperrt' => 1, ':limit' => $limit, ':offset' => $offset]);
}
public function noErrorInBug174(Connection $connection, string $name, ?int $gesperrt = null, ?int $adaid = null)
{
$sql = 'SELECT adaid FROM ada WHERE email = :name';
$args = ['name' => $name];
if (null !== $gesperrt) {
$sql .= ' AND gesperrt = :gesperrt';
$args['gesperrt'] = $gesperrt;
}
$connection->preparedQuery($sql, $args);
}
public function conditionalNumberOfPlaceholders(Connection $connection, string $name, ?int $gesperrt = null, ?int $adaid = null)
{
$sql = 'SELECT adaid FROM ada WHERE email = :name';
$args = [];
if (null !== $gesperrt) {
$sql .= ' AND gesperrt = :gesperrt';
$args['gesperrt'] = $gesperrt;
}
$connection->preparedQuery($sql, $args);
}
public function noErrorOnTraillingSemicolonAndWhitespace(Connection $connection)
{
$query = 'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada; ';
$connection->preparedQuery($query, []);
}
public function errorOnQueryWithoutArgs(\Doctrine\DBAL\Connection $connection)
{
$query = 'SELECT email adaid gesperrt freigabe1u1 FROM ada';
$connection->executeQuery($query);
}
public function preparedNamedParamsSubstitution(Connection $connection)
{
$connection->preparedQuery('SELECT email FROM ada WHERE email = :param OR email = :parameter', ['param' => 'abc', 'parameter' => 'def']);
}
public function unknownConstant(Connection $connection)
{
$connection->preparedQuery('SELECT email FROM ada WHERE email = :param OR email = :parameter', ['param' => CONSTANT_DOES_NOT_EXIST, 'parameter' => 'def']);
}
}