Skip to content

Commit e607fe5

Browse files
committed
Preserve the user-visible last insert ID
Snapshot IDs produced by user INSERT and REPLACE statements so internal information-schema writes cannot alter PDO and legacy driver results.
1 parent 628b64f commit e607fe5

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,13 @@ class WP_MySQL_On_SQLite extends PDO {
524524
*/
525525
private $connection;
526526

527+
/**
528+
* ID generated by the last user-issued INSERT or REPLACE statement.
529+
*
530+
* @var string
531+
*/
532+
private $last_insert_id = '0';
533+
527534
/**
528535
* User-defined functions registered on the SQLite connection.
529536
*
@@ -1106,7 +1113,7 @@ public function lastInsertId( $name = null ) {
11061113
return false;
11071114
}
11081115

1109-
return $this->connection->get_pdo()->lastInsertId( $name );
1116+
return $this->last_insert_id;
11101117
}
11111118

11121119
/**
@@ -2208,10 +2215,12 @@ function ( $column ) {
22082215
throw $e;
22092216
}
22102217
}
2218+
$this->last_insert_id = $this->connection->get_last_insert_id();
22112219
return;
22122220
}
22132221

22142222
$this->last_result_statement = $this->execute_sqlite_query( $query );
2223+
$this->last_insert_id = $this->connection->get_last_insert_id();
22152224
}
22162225

22172226
/**
@@ -7399,6 +7408,7 @@ private function flush(): void {
73997408
$this->last_sqlite_queries = array();
74007409
$this->last_result_statement = null;
74017410
$this->last_affected_rows = null;
7411+
$this->last_insert_id = '0';
74027412
$this->last_column_meta = array();
74037413
$this->is_readonly = false;
74047414
$this->wrapper_transaction_type = null;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,18 @@ public function test_exec(): void {
320320
}
321321

322322
public function test_last_insert_id(): void {
323+
$this->assertSame( '0', $this->driver->lastInsertId() );
324+
323325
$this->driver->query( 'CREATE TABLE t (id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY)' );
326+
$this->assertSame( '0', $this->driver->lastInsertId() );
327+
324328
$this->driver->query( 'INSERT INTO t (id) VALUES (NULL)' );
325329

326330
$this->assertSame( '1', $this->driver->lastInsertId() );
327331
$this->assertSame( '1', $this->driver->lastInsertId( 'ignored_sequence_name' ) );
332+
333+
$this->driver->query( 'CREATE TABLE another_table (id INT)' );
334+
$this->assertSame( '0', $this->driver->lastInsertId() );
328335
}
329336

330337
public function test_last_insert_id_rejects_invalid_sequence_name(): void {

0 commit comments

Comments
 (0)