Skip to content
Merged
Prev Previous commit
Next Next commit
Add a fix for a PDO SQLite bug in PHP < 7.3 (#79664)
  • Loading branch information
JanJakes committed Oct 29, 2025
commit 69e118958c7ed530740cf42a443a600d8fd303d7
4 changes: 4 additions & 0 deletions tests/WP_SQLite_Driver_Translation_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function testInsert(): void {
$this->driver->query( 'CREATE TABLE t (c INT, c1 INT, c2 INT)' );
$this->driver->query( 'CREATE TABLE t1 (c1 INT, c2 INT)' );
$this->driver->query( 'CREATE TABLE t2 (c1 INT, c2 INT)' );
$this->driver->query( 'INSERT INTO t2 VALUES (1, 2)' );

$this->assertQuery(
'INSERT INTO `t` (`c`) SELECT `column1` FROM (VALUES ( 1 )) WHERE true',
Expand Down Expand Up @@ -130,6 +131,7 @@ public function testInsert(): void {
public function testInsertWithTypeCasting(): void {
$this->driver->query( 'CREATE TABLE t1 (c1 TEXT, c2 TEXT)' );
$this->driver->query( 'CREATE TABLE t2 (c1 TEXT, c2 TEXT)' );
$this->driver->query( 'INSERT INTO t2 VALUES (1, 2)' );

$this->assertQuery(
'INSERT INTO `t1` (`c1`) SELECT CAST(`column1` AS TEXT) FROM (VALUES ( 1 )) WHERE true',
Expand Down Expand Up @@ -159,6 +161,7 @@ public function testReplace(): void {
$this->driver->query( 'CREATE TABLE t (c INT, c1 INT, c2 INT)' );
$this->driver->query( 'CREATE TABLE t1 (c1 INT, c2 INT)' );
$this->driver->query( 'CREATE TABLE t2 (c1 INT, c2 INT)' );
$this->driver->query( 'INSERT INTO t2 VALUES (1, 2)' );

$this->assertQuery(
'REPLACE INTO `t` (`c`) SELECT `column1` FROM (VALUES ( 1 )) WHERE true',
Expand Down Expand Up @@ -192,6 +195,7 @@ public function testReplace(): void {
public function testReplaceWithTypeCasting(): void {
$this->driver->query( 'CREATE TABLE t1 (c1 TEXT, c2 TEXT)' );
$this->driver->query( 'CREATE TABLE t2 (c1 TEXT, c2 TEXT)' );
$this->driver->query( 'INSERT INTO t2 VALUES (1, 2)' );

$this->assertQuery(
'REPLACE INTO `t1` (`c1`) SELECT CAST(`column1` AS TEXT) FROM (VALUES ( 1 )) WHERE true',
Expand Down
17 changes: 17 additions & 0 deletions wp-includes/sqlite-ast/class-wp-sqlite-driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4512,6 +4512,23 @@ function ( $column ) use ( $is_strict_mode, $insert_map ) {
$stmt->execute();

for ( $i = 0; $i < $stmt->columnCount(); $i++ ) {
/*
* Workaround for PHP PDO SQLite bug (#79664) in PHP < 7.3.
* See also: https://github.com/php/php-src/pull/5654
*/
if ( PHP_VERSION_ID < 70300 ) {
try {
$column_meta = $stmt->getColumnMeta( $i );
} catch ( Throwable $e ) {
$column_meta = false;
}
if ( false === $column_meta ) {
// Due to a PDO bug in PHP < 7.3, we get no column metadata
// when no rows are returned. In that case, no data will be
// inserted, so we can bail out using a simple translation.
return $this->translate( $node );
}
}
$select_list[] = $stmt->getColumnMeta( $i )['name'];
}
} else {
Expand Down