Commit bd461a2
authored
## Summary
Adds support for single-table `DELETE` statements with `ORDER BY` and/or
`LIMIT` clauses, which SQLite rejects as a syntax error unless it was
compiled with `SQLITE_ENABLE_UPDATE_DELETE_LIMIT` (not the case for
standard builds, including the one bundled with WordPress Playground).
The fix mirrors the existing `UPDATE` LIMIT rewrite: when `ORDER BY` or
`LIMIT` is present, the statement is translated to drive deletion off a
`rowid` subquery.
```sql
-- MySQL
DELETE FROM t WHERE c = 1 ORDER BY c LIMIT 10
-- Translated SQLite
DELETE FROM `t` WHERE rowid IN (
SELECT rowid FROM `t` WHERE `c` = 1 ORDER BY `c` ASC LIMIT 10
)
```
The subquery forwards `tableRef`, `tableAlias`, `whereClause`,
`orderClause`, and `simpleLimitClause`, so queries that reference an
aliased table keep the alias in scope inside the subquery (e.g. `DELETE
FROM t AS a WHERE a.c = 1 LIMIT 1` works).
## Scope
- **In scope:** single-table `DELETE` with `ORDER BY`, `LIMIT`, or both
— with or without `WHERE` and alias.
- **Out of scope:** multi-table `DELETE` — MySQL's grammar disallows
`ORDER BY`/`LIMIT` there, so there's nothing to add.
## Tests
- Translation tests covering plain `LIMIT`, `WHERE` + `LIMIT`, `ORDER
BY` + `LIMIT`, `WHERE` + `ORDER BY` + `LIMIT`, and alias + `WHERE` +
`LIMIT`.
- Runtime tests that insert rows, run `DELETE ... LIMIT` through the
driver, and assert on the surviving rows — including a case that matches
multiple rows to verify `LIMIT` is actually enforced, and an alias case.
Fixes #100
1 parent 3a3baf7 commit bd461a2
3 files changed
Lines changed: 120 additions & 0 deletions
File tree
- packages/mysql-on-sqlite
- src/sqlite
- tests
Lines changed: 32 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2308 | 2308 | | |
2309 | 2309 | | |
2310 | 2310 | | |
| 2311 | + | |
| 2312 | + | |
| 2313 | + | |
| 2314 | + | |
| 2315 | + | |
| 2316 | + | |
| 2317 | + | |
| 2318 | + | |
| 2319 | + | |
| 2320 | + | |
| 2321 | + | |
| 2322 | + | |
| 2323 | + | |
| 2324 | + | |
| 2325 | + | |
| 2326 | + | |
| 2327 | + | |
| 2328 | + | |
| 2329 | + | |
| 2330 | + | |
| 2331 | + | |
| 2332 | + | |
| 2333 | + | |
| 2334 | + | |
| 2335 | + | |
| 2336 | + | |
| 2337 | + | |
| 2338 | + | |
| 2339 | + | |
| 2340 | + | |
| 2341 | + | |
| 2342 | + | |
2311 | 2343 | | |
2312 | 2344 | | |
2313 | 2345 | | |
| |||
Lines changed: 58 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
222 | 222 | | |
223 | 223 | | |
224 | 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 | + | |
225 | 283 | | |
226 | 284 | | |
227 | 285 | | |
| |||
Lines changed: 30 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
382 | 382 | | |
383 | 383 | | |
384 | 384 | | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
385 | 415 | | |
386 | 416 | | |
387 | 417 | | |
| |||
0 commit comments