Skip to content

Commit 0bfd738

Browse files
committed
Build empty result sets without a TEMP table
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 an unknown pragma instead. It has no columns, SQLite ignores it, and it needs no table and takes no locks. PDO reports the row count of the last write for it, so result sets that the driver builds itself now default to 0 affected rows. Add regression tests for the rollback, and a test for the empty result statement API. See #535
1 parent 241e6bc commit 0bfd738

2 files changed

Lines changed: 87 additions & 41 deletions

File tree

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

Lines changed: 11 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
*
@@ -7752,38 +7738,22 @@ private function flush(): void {
77527738
* @return PDOStatement The corresponding PDO SQLite statement.
77537739
*/
77547740
private function create_result_statement_from_data( array $columns, array $rows ): PDOStatement {
7741+
/*
7742+
* The statement writes nothing, but PDO SQLite can report the row count
7743+
* of the last write for it. Default the affected rows to 0.
7744+
*/
7745+
$this->last_affected_rows = $this->last_affected_rows ?? 0;
7746+
77557747
$pdo = $this->connection->get_pdo();
77567748

77577749
/*
7758-
* With 0 columns, we need to create a PDO statement that has no columns.
7759-
* This can be done using a noop INSERT statement that modifies no data.
7750+
* With 0 columns, we need a PDO statement with no columns and no rows.
7751+
*
7752+
* An unknown pragma has no columns, needs no table, takes no locks,
7753+
* and SQLite ignores it.
77607754
*/
77617755
if ( 0 === count( $columns ) ) {
7762-
/*
7763-
* Build a 0-column statement to represent an empty result set.
7764-
*
7765-
* A no-op "INSERT ... WHERE FALSE" into a regular table still acquires
7766-
* SQLite's write lock, even though it writes nothing. That makes
7767-
* no-write statements such as "SET sql_mode = ..." fatal with
7768-
* "database is locked" whenever another connection holds the write
7769-
* lock. Writing to a connection-private TEMP table uses the temp
7770-
* database, which has no shared lock, so it never contends.
7771-
*/
7772-
if ( ! $this->empty_result_table_ready ) {
7773-
$pdo->exec(
7774-
sprintf(
7775-
'CREATE TEMP TABLE IF NOT EXISTS %s ( placeholder_for_empty_insert )',
7776-
$this->quote_sqlite_identifier( self::EMPTY_RESULT_TABLE_NAME )
7777-
)
7778-
);
7779-
$this->empty_result_table_ready = true;
7780-
}
7781-
return $pdo->query(
7782-
sprintf(
7783-
'INSERT INTO %s ( placeholder_for_empty_insert ) SELECT NULL WHERE FALSE',
7784-
$this->quote_sqlite_identifier( self::EMPTY_RESULT_TABLE_NAME )
7785-
)
7786-
);
7756+
return $pdo->query( 'PRAGMA ' . self::RESERVED_PREFIX . 'empty_result' );
77877757
}
77887758

77897759
/*

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,59 @@ 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 an unread statement must not block a commit or a schema change.
578+
$stmt = $this->driver->query( 'SET @a = 1' );
579+
$this->driver->exec( 'START TRANSACTION' );
580+
$this->driver->exec( 'INSERT INTO t VALUES (4)' );
581+
$this->assertSame( 0, $this->driver->exec( 'COMMIT' ) );
582+
$this->assertSame( 0, $this->driver->exec( 'DROP TABLE t' ) );
583+
}
584+
532585
public function test_statement_iteration(): void {
533586
$stmt = $this->driver->query( 'SELECT 1 AS value UNION ALL SELECT 2', PDO::FETCH_ASSOC );
534587

@@ -927,6 +980,29 @@ public function test_rollback(): void {
927980
$this->assertTrue( $result );
928981
}
929982

983+
public function test_rollback_statement_as_first_transaction_on_connection(): void {
984+
// Roll back the first transaction on the connection.
985+
$this->driver->exec( 'START TRANSACTION' );
986+
$this->driver->exec( 'ROLLBACK' );
987+
$this->assertFalse( $this->driver->inTransaction() );
988+
989+
// Statements with empty result sets keep working after the rollback.
990+
$this->driver->exec( 'START TRANSACTION' );
991+
$this->driver->exec( 'CREATE TABLE t (id INT)' );
992+
$this->driver->exec( 'ROLLBACK' );
993+
$this->assertSame( 0, $this->driver->exec( 'CREATE TABLE t (id INT)' ) );
994+
}
995+
996+
public function test_rollback_method_as_first_transaction_on_connection(): void {
997+
// Roll back the first transaction on the connection.
998+
$this->driver->beginTransaction();
999+
$this->driver->exec( 'CREATE TABLE t (id INT)' );
1000+
$this->assertTrue( $this->driver->rollBack() );
1001+
1002+
// Statements with empty result sets keep working after the rollback.
1003+
$this->assertSame( 0, $this->driver->exec( 'CREATE TABLE t (id INT)' ) );
1004+
}
1005+
9301006
public function test_rollback_no_active_transaction(): void {
9311007
$this->expectException( PDOException::class );
9321008
$this->expectExceptionMessage( 'There is no active transaction' );

0 commit comments

Comments
 (0)