Skip to content

Commit 2b7261c

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 fe1965d commit 2b7261c

5 files changed

Lines changed: 141 additions & 13 deletions

File tree

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

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,16 @@ class WP_MySQL_On_SQLite extends PDO {
535535
*/
536536
private $error_mode = PDO::ERRMODE_EXCEPTION;
537537

538+
/**
539+
* Whether fetched scalar values should be converted to strings.
540+
*
541+
* PDO SQLite cannot report PDO::ATTR_STRINGIFY_FETCHES on PHP 7.2–8.1,
542+
* so the wrapper tracks its value on those versions.
543+
*
544+
* @var bool
545+
*/
546+
private $stringify_fetches = false;
547+
538548
/**
539549
* SQLSTATE associated with the last PDO operation.
540550
*
@@ -735,9 +745,11 @@ class WP_MySQL_On_SQLite extends PDO {
735745
* @param string $dsn MySQL-on-SQLite DSN containing the SQLite path and database name.
736746
* @param string|null $username Optional. Ignored by this driver.
737747
* @param string|null $password Optional. Ignored by this driver.
738-
* @param array $options {
748+
* @param array|null $options {
739749
* Optional driver options.
740750
*
751+
* Numeric keys are handled as standard PDO constructor options.
752+
*
741753
* @type int $mysql_version Optional. MySQL version to emulate. Default 80038.
742754
* @type PDO|null $pdo Optional. Existing SQLite PDO connection.
743755
* @type string|null $journal_mode Optional. SQLite journal mode. Default 'WAL'.
@@ -750,8 +762,10 @@ public function __construct(
750762
string $dsn,
751763
?string $username = null,
752764
?string $password = null,
753-
array $options = array()
765+
?array $options = null
754766
) {
767+
$options = $options ?? array();
768+
755769
// PDO DSN can't include "\0" bytes; parsing stops at the first one.
756770
$first_null_byte_index = strpos( $dsn, "\0" );
757771
if ( false !== $first_null_byte_index ) {
@@ -792,9 +806,18 @@ public function __construct(
792806
$db_name = $args['dbname'] ?? 'sqlite_database';
793807

794808
// Create a new SQLite connection.
809+
$pdo_options = array_filter(
810+
$options,
811+
function ( $key ) {
812+
return is_int( $key );
813+
},
814+
ARRAY_FILTER_USE_KEY
815+
);
816+
795817
$connection_options = array(
796818
'journal_mode' => $options['journal_mode'] ?? null,
797819
'synchronous' => $options['synchronous'] ?? null,
820+
'pdo_options' => $pdo_options,
798821
);
799822
if ( isset( $options['pdo'] ) ) {
800823
$connection_options['pdo'] = $options['pdo'];
@@ -892,6 +915,15 @@ function ( string $sql, array $params ) {
892915
);
893916
}
894917
);
918+
919+
foreach ( $pdo_options as $attribute => $value ) {
920+
// Persistence is a connection-time-only option and was already passed
921+
// to the underlying PDO constructor when creating a new connection.
922+
if ( PDO::ATTR_PERSISTENT === $attribute ) {
923+
continue;
924+
}
925+
$this->setAttribute( $attribute, $value );
926+
}
895927
}
896928

897929
/**
@@ -937,11 +969,6 @@ public function query( string $query, ?int $fetch_mode = null, ...$fetch_mode_ar
937969
);
938970
return false;
939971
}
940-
941-
// When the default FETCH_BOTH is not set explicitly, additional
942-
// arguments are ignored, and the argument count is not validated.
943-
$fetch_mode = $this->connection->get_pdo()->getAttribute( PDO::ATTR_DEFAULT_FETCH_MODE );
944-
$fetch_mode_args = array();
945972
} elseif ( PDO::FETCH_COLUMN === $fetch_mode ) {
946973
if ( 3 !== $arg_count ) {
947974
throw new ArgumentCountError(
@@ -1076,7 +1103,9 @@ public function query( string $query, ?int $fetch_mode = null, ...$fetch_mode_ar
10761103
}
10771104

10781105
$stmt = new WP_MySQL_On_SQLite_Statement( $this->last_result_statement, $query, $this->last_affected_rows );
1079-
$stmt->setFetchMode( $fetch_mode, ...$fetch_mode_args );
1106+
if ( null !== $fetch_mode ) {
1107+
$stmt->setFetchMode( $fetch_mode, ...$fetch_mode_args );
1108+
}
10801109
$this->error_code = '00000';
10811110
$this->error_info = array( '00000', null, null );
10821111
return $stmt;
@@ -1317,7 +1346,12 @@ public function setAttribute( $attribute, $value ): bool {
13171346
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
13181347
return true;
13191348
}
1320-
return $this->connection->get_pdo()->setAttribute( $attribute, $value );
1349+
1350+
$result = $this->connection->get_pdo()->setAttribute( $attribute, $value );
1351+
if ( $result && PDO::ATTR_STRINGIFY_FETCHES === $attribute ) {
1352+
$this->stringify_fetches = (bool) $value;
1353+
}
1354+
return $result;
13211355
}
13221356

13231357
/**
@@ -1336,6 +1370,10 @@ public function getAttribute( $attribute ) {
13361370
if ( PDO::ATTR_ERRMODE === $attribute ) {
13371371
return $this->error_mode;
13381372
}
1373+
if ( PDO::ATTR_STRINGIFY_FETCHES === $attribute && PHP_VERSION_ID < 80200 ) {
1374+
// PDO SQLite cannot report this attribute before PHP 8.2.
1375+
return $this->stringify_fetches;
1376+
}
13391377
return $this->connection->get_pdo()->getAttribute( $attribute );
13401378
}
13411379

‎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/src/sqlite/class-wp-sqlite-driver.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function __construct(
8282
);
8383
$this->client_info = $this->mysql_on_sqlite_driver->client_info;
8484

85-
$connection->get_pdo()->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true );
85+
$this->mysql_on_sqlite_driver->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true );
8686
}
8787

8888
/**

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,85 @@ 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_STRINGIFY_FETCHES => true,
57+
)
58+
);
59+
60+
$this->assertSame( PDO::FETCH_ASSOC, $driver->getAttribute( PDO::ATTR_DEFAULT_FETCH_MODE ) );
61+
$this->assertSame( PDO::ERRMODE_SILENT, $driver->getAttribute( PDO::ATTR_ERRMODE ) );
62+
$this->assertTrue( $driver->getAttribute( PDO::ATTR_STRINGIFY_FETCHES ) );
63+
$this->assertSame( array( 'value' => '1' ), $driver->query( 'SELECT 1 AS value' )->fetch() );
64+
65+
// Internal operations always retain exception mode.
66+
$this->assertSame( PDO::ERRMODE_EXCEPTION, $driver->get_sqlite_pdo()->getAttribute( PDO::ATTR_ERRMODE ) );
67+
}
68+
69+
public function test_constructor_applies_fetch_column_default(): void {
70+
$driver = new WP_MySQL_On_SQLite(
71+
'mysql-on-sqlite:path=:memory:;dbname=WordPress;',
72+
null,
73+
null,
74+
array( PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_COLUMN )
75+
);
76+
77+
$this->assertSame( 'value', $driver->query( "SELECT 'value'" )->fetch() );
78+
}
79+
80+
public function test_constructor_applies_persistent_option(): void {
81+
$path = tempnam( sys_get_temp_dir(), 'wp_sqlite_' );
82+
unlink( $path );
83+
84+
try {
85+
$driver = new WP_MySQL_On_SQLite(
86+
'mysql-on-sqlite:path=' . $path . ';dbname=WordPress;',
87+
null,
88+
null,
89+
array( PDO::ATTR_PERSISTENT => true )
90+
);
91+
92+
$this->assertTrue( $driver->getAttribute( PDO::ATTR_PERSISTENT ) );
93+
} finally {
94+
$this->remove_database_files( $path );
95+
}
96+
}
97+
98+
public function test_constructor_reports_stringify_fetches_from_injected_pdo(): void {
99+
if ( PHP_VERSION_ID < 80200 ) {
100+
$this->markTestSkipped( 'PDO SQLite cannot report PDO::ATTR_STRINGIFY_FETCHES before PHP 8.2.' );
101+
}
102+
103+
$pdo_class = PHP_VERSION_ID >= 80400 ? PDO\SQLite::class : PDO::class;
104+
$pdo = new $pdo_class( 'sqlite::memory:' );
105+
$pdo->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true );
106+
$driver = new WP_MySQL_On_SQLite(
107+
'mysql-on-sqlite:dbname=wp',
108+
null,
109+
null,
110+
array( 'pdo' => $pdo )
111+
);
112+
113+
$this->assertTrue( $driver->getAttribute( PDO::ATTR_STRINGIFY_FETCHES ) );
114+
}
115+
37116
public function test_driver_exception_exposes_originating_driver(): void {
38117
$exception = new WP_MySQL_On_SQLite_Exception( $this->driver, 'Test error.' );
39118

@@ -778,13 +857,15 @@ public function test_attr_default_fetch_mode(): void {
778857

779858
public function test_attr_stringify_fetches(): void {
780859
$this->driver->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true );
860+
$this->assertTrue( $this->driver->getAttribute( PDO::ATTR_STRINGIFY_FETCHES ) );
781861
$result = $this->driver->query( "SELECT 123, 1.23, 'abc', true, false" );
782862
$this->assertSame(
783863
array( '123', '1.23', 'abc', '1', '0' ),
784864
$result->fetch( PDO::FETCH_NUM )
785865
);
786866

787867
$this->driver->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, false );
868+
$this->assertFalse( $this->driver->getAttribute( PDO::ATTR_STRINGIFY_FETCHES ) );
788869
$result = $this->driver->query( "SELECT 123, 1.23, 'abc', true, false" );
789870
$this->assertSame(
790871
/*

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ function () {
2828
WP_SQLite_Driver::class
2929
);
3030

31-
$this->assertInstanceOf( WP_MySQL_On_SQLite::class, $get_driver() );
31+
$mysql_on_sqlite_driver = $get_driver();
32+
33+
$this->assertInstanceOf( WP_MySQL_On_SQLite::class, $mysql_on_sqlite_driver );
34+
$this->assertTrue( $mysql_on_sqlite_driver->getAttribute( PDO::ATTR_STRINGIFY_FETCHES ) );
3235
$this->assertSame( $this->sqlite, $this->driver->get_connection()->get_pdo() );
3336
$this->assertSame( $this->driver->get_sqlite_version(), $this->driver->client_info );
3437
$this->assertSame( SQLITE_DRIVER_VERSION, $this->driver->get_saved_driver_version() );

0 commit comments

Comments
 (0)