Skip to content

Commit 6939616

Browse files
committed
Honor PDO constructor options
Accept null options, pass numeric attributes to new SQLite connections, and apply caller-visible attributes through the wrapper.
1 parent 9a8d2ba commit 6939616

3 files changed

Lines changed: 84 additions & 5 deletions

File tree

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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,13 @@ class WP_MySQL_On_SQLite extends PDO {
472472
*/
473473
private $error_mode = PDO::ERRMODE_EXCEPTION;
474474

475+
/**
476+
* Whether fetched scalar values should be converted to strings.
477+
*
478+
* @var bool
479+
*/
480+
private $stringify_fetches = false;
481+
475482
/**
476483
* SQLSTATE associated with the last PDO operation.
477484
*
@@ -679,9 +686,11 @@ class WP_MySQL_On_SQLite extends PDO {
679686
* @param string $dsn MySQL-on-SQLite DSN containing the SQLite path and database name.
680687
* @param string|null $username Optional. Ignored by this driver.
681688
* @param string|null $password Optional. Ignored by this driver.
682-
* @param array $options {
689+
* @param array|null $options {
683690
* Optional driver options.
684691
*
692+
* Numeric keys are handled as standard PDO constructor options.
693+
*
685694
* @type int $mysql_version Optional. MySQL version to emulate. Default 80038.
686695
* @type PDO|null $pdo Optional. Existing SQLite PDO connection.
687696
* @type string|null $journal_mode Optional. SQLite journal mode. Default 'WAL'.
@@ -694,8 +703,10 @@ public function __construct(
694703
string $dsn,
695704
?string $username = null,
696705
?string $password = null,
697-
array $options = array()
706+
?array $options = null
698707
) {
708+
$options = $options ?? array();
709+
699710
// PDO DSN can't include "\0" bytes; parsing stops at the first one.
700711
$first_null_byte_index = strpos( $dsn, "\0" );
701712
if ( false !== $first_null_byte_index ) {
@@ -736,9 +747,18 @@ public function __construct(
736747
$db_name = $args['dbname'] ?? 'sqlite_database';
737748

738749
// Create a new SQLite connection.
750+
$pdo_options = array_filter(
751+
$options,
752+
function ( $key ) {
753+
return is_int( $key );
754+
},
755+
ARRAY_FILTER_USE_KEY
756+
);
757+
739758
$connection_options = array(
740759
'journal_mode' => $options['journal_mode'] ?? null,
741760
'synchronous' => $options['synchronous'] ?? null,
761+
'pdo_options' => $pdo_options,
742762
);
743763
if ( isset( $options['pdo'] ) ) {
744764
$connection_options['pdo'] = $options['pdo'];
@@ -835,6 +855,15 @@ function ( string $sql, array $params ) {
835855
);
836856
}
837857
);
858+
859+
foreach ( $pdo_options as $attribute => $value ) {
860+
// Persistence is a connection-time-only option and was already passed
861+
// to the underlying PDO constructor when creating a new connection.
862+
if ( PDO::ATTR_PERSISTENT === $attribute ) {
863+
continue;
864+
}
865+
$this->setAttribute( $attribute, $value );
866+
}
838867
}
839868

840869
/**
@@ -1236,7 +1265,12 @@ public function setAttribute( $attribute, $value ): bool {
12361265
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
12371266
return true;
12381267
}
1239-
return $this->connection->get_pdo()->setAttribute( $attribute, $value );
1268+
1269+
$result = $this->connection->get_pdo()->setAttribute( $attribute, $value );
1270+
if ( $result && PDO::ATTR_STRINGIFY_FETCHES === $attribute ) {
1271+
$this->stringify_fetches = (bool) $value;
1272+
}
1273+
return $result;
12401274
}
12411275

12421276
/**
@@ -1254,6 +1288,9 @@ public function getAttribute( $attribute ) {
12541288
if ( PDO::ATTR_ERRMODE === $attribute ) {
12551289
return $this->error_mode;
12561290
}
1291+
if ( PDO::ATTR_STRINGIFY_FETCHES === $attribute ) {
1292+
return $this->stringify_fetches;
1293+
}
12571294
return $this->connection->get_pdo()->getAttribute( $attribute );
12581295
}
12591296

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class WP_SQLite_Connection {
8181
* @type string|null $journal_mode Optional. SQLite journal mode. Defaults to WAL.
8282
* @type string|int|null $synchronous Optional. SQLite synchronous setting. Defaults to
8383
* NORMAL when the effective journal mode is WAL.
84+
* @type array $pdo_options Optional. PDO constructor options.
8485
* }
8586
*
8687
* @throws InvalidArgumentException When some connection options are invalid.
@@ -94,8 +95,13 @@ public function __construct( array $options ) {
9495
if ( ! isset( $options['path'] ) || ! is_string( $options['path'] ) ) {
9596
throw new InvalidArgumentException( 'Option "path" is required when "connection" is not provided.' );
9697
}
97-
$pdo_class = PHP_VERSION_ID >= 80400 ? PDO\SQLite::class : PDO::class;
98-
$this->pdo = new $pdo_class( 'sqlite:' . $options['path'] );
98+
$pdo_class = PHP_VERSION_ID >= 80400 ? PDO\SQLite::class : PDO::class;
99+
$pdo_options = $options['pdo_options'] ?? array();
100+
101+
// Internal driver operations require exceptions regardless of the
102+
// caller-visible WP_MySQL_On_SQLite::ATTR_ERRMODE setting.
103+
$pdo_options[ PDO::ATTR_ERRMODE ] = PDO::ERRMODE_EXCEPTION;
104+
$this->pdo = new $pdo_class( 'sqlite:' . $options['path'], null, null, $pdo_options );
99105
}
100106

101107
// Throw exceptions on error.

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,40 @@ public function test_connection(): void {
3434
$this->assertInstanceOf( PDO::class, $driver );
3535
}
3636

37+
public function test_constructor_accepts_null_options(): void {
38+
$driver = new WP_MySQL_On_SQLite(
39+
'mysql-on-sqlite:path=:memory:;dbname=WordPress;',
40+
null,
41+
null,
42+
null
43+
);
44+
45+
$this->assertInstanceOf( PDO::class, $driver );
46+
}
47+
48+
public function test_constructor_applies_pdo_options(): void {
49+
$driver = new WP_MySQL_On_SQLite(
50+
'mysql-on-sqlite:path=:memory:;dbname=WordPress;',
51+
null,
52+
null,
53+
array(
54+
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
55+
PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT,
56+
PDO::ATTR_PERSISTENT => true,
57+
PDO::ATTR_STRINGIFY_FETCHES => true,
58+
)
59+
);
60+
61+
$this->assertSame( PDO::FETCH_ASSOC, $driver->getAttribute( PDO::ATTR_DEFAULT_FETCH_MODE ) );
62+
$this->assertSame( PDO::ERRMODE_SILENT, $driver->getAttribute( PDO::ATTR_ERRMODE ) );
63+
$this->assertTrue( $driver->getAttribute( PDO::ATTR_PERSISTENT ) );
64+
$this->assertTrue( $driver->getAttribute( PDO::ATTR_STRINGIFY_FETCHES ) );
65+
$this->assertSame( array( 'value' => '1' ), $driver->query( 'SELECT 1 AS value' )->fetch() );
66+
67+
// Internal operations always retain exception mode.
68+
$this->assertSame( PDO::ERRMODE_EXCEPTION, $driver->get_sqlite_pdo()->getAttribute( PDO::ATTR_ERRMODE ) );
69+
}
70+
3771
public function test_driver_exception_exposes_originating_driver(): void {
3872
$exception = new WP_MySQL_On_SQLite_Exception( $this->driver, 'Test error.' );
3973

@@ -737,13 +771,15 @@ public function test_attr_default_fetch_mode(): void {
737771

738772
public function test_attr_stringify_fetches(): void {
739773
$this->driver->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true );
774+
$this->assertTrue( $this->driver->getAttribute( PDO::ATTR_STRINGIFY_FETCHES ) );
740775
$result = $this->driver->query( "SELECT 123, 1.23, 'abc', true, false" );
741776
$this->assertSame(
742777
array( '123', '1.23', 'abc', '1', '0' ),
743778
$result->fetch( PDO::FETCH_NUM )
744779
);
745780

746781
$this->driver->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, false );
782+
$this->assertFalse( $this->driver->getAttribute( PDO::ATTR_STRINGIFY_FETCHES ) );
747783
$result = $this->driver->query( "SELECT 123, 1.23, 'abc', true, false" );
748784
$this->assertSame(
749785
/*

0 commit comments

Comments
 (0)