1414 *
1515 * The driver requires PDO with the SQLite driver, and the PCRE engine.
1616 */
17- class WP_PDO_MySQL_On_SQLite {
17+ class WP_PDO_MySQL_On_SQLite extends PDO {
1818 /**
1919 * The path to the MySQL SQL grammar file.
2020 */
@@ -579,24 +579,47 @@ class WP_PDO_MySQL_On_SQLite {
579579 private $ user_variables = array ();
580580
581581 /**
582- * Constructor.
582+ * PDO API: Constructor.
583583 *
584584 * Set up an SQLite connection and the MySQL-on-SQLite driver.
585585 *
586586 * @param WP_SQLite_Connection $connection A SQLite database connection.
587- * @param string $database The database name.
587+ * @param string $db_name The database name.
588588 *
589589 * @throws WP_SQLite_Driver_Exception When the driver initialization fails.
590590 */
591591 public function __construct (
592- WP_SQLite_Connection $ connection ,
593- string $ database ,
594- int $ mysql_version = 80038
592+ string $ dsn ,
593+ ?string $ username = null ,
594+ ?string $ password = null ,
595+ array $ options = array ()
595596 ) {
596- $ this ->mysql_version = $ mysql_version ;
597- $ this ->connection = $ connection ;
598- $ this ->main_db_name = $ database ;
599- $ this ->db_name = $ database ;
597+ // Parse the DSN.
598+ $ dsn_parts = explode ( ': ' , $ dsn , 2 );
599+ if ( count ( $ dsn_parts ) < 2 ) {
600+ throw new PDOException ( 'invalid data source name ' );
601+ }
602+
603+ $ driver = $ dsn_parts [0 ];
604+ if ( 'mysql-on-sqlite ' !== $ driver ) {
605+ throw new PDOException ( 'could not find driver ' );
606+ }
607+
608+ $ args = array ();
609+ foreach ( explode ( '; ' , $ dsn_parts [1 ] ) as $ arg ) {
610+ $ arg_parts = explode ( '= ' , $ arg , 2 );
611+ $ args [ $ arg_parts [0 ] ] = $ arg_parts [1 ] ?? null ;
612+ }
613+
614+ $ path = $ args ['path ' ] ?? ':memory: ' ;
615+ $ db_name = $ args ['dbname ' ] ?? 'sqlite_database ' ;
616+
617+ // Create a new SQLite connection.
618+ $ this ->connection = new WP_SQLite_Connection ( array ( 'path ' => $ path ) );
619+
620+ $ this ->mysql_version = $ options ['mysql_version ' ] ?? 80038 ;
621+ $ this ->main_db_name = $ db_name ;
622+ $ this ->db_name = $ db_name ;
600623
601624 // Check the database name.
602625 if ( '' === $ this ->db_name ) {
@@ -832,7 +855,7 @@ public function get_insert_id() {
832855 }
833856
834857 /**
835- * Translate and execute a MySQL query in SQLite.
858+ * PDO API: Translate and execute a MySQL query in SQLite.
836859 *
837860 * A single MySQL query can be translated into zero or more SQLite queries.
838861 *
@@ -843,13 +866,9 @@ public function get_insert_id() {
843866 * @return mixed Return value, depending on the query type.
844867 *
845868 * @throws WP_SQLite_Driver_Exception When the query execution fails.
846- *
847- * TODO:
848- * The API of this function is not final.
849- * We should also add support for parametrized queries.
850- * See: https://github.com/Automattic/sqlite-database-integration/issues/7
851869 */
852- public function query ( string $ query , $ fetch_mode = PDO ::FETCH_OBJ , ...$ fetch_mode_args ) {
870+ #[ReturnTypeWillChange]
871+ public function query ( string $ query , ?int $ fetch_mode = PDO ::FETCH_COLUMN , ...$ fetch_mode_args ) {
853872 $ this ->flush ();
854873 $ this ->pdo_fetch_mode = $ fetch_mode ;
855874 $ this ->last_mysql_query = $ query ;
@@ -895,7 +914,11 @@ public function query( string $query, $fetch_mode = PDO::FETCH_OBJ, ...$fetch_mo
895914 if ( $ wrap_in_transaction ) {
896915 $ this ->commit_wrapper_transaction ();
897916 }
898- return $ this ->last_return_value ;
917+
918+ $ affected_rows = is_int ( $ this ->last_return_value ) ? $ this ->last_return_value : 0 ;
919+ $ rows = is_array ( $ this ->last_result ) ? $ this ->last_result : array ();
920+ $ column_meta = is_array ( $ this ->last_column_meta ) ? $ this ->last_column_meta : array ();
921+ return new WP_PDO_Synthetic_Statement ( $ rows , $ column_meta , $ affected_rows );
899922 } catch ( Throwable $ e ) {
900923 try {
901924 $ this ->rollback_user_transaction ();
0 commit comments