@@ -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
0 commit comments