Skip to content

Commit 6cc331b

Browse files
committed
Build empty result sets from an unexecuted statement
Empty result sets used a no-op INSERT into a TEMP table that was created on first use. When the first use happened in a transaction, for example for START TRANSACTION itself, a rollback dropped the table, while the driver still assumed it existed. Every later statement with an empty result set then failed with "Table '_wp_sqlite_empty_result' doesn't exist". Use a prepared statement that is never executed instead. It needs no table and takes no locks, and its row count is always 0. Closing its cursor sets its error code to "00000", so it reports errors and iterates like an executed statement. Add regression tests for the rollback, and a test for the empty result statement API, so that CI catches any PHP change in this behavior. See #535
1 parent 4d94f39 commit 6cc331b

2 files changed

Lines changed: 94 additions & 41 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php‎

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,6 @@ class WP_MySQL_On_SQLite extends PDO {
8282
*/
8383
const GLOBAL_VARIABLES_TABLE_NAME = self::RESERVED_PREFIX . 'global_variables';
8484

85-
/**
86-
* Name of the connection-private TEMP table used to build empty result sets
87-
* without acquiring a write lock on the database. See create_result_statement_from_data().
88-
*/
89-
private const EMPTY_RESULT_TABLE_NAME = self::RESERVED_PREFIX . 'empty_result';
90-
9185
/**
9286
* The name of the SQLite driver version variable.
9387
*
@@ -678,14 +672,6 @@ class WP_MySQL_On_SQLite extends PDO {
678672
*/
679673
private $is_readonly;
680674

681-
/**
682-
* Whether the TEMP table backing empty result sets has been created on this
683-
* connection. See create_result_statement_from_data().
684-
*
685-
* @var bool
686-
*/
687-
private $empty_result_table_ready = false;
688-
689675
/**
690676
* Type of wrapper transaction that is active for the MySQL query emulation.
691677
*
@@ -7741,35 +7727,27 @@ private function create_result_statement_from_data( array $columns, array $rows
77417727
$pdo = $this->connection->get_pdo();
77427728

77437729
/*
7744-
* With 0 columns, we need to create a PDO statement that has no columns.
7745-
* This can be done using a noop INSERT statement that modifies no data.
7730+
* With 0 columns, we need a PDO statement with no columns and no rows:
7731+
*
7732+
* 1. Prepare a statement, but never execute it. It doesn't touch the
7733+
* database, so it takes no locks, needs no table, and its row count
7734+
* is always 0. An executed statement that writes nothing would
7735+
* report the row count of the last write instead.
7736+
* 2. The statement must have no columns. An unknown pragma has none,
7737+
* and SQLite ignores it.
7738+
* 3. Close the cursor. This sets the error code to "00000", as after
7739+
* a successful execution. Without it, iteration fails.
7740+
*
7741+
* If PHP changes this behavior, use a no-op INSERT into a TEMP table.
7742+
* Ensure the table on every use, as a rollback can drop it:
7743+
*
7744+
* CREATE TEMP TABLE IF NOT EXISTS <name> ( placeholder_for_empty_insert );
7745+
* INSERT INTO <name> ( placeholder_for_empty_insert ) SELECT NULL WHERE FALSE;
77467746
*/
77477747
if ( 0 === count( $columns ) ) {
7748-
/*
7749-
* Build a 0-column statement to represent an empty result set.
7750-
*
7751-
* A no-op "INSERT ... WHERE FALSE" into a regular table still acquires
7752-
* SQLite's write lock, even though it writes nothing. That makes
7753-
* no-write statements such as "SET sql_mode = ..." fatal with
7754-
* "database is locked" whenever another connection holds the write
7755-
* lock. Writing to a connection-private TEMP table uses the temp
7756-
* database, which has no shared lock, so it never contends.
7757-
*/
7758-
if ( ! $this->empty_result_table_ready ) {
7759-
$pdo->exec(
7760-
sprintf(
7761-
'CREATE TEMP TABLE IF NOT EXISTS %s ( placeholder_for_empty_insert )',
7762-
$this->quote_sqlite_identifier( self::EMPTY_RESULT_TABLE_NAME )
7763-
)
7764-
);
7765-
$this->empty_result_table_ready = true;
7766-
}
7767-
return $pdo->query(
7768-
sprintf(
7769-
'INSERT INTO %s ( placeholder_for_empty_insert ) SELECT NULL WHERE FALSE',
7770-
$this->quote_sqlite_identifier( self::EMPTY_RESULT_TABLE_NAME )
7771-
)
7772-
);
7748+
$stmt = $pdo->prepare( 'PRAGMA ' . self::RESERVED_PREFIX . 'empty_result' );
7749+
$stmt->closeCursor();
7750+
return $stmt;
77737751
}
77747752

77757753
/*

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_PDO_API_Tests.php‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,58 @@ function () {
529529
$this->assertSame( array( '00000', null, null ), $stmt->errorInfo() );
530530
}
531531

532+
public function test_empty_result_statement(): void {
533+
// The row count of a previous write must not leak into the empty result.
534+
$this->driver->exec( 'CREATE TABLE t (id INT)' );
535+
$this->driver->exec( 'INSERT INTO t VALUES (1), (2), (3)' );
536+
$stmt = $this->driver->query( 'SET @a = 1' );
537+
538+
// Check the error state and iteration first, as fetching resets the error state.
539+
$this->assertSame( '00000', $stmt->errorCode() );
540+
$this->assertSame( array( '00000', null, null ), $stmt->errorInfo() );
541+
$this->assertSame( array(), iterator_to_array( $stmt ) );
542+
543+
$this->assertSame( 0, $stmt->columnCount() );
544+
$this->assertSame( 0, $stmt->rowCount() );
545+
$this->assertFalse( $stmt->getColumnMeta( 0 ) );
546+
547+
$fetch_modes = array(
548+
PDO::FETCH_BOTH,
549+
PDO::FETCH_NUM,
550+
PDO::FETCH_ASSOC,
551+
PDO::FETCH_NAMED,
552+
PDO::FETCH_OBJ,
553+
PDO::FETCH_COLUMN,
554+
PDO::FETCH_KEY_PAIR,
555+
);
556+
foreach ( $fetch_modes as $mode ) {
557+
$this->assertFalse( $stmt->fetch( $mode ) );
558+
$this->assertSame( array(), $stmt->fetchAll( $mode ) );
559+
}
560+
$this->assertFalse( $stmt->fetchColumn() );
561+
$this->assertFalse( $stmt->fetchObject() );
562+
$this->assertSame( array(), $stmt->fetchAll( PDO::FETCH_GROUP | PDO::FETCH_ASSOC ) );
563+
$this->assertSame( array(), $stmt->fetchAll( PDO::FETCH_UNIQUE | PDO::FETCH_ASSOC ) );
564+
$this->assertSame( array(), $stmt->fetchAll( PDO::FETCH_CLASS, stdClass::class ) );
565+
566+
$this->assertTrue( $stmt->setFetchMode( PDO::FETCH_CLASS, stdClass::class ) );
567+
$this->assertFalse( $stmt->fetch() );
568+
569+
$value = 'unchanged';
570+
$this->assertTrue( $stmt->bindColumn( 1, $value ) );
571+
$this->assertFalse( $stmt->fetch( PDO::FETCH_BOUND ) );
572+
$this->assertSame( 'unchanged', $value );
573+
574+
$this->assertTrue( $stmt->closeCursor() );
575+
$this->assertFalse( $stmt->fetch() );
576+
577+
// Holding the statement must not block a commit or a schema change.
578+
$this->driver->exec( 'START TRANSACTION' );
579+
$this->driver->exec( 'INSERT INTO t VALUES (4)' );
580+
$this->assertSame( 0, $this->driver->exec( 'COMMIT' ) );
581+
$this->assertSame( 0, $this->driver->exec( 'DROP TABLE t' ) );
582+
}
583+
532584
public function test_statement_iteration(): void {
533585
$stmt = $this->driver->query( 'SELECT 1 AS value UNION ALL SELECT 2', PDO::FETCH_ASSOC );
534586

@@ -888,6 +940,29 @@ public function test_rollback(): void {
888940
$this->assertTrue( $result );
889941
}
890942

943+
public function test_rollback_statement_as_first_transaction_on_connection(): void {
944+
// Roll back the first transaction on the connection.
945+
$this->driver->exec( 'START TRANSACTION' );
946+
$this->driver->exec( 'ROLLBACK' );
947+
$this->assertFalse( $this->driver->inTransaction() );
948+
949+
// Statements with empty result sets keep working after the rollback.
950+
$this->driver->exec( 'START TRANSACTION' );
951+
$this->driver->exec( 'CREATE TABLE t (id INT)' );
952+
$this->driver->exec( 'ROLLBACK' );
953+
$this->assertSame( 0, $this->driver->exec( 'CREATE TABLE t (id INT)' ) );
954+
}
955+
956+
public function test_rollback_method_as_first_transaction_on_connection(): void {
957+
// Roll back the first transaction on the connection.
958+
$this->driver->beginTransaction();
959+
$this->driver->exec( 'CREATE TABLE t (id INT)' );
960+
$this->assertTrue( $this->driver->rollBack() );
961+
962+
// Statements with empty result sets keep working after the rollback.
963+
$this->assertSame( 0, $this->driver->exec( 'CREATE TABLE t (id INT)' ) );
964+
}
965+
891966
public function test_rollback_no_active_transaction(): void {
892967
$this->expectException( PDOException::class );
893968
$this->expectExceptionMessage( 'There is no active transaction' );

0 commit comments

Comments
 (0)