Repository navigation
Commit e88a817
authored
Fix
## Summary
On a fresh connection, rolling back the first transaction breaks the
connection:
```sql
START TRANSACTION;
ROLLBACK;
-- SQLSTATE[42S02]: Base table or view not found: 1146
-- Table '_wp_sqlite_empty_result' doesn't exist
```
Statements with an empty result set (`SET`, `CREATE TABLE`, …) used a
no-op `INSERT` into a TEMP table that was created on first use. When the
first use happened inside a transaction, the rollback dropped the table,
but the driver still assumed it existed.
This is an **alternative to WordPress#535**. Instead of creating the table again,
it **removes the need for the table**.
## Fix
PDO needs a statement object even when there is nothing to return. This
PR uses an **unknown pragma** for it. SQLite ignores unknown pragmas, so
the statement has no columns and no rows. It **needs no table and takes
no locks**, so a rollback has nothing to drop, and statements like `SET`
still work while another connection writes.
## Row counts
PDO SQLite's `rowCount()` reports affected rows only for `INSERT`,
`UPDATE`, and `DELETE`. For other statements, it can report the row
count of the **last write** on the connection, including the driver's
internal writes. The old no-op `INSERT` reset that count after every
empty result set, which hid most of these cases. The pragma doesn't
reset it.
The driver now **reports `0` affected rows for all statements other than
`INSERT`, `REPLACE`, `UPDATE`, and `DELETE`**. This also fixes cases
that were already broken on `trunk`:
- **`SELECT` or `SHOW` without rows** right after a write reported the
written rows.
- **`DROP VIEW` or `DROP TRIGGER`** right after a write reported the
written rows.
- **`TRUNCATE TABLE`** on a table with `AUTO_INCREMENT` reported `1`. It
now reports `0`, as in MySQL.
## Why not keep the table?
The other options are worse than the pragma:
- **Create the table again and retry (WordPress#535).** It keeps a flag that can
go stale, and depends on SQLite's "no such table" error message.
- **Run `CREATE TEMP TABLE IF NOT EXISTS` before every `INSERT`.** It
costs about 0.5 µs per empty result, and it doesn't fix the row counts.
- **Use another no-op statement.** PDO can't execute a statement with
only a comment or `;`. `DROP … IF EXISTS` for a missing table can need a
lock to read the schema.
## Tests
The new tests cover the bug, the row counts, and the empty result
statement:
- **Two rollback regression tests**, adapted from WordPress#535. They fail on
`trunk` with the error above.
- **Two row count tests** for statements that don't write. They fail on
`trunk`.
- **`test_empty_result_statement`** checks the PDO API of the empty
result statement, and that holding an unread one doesn't block `COMMIT`
or `DROP TABLE`.
Supersedes WordPress#535.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Bug Fixes**
* Non-data-changing statements, including `TRUNCATE`, now report zero
affected rows rather than carrying over a previous write count. INSERT,
REPLACE, UPDATE, and DELETE continue to report their affected-row
counts.
* Empty-result statements now return consistent results and counts, and
no longer block transaction commits or schema changes.
* **Tests**
* Added coverage for empty results, affected-row counts, and rolling
back the first transaction on a connection.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->ROLLBACK as the first transaction of a connection (WordPress#537)1 parent 4d94f39 commit e88a817
2 files changed
Lines changed: 143 additions & 44 deletions
File tree
- packages/mysql-on-sqlite
- src/sqlite
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | 85 | | |
92 | 86 | | |
93 | 87 | | |
| |||
678 | 672 | | |
679 | 673 | | |
680 | 674 | | |
681 | | - | |
682 | | - | |
683 | | - | |
684 | | - | |
685 | | - | |
686 | | - | |
687 | | - | |
688 | | - | |
689 | 675 | | |
690 | 676 | | |
691 | 677 | | |
| |||
1145 | 1131 | | |
1146 | 1132 | | |
1147 | 1133 | | |
1148 | | - | |
1149 | | - | |
| 1134 | + | |
| 1135 | + | |
1150 | 1136 | | |
1151 | 1137 | | |
1152 | 1138 | | |
| |||
1180 | 1166 | | |
1181 | 1167 | | |
1182 | 1168 | | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
1183 | 1183 | | |
1184 | 1184 | | |
1185 | 1185 | | |
1186 | 1186 | | |
1187 | | - | |
| 1187 | + | |
1188 | 1188 | | |
1189 | 1189 | | |
1190 | 1190 | | |
| |||
7738 | 7738 | | |
7739 | 7739 | | |
7740 | 7740 | | |
| 7741 | + | |
| 7742 | + | |
| 7743 | + | |
| 7744 | + | |
| 7745 | + | |
| 7746 | + | |
7741 | 7747 | | |
7742 | 7748 | | |
7743 | 7749 | | |
7744 | | - | |
7745 | | - | |
| 7750 | + | |
| 7751 | + | |
| 7752 | + | |
| 7753 | + | |
7746 | 7754 | | |
7747 | 7755 | | |
7748 | | - | |
7749 | | - | |
7750 | | - | |
7751 | | - | |
7752 | | - | |
7753 | | - | |
7754 | | - | |
7755 | | - | |
7756 | | - | |
7757 | | - | |
7758 | | - | |
7759 | | - | |
7760 | | - | |
7761 | | - | |
7762 | | - | |
7763 | | - | |
7764 | | - | |
7765 | | - | |
7766 | | - | |
7767 | | - | |
7768 | | - | |
7769 | | - | |
7770 | | - | |
7771 | | - | |
7772 | | - | |
| 7756 | + | |
7773 | 7757 | | |
7774 | 7758 | | |
7775 | 7759 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
529 | 529 | | |
530 | 530 | | |
531 | 531 | | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
532 | 585 | | |
533 | 586 | | |
534 | 587 | | |
| |||
715 | 768 | | |
716 | 769 | | |
717 | 770 | | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
718 | 810 | | |
719 | 811 | | |
720 | 812 | | |
| |||
888 | 980 | | |
889 | 981 | | |
890 | 982 | | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
891 | 1006 | | |
892 | 1007 | | |
893 | 1008 | | |
| |||
0 commit comments