Skip to content

Commit 4099a4b

Browse files
committed
Support whitespace in PDO DSN as per PHP PDO
1 parent 2120a9f commit 4099a4b

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

tests/WP_PDO_MySQL_On_SQLite_PDO_API_Tests.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@ public function test_connection(): void {
1919
$this->assertInstanceOf( PDO::class, $driver );
2020
}
2121

22+
public function test_dsn_parsing(): void {
23+
// Standard DSN.
24+
$driver = new WP_PDO_MySQL_On_SQLite( 'mysql-on-sqlite:path=:memory:;dbname=wp' );
25+
$this->assertSame( 'wp', $driver->query( 'SELECT DATABASE()' )->fetch()[0] );
26+
27+
// DSN with trailing semicolon.
28+
$driver = new WP_PDO_MySQL_On_SQLite( 'mysql-on-sqlite:path=:memory:;dbname=wp;' );
29+
$this->assertSame( 'wp', $driver->query( 'SELECT DATABASE()' )->fetch()[0] );
30+
31+
// DSN with whitespace before argument names.
32+
$driver = new WP_PDO_MySQL_On_SQLite( "mysql-on-sqlite: path=:memory:;\t dbname=wp" );
33+
$this->assertSame( 'wp', $driver->query( 'SELECT DATABASE()' )->fetch()[0] );
34+
35+
// DSN with whitespace in the database name.
36+
$driver = new WP_PDO_MySQL_On_SQLite( 'mysql-on-sqlite:path=:memory:;dbname= w p ' );
37+
$this->assertSame( ' w p ', $driver->query( 'SELECT DATABASE()' )->fetch()[0] );
38+
}
39+
2240
public function test_query(): void {
2341
$result = $this->driver->query( "SELECT 1, 'abc'" );
2442
$this->assertInstanceOf( PDOStatement::class, $result );

wp-includes/sqlite-ast/class-wp-pdo-mysql-on-sqlite.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,10 @@ public function __construct(
619619

620620
$args = array();
621621
foreach ( explode( ';', $dsn_parts[1] ) as $arg ) {
622+
$arg = ltrim( $arg ); // PDO DSN allows whitespace before argument name.
623+
if ( '' === $arg ) {
624+
continue;
625+
}
622626
$arg_parts = explode( '=', $arg, 2 );
623627
$args[ $arg_parts[0] ] = $arg_parts[1] ?? null;
624628
}

0 commit comments

Comments
 (0)