Skip to content

Commit 3e974b3

Browse files
committed
Make WP_PDO_MySQL_On_SQLite extend PDO, add a dummy WP_PDO_Synthetic_Statement
1 parent 22203ee commit 3e974b3

5 files changed

Lines changed: 369 additions & 22 deletions

File tree

tests/WP_PDO_MySQL_On_SQLite_PDO_API_Tests.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ class WP_PDO_MySQL_On_SQLite_PDO_API_Tests extends TestCase {
77
private $driver;
88

99
public function setUp(): void {
10-
$connection = new WP_SQLite_Connection( array( 'path' => ':memory:' ) );
11-
$this->driver = new WP_PDO_MySQL_On_SQLite( $connection, 'wp' );
10+
$this->driver = new WP_PDO_MySQL_On_SQLite( 'mysql-on-sqlite:path=:memory:;dbname=wp;' );
11+
}
12+
13+
public function test_connection(): void {
14+
$driver = new WP_PDO_MySQL_On_SQLite( 'mysql-on-sqlite:path=:memory:;dbname=WordPress;' );
15+
$this->assertInstanceOf( PDO::class, $driver );
1216
}
1317

1418
public function test_begin_transaction(): void {

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

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
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,51 @@ 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+
if ( isset( $options['pdo'] ) ) {
619+
$this->connection = new WP_SQLite_Connection( array( 'pdo' => $options['pdo'] ) );
620+
} else {
621+
$this->connection = new WP_SQLite_Connection( array( 'path' => $path ) );
622+
}
623+
624+
$this->mysql_version = $options['mysql_version'] ?? 80038;
625+
$this->main_db_name = $db_name;
626+
$this->db_name = $db_name;
600627

601628
// Check the database name.
602629
if ( '' === $this->db_name ) {
@@ -685,7 +712,7 @@ function ( string $sql, array $params ) {
685712
}
686713

687714
/**
688-
* Translate and execute a MySQL query in SQLite.
715+
* PDO API: Translate and execute a MySQL query in SQLite.
689716
*
690717
* A single MySQL query can be translated into zero or more SQLite queries.
691718
*
@@ -696,13 +723,9 @@ function ( string $sql, array $params ) {
696723
* @return mixed Return value, depending on the query type.
697724
*
698725
* @throws WP_SQLite_Driver_Exception When the query execution fails.
699-
*
700-
* TODO:
701-
* The API of this function is not final.
702-
* We should also add support for parametrized queries.
703-
* See: https://github.com/Automattic/sqlite-database-integration/issues/7
704726
*/
705-
public function query( string $query, $fetch_mode = PDO::FETCH_OBJ, ...$fetch_mode_args ) {
727+
#[ReturnTypeWillChange]
728+
public function query( string $query, ?int $fetch_mode = PDO::FETCH_COLUMN, ...$fetch_mode_args ) {
706729
$this->flush();
707730
$this->pdo_fetch_mode = $fetch_mode;
708731
$this->last_mysql_query = $query;
@@ -748,7 +771,8 @@ public function query( string $query, $fetch_mode = PDO::FETCH_OBJ, ...$fetch_mo
748771
if ( $wrap_in_transaction ) {
749772
$this->commit_wrapper_transaction();
750773
}
751-
return $this->last_return_value;
774+
775+
return new WP_PDO_Synthetic_Statement();
752776
} catch ( Throwable $e ) {
753777
try {
754778
$this->rollback_user_transaction();

0 commit comments

Comments
 (0)