Skip to content

Commit 1184f11

Browse files
committed
Add a workaround for PHP PDO SQLite bug (#79664) in PHP < 7.3
See also: php/php-src#5654
1 parent fd95703 commit 1184f11

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

tests/WP_SQLite_Driver_Tests.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8170,4 +8170,29 @@ public function testColumnInfoWithZeroRows(): void {
81708170
$column_info
81718171
);
81728172
}
8173+
8174+
public function testColumnInfoWithZeroRowsPhpBug(): void {
8175+
if ( PHP_VERSION_ID < 70300 ) {
8176+
$this->markTestSkipped( 'Skipping due to PHP bug (#79664)' );
8177+
}
8178+
8179+
$this->assertQuery( 'CREATE TABLE t ( id INT )' );
8180+
$this->assertQuery( 'SELECT * FROM t' );
8181+
$this->assertEquals( 1, $this->engine->get_last_column_count() );
8182+
$column_info = $this->engine->get_last_column_meta();
8183+
$this->assertCount( 1, $column_info );
8184+
$this->assertSame(
8185+
array(
8186+
'native_type' => 'LONG',
8187+
'pdo_type' => PDO::PARAM_INT,
8188+
'flags' => array(),
8189+
'table' => 't',
8190+
'name' => 'id',
8191+
'len' => 11,
8192+
'precision' => 0,
8193+
'sqlite:decl_type' => 'INTEGER',
8194+
),
8195+
$column_info[0]
8196+
);
8197+
}
81738198
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,27 @@ private function execute_select_statement( WP_Parser_Node $node ): void {
14421442
// seems to erase type information for expressions in the SELECT clause.
14431443
$this->last_column_meta = array();
14441444
for ( $i = 0; $i < $stmt->columnCount(); $i++ ) {
1445+
/*
1446+
* Workaround for PHP PDO SQLite bug (#79664) in PHP < 7.3.
1447+
* See also: https://github.com/php/php-src/pull/5654
1448+
*/
1449+
if ( PHP_VERSION_ID < 70300 ) {
1450+
try {
1451+
$this->last_column_meta[] = $stmt->getColumnMeta( $i );
1452+
} catch ( Throwable $e ) {
1453+
$this->last_column_meta[] = array(
1454+
'native_type' => 'null',
1455+
'pdo_type' => PDO::PARAM_NULL,
1456+
'flags' => array(),
1457+
'table' => '',
1458+
'name' => '',
1459+
'len' => -1,
1460+
'precision' => 0,
1461+
);
1462+
}
1463+
continue;
1464+
}
1465+
14451466
$this->last_column_meta[] = $stmt->getColumnMeta( $i );
14461467
}
14471468

0 commit comments

Comments
 (0)