Skip to content

Commit 69e1189

Browse files
committed
Add a fix for a PDO SQLite bug in PHP < 7.3 (#79664)
See also: php/php-src#5654
1 parent 951fc45 commit 69e1189

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

tests/WP_SQLite_Driver_Translation_Tests.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public function testInsert(): void {
9797
$this->driver->query( 'CREATE TABLE t (c INT, c1 INT, c2 INT)' );
9898
$this->driver->query( 'CREATE TABLE t1 (c1 INT, c2 INT)' );
9999
$this->driver->query( 'CREATE TABLE t2 (c1 INT, c2 INT)' );
100+
$this->driver->query( 'INSERT INTO t2 VALUES (1, 2)' );
100101

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

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

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

196200
$this->assertQuery(
197201
'REPLACE INTO `t1` (`c1`) SELECT CAST(`column1` AS TEXT) FROM (VALUES ( 1 )) WHERE true',

wp-includes/sqlite-ast/class-wp-sqlite-driver.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4512,6 +4512,23 @@ function ( $column ) use ( $is_strict_mode, $insert_map ) {
45124512
$stmt->execute();
45134513

45144514
for ( $i = 0; $i < $stmt->columnCount(); $i++ ) {
4515+
/*
4516+
* Workaround for PHP PDO SQLite bug (#79664) in PHP < 7.3.
4517+
* See also: https://github.com/php/php-src/pull/5654
4518+
*/
4519+
if ( PHP_VERSION_ID < 70300 ) {
4520+
try {
4521+
$column_meta = $stmt->getColumnMeta( $i );
4522+
} catch ( Throwable $e ) {
4523+
$column_meta = false;
4524+
}
4525+
if ( false === $column_meta ) {
4526+
// Due to a PDO bug in PHP < 7.3, we get no column metadata
4527+
// when no rows are returned. In that case, no data will be
4528+
// inserted, so we can bail out using a simple translation.
4529+
return $this->translate( $node );
4530+
}
4531+
}
45154532
$select_list[] = $stmt->getColumnMeta( $i )['name'];
45164533
}
45174534
} else {

0 commit comments

Comments
 (0)