Skip to content

Commit 16ec027

Browse files
committed
Reintroduce WP_SQLite_Driver as a temporary proxy exposing legacy API
1 parent c91b55b commit 16ec027

6 files changed

Lines changed: 163 additions & 20 deletions

tests/WP_SQLite_Information_Schema_Reconstructor_Tests.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function setUp(): void {
4949
);
5050

5151
$builder = new WP_SQLite_Information_Schema_Builder(
52-
WP_SQLite_Driver::RESERVED_PREFIX,
52+
WP_PDO_MySQL_On_SQLite::RESERVED_PREFIX,
5353
$this->engine->get_connection()
5454
);
5555

wp-includes/sqlite-ast/class-wp-sqlite-configurator.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class WP_SQLite_Configurator {
1414
/**
1515
* The SQLite driver instance.
1616
*
17-
* @var WP_SQLite_Driver
17+
* @var WP_PDO_MySQL_On_SQLite
1818
*/
1919
private $driver;
2020

@@ -35,11 +35,11 @@ class WP_SQLite_Configurator {
3535
/**
3636
* Constructor.
3737
*
38-
* @param WP_SQLite_Driver $driver The SQLite driver instance.
38+
* @param WP_PDO_MySQL_On_SQLite $driver The SQLite driver instance.
3939
* @param WP_SQLite_Information_Schema_Builder $schema_builder The information schema builder instance.
4040
*/
4141
public function __construct(
42-
WP_SQLite_Driver $driver,
42+
WP_PDO_MySQL_On_SQLite $driver,
4343
WP_SQLite_Information_Schema_Builder $schema_builder
4444
) {
4545
$this->driver = $driver;
@@ -100,7 +100,7 @@ private function ensure_global_variables_table(): void {
100100
sprintf(
101101
'CREATE TABLE IF NOT EXISTS %s (name TEXT PRIMARY KEY, value TEXT)',
102102
$this->driver->get_connection()->quote_identifier(
103-
WP_SQLite_Driver::GLOBAL_VARIABLES_TABLE_NAME
103+
WP_PDO_MySQL_On_SQLite::GLOBAL_VARIABLES_TABLE_NAME
104104
)
105105
)
106106
);
@@ -260,11 +260,11 @@ private function save_current_driver_version(): void {
260260
sprintf(
261261
'INSERT INTO %s (name, value) VALUES (?, ?) ON CONFLICT(name) DO UPDATE SET value = ?',
262262
$this->driver->get_connection()->quote_identifier(
263-
WP_SQLite_Driver::GLOBAL_VARIABLES_TABLE_NAME
263+
WP_PDO_MySQL_On_SQLite::GLOBAL_VARIABLES_TABLE_NAME
264264
)
265265
),
266266
array(
267-
WP_SQLite_Driver::DRIVER_VERSION_VARIABLE_NAME,
267+
WP_PDO_MySQL_On_SQLite::DRIVER_VERSION_VARIABLE_NAME,
268268
SQLITE_DRIVER_VERSION,
269269
SQLITE_DRIVER_VERSION,
270270
)

wp-includes/sqlite-ast/class-wp-sqlite-driver-exception.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ class WP_SQLite_Driver_Exception extends PDOException {
44
/**
55
* The SQLite driver that originated the exception.
66
*
7-
* @var WP_SQLite_Driver
7+
* @var WP_PDO_MySQL_On_SQLite
88
*/
99
private $driver;
1010

1111
/**
1212
* Constructor.
1313
*
14-
* @param WP_SQLite_Driver $driver The SQLite driver that originated the exception.
15-
* @param string $message The exception message.
16-
* @param int|string $code The exception code. In PDO, it can be a string with value of SQLSTATE.
17-
* @param Throwable|null $previous The previous throwable used for the exception chaining.
14+
* @param WP_PDO_MySQL_On_SQLite $driver The SQLite driver that originated the exception.
15+
* @param string $message The exception message.
16+
* @param int|string $code The exception code. In PDO, it can be a string with value of SQLSTATE.
17+
* @param Throwable|null $previous The previous throwable used for the exception chaining.
1818
*/
1919
public function __construct(
20-
WP_SQLite_Driver $driver,
20+
WP_PDO_MySQL_On_SQLite $driver,
2121
string $message,
2222
$code = 0,
2323
?Throwable $previous = null
@@ -27,7 +27,7 @@ public function __construct(
2727
$this->driver = $driver;
2828
}
2929

30-
public function getDriver(): WP_SQLite_Driver {
30+
public function getDriver(): WP_PDO_MySQL_On_SQLite {
3131
return $this->driver;
3232
}
3333
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
/*
4+
* The SQLite driver uses PDO. Enable PDO function calls:
5+
* phpcs:disable WordPress.DB.RestrictedClasses.mysql__PDO
6+
*/
7+
8+
/**
9+
* For back compatibility with dependencies that use their own loader scripts
10+
* (e.g., WP CLI SQLite Command), ensure the new PDO-based classes are loaded.
11+
*/
12+
require_once __DIR__ . '/class-wp-pdo-mysql-on-sqlite.php';
13+
14+
/**
15+
* Deprecated: A proxy of the WP_PDO_MySQL_On_SQLite class preserving legacy API.
16+
*
17+
* This is a temporary class to preserve the legacy API for easier transition
18+
* to the new PDO-based API, developed in the "WP_PDO_MySQL_On_SQLite" class.
19+
*/
20+
class WP_SQLite_Driver {
21+
/** @var string */
22+
public $client_info;
23+
24+
/** @var WP_PDO_MySQL_On_SQLite */
25+
private $mysql_on_sqlite_driver;
26+
27+
public function __construct(
28+
WP_SQLite_Connection $connection,
29+
string $database,
30+
int $mysql_version = 80038
31+
) {
32+
$this->mysql_on_sqlite_driver = new WP_PDO_MySQL_On_SQLite( $connection, $database, $mysql_version );
33+
$this->main_db_name = $database;
34+
$this->client_info = $this->mysql_on_sqlite_driver->client_info;
35+
}
36+
37+
public function get_connection(): WP_SQLite_Connection {
38+
return $this->mysql_on_sqlite_driver->get_connection();
39+
}
40+
41+
public function get_sqlite_version(): string {
42+
return $this->mysql_on_sqlite_driver->get_sqlite_version();
43+
}
44+
45+
public function get_saved_driver_version(): string {
46+
return $this->mysql_on_sqlite_driver->get_saved_driver_version();
47+
}
48+
49+
public function is_sql_mode_active( string $mode ): bool {
50+
return $this->mysql_on_sqlite_driver->is_sql_mode_active( $mode );
51+
}
52+
53+
public function get_last_mysql_query(): ?string {
54+
return $this->mysql_on_sqlite_driver->get_last_mysql_query();
55+
}
56+
57+
public function get_last_sqlite_queries(): array {
58+
return $this->mysql_on_sqlite_driver->get_last_sqlite_queries();
59+
}
60+
61+
/** @return int|string */
62+
public function get_insert_id() {
63+
return $this->mysql_on_sqlite_driver->get_insert_id();
64+
}
65+
66+
/**
67+
* @param string $query Full SQL statement string.
68+
* @param int $fetch_mode PDO fetch mode. Default is PDO::FETCH_OBJ.
69+
* @param array ...$fetch_mode_args Additional fetch mode arguments.
70+
*
71+
* @return mixed Return value, depending on the query type.
72+
*
73+
* @throws WP_SQLite_Driver_Exception When the query execution fails.
74+
*/
75+
public function query( string $query, $fetch_mode = PDO::FETCH_OBJ, ...$fetch_mode_args ) {
76+
return $this->mysql_on_sqlite_driver->query( $query, $fetch_mode, ...$fetch_mode_args );
77+
}
78+
79+
public function create_parser( string $query ): WP_MySQL_Parser {
80+
return $this->mysql_on_sqlite_driver->create_parser( $query );
81+
}
82+
83+
/**
84+
* @return mixed
85+
*/
86+
public function get_query_results() {
87+
return $this->mysql_on_sqlite_driver->get_query_results();
88+
}
89+
90+
/**
91+
* @return mixed
92+
*/
93+
public function get_last_return_value() {
94+
return $this->mysql_on_sqlite_driver->get_last_return_value();
95+
}
96+
97+
public function get_last_column_count(): int {
98+
return $this->mysql_on_sqlite_driver->get_last_column_count();
99+
}
100+
101+
public function get_last_column_meta(): array {
102+
return $this->mysql_on_sqlite_driver->get_last_column_meta();
103+
}
104+
105+
public function execute_sqlite_query( string $sql, array $params = array() ): PDOStatement {
106+
return $this->mysql_on_sqlite_driver->execute_sqlite_query( $sql, $params );
107+
}
108+
109+
public function begin_transaction(): void {
110+
$this->mysql_on_sqlite_driver->begin_transaction();
111+
}
112+
113+
public function commit(): void {
114+
$this->mysql_on_sqlite_driver->commit();
115+
}
116+
117+
public function rollback(): void {
118+
$this->mysql_on_sqlite_driver->rollback();
119+
}
120+
121+
/**
122+
* Proxy also the private property "$main_db_name", as it is used in tests.
123+
*/
124+
public function __set( string $name, $value ): void {
125+
if ( 'main_db_name' === $name ) {
126+
$closure = function ( string $value ) {
127+
$this->main_db_name = $value;
128+
};
129+
$closure->call( $this->mysql_on_sqlite_driver, $value );
130+
}
131+
}
132+
133+
/**
134+
* Proxy also this private method, as it is used in tests.
135+
*/
136+
private function quote_mysql_utf8_string_literal( string $utf8_literal ): string {
137+
$closure = function ( string $utf8_literal ) {
138+
return $this->quote_mysql_utf8_string_literal( $utf8_literal );
139+
};
140+
return $closure->call( $this->mysql_on_sqlite_driver, $utf8_literal );
141+
}
142+
}

wp-includes/sqlite-ast/class-wp-sqlite-information-schema-reconstructor.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class WP_SQLite_Information_Schema_Reconstructor {
1919
/**
2020
* The SQLite driver instance.
2121
*
22-
* @var WP_SQLite_Driver
22+
* @var WP_PDO_MySQL_On_SQLite
2323
*/
2424
private $driver;
2525

@@ -40,11 +40,11 @@ class WP_SQLite_Information_Schema_Reconstructor {
4040
/**
4141
* Constructor.
4242
*
43-
* @param WP_SQLite_Driver $driver The SQLite driver instance.
43+
* @param WP_PDO_MySQL_On_SQLite $driver The SQLite driver instance.
4444
* @param WP_SQLite_Information_Schema_Builder $schema_builder The information schema builder instance.
4545
*/
4646
public function __construct(
47-
WP_SQLite_Driver $driver,
47+
$driver,
4848
WP_SQLite_Information_Schema_Builder $schema_builder
4949
) {
5050
$this->driver = $driver;
@@ -137,7 +137,7 @@ private function get_sqlite_table_names(): array {
137137
array(
138138
'_mysql_data_types_cache',
139139
'sqlite\_%',
140-
str_replace( '_', '\_', WP_SQLite_Driver::RESERVED_PREFIX ) . '%',
140+
str_replace( '_', '\_', WP_PDO_MySQL_On_SQLite::RESERVED_PREFIX ) . '%',
141141
)
142142
)->fetchAll( PDO::FETCH_COLUMN );
143143
}
@@ -692,9 +692,9 @@ private function get_mysql_column_type( string $column_type ): string {
692692
/**
693693
* Format a MySQL UTF-8 string literal for output in a CREATE TABLE statement.
694694
*
695-
* See WP_SQLite_Driver::quote_mysql_utf8_string_literal().
695+
* See WP_PDO_MySQL_On_SQLite::quote_mysql_utf8_string_literal().
696696
*
697-
* TODO: This is a copy of WP_SQLite_Driver::quote_mysql_utf8_string_literal().
697+
* TODO: This is a copy of WP_PDO_MySQL_On_SQLite::quote_mysql_utf8_string_literal().
698698
* We may consider extracing it to reusable MySQL helpers.
699699
*
700700
* @param string $utf8_literal The UTF-8 string literal to escape.

wp-pdo-mysql-on-sqlite.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919
require_once __DIR__ . '/wp-includes/sqlite-ast/class-wp-sqlite-information-schema-builder.php';
2020
require_once __DIR__ . '/wp-includes/sqlite-ast/class-wp-sqlite-information-schema-exception.php';
2121
require_once __DIR__ . '/wp-includes/sqlite-ast/class-wp-sqlite-information-schema-reconstructor.php';
22+
require_once __DIR__ . '/wp-includes/sqlite-ast/class-wp-pdo-mysql-on-sqlite.php';

0 commit comments

Comments
 (0)