Repository navigation
Expand file tree
/
Copy pathclass-wp-sqlite-driver.php
More file actions
280 lines (252 loc) · 7.67 KB
/
Copy pathclass-wp-sqlite-driver.php
File metadata and controls
280 lines (252 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
/*
* The SQLite driver uses PDO. Enable PDO function calls:
* phpcs:disable WordPress.DB.RestrictedClasses.mysql__PDO
*/
/**
* For back compatibility with dependencies that use their own loader scripts
* (e.g., WP CLI SQLite Command), ensure the PDO-based classes are loaded.
*/
require_once __DIR__ . '/class-wp-mysql-on-sqlite.php';
require_once __DIR__ . '/class-wp-pdo-proxy-statement.php';
/**
* Deprecated: A proxy of the WP_MySQL_On_SQLite class preserving the legacy API.
*
* This class temporarily preserves the legacy constructor and result API while
* consumers transition to the PDO-based WP_MySQL_On_SQLite API.
*/
class WP_SQLite_Driver {
/**
* The SQLite engine version.
*
* This is a mysqli-like property that is needed to avoid a PHP warning in
* the WordPress health info. The "WP_Debug_Data::get_wp_database()" method
* calls "$wpdb->dbh->client_info" - a mysqli-specific abstraction leak.
*
* @TODO: This should be fixed in WordPress core.
*
* See:
* https://github.com/WordPress/wordpress-develop/blob/bcdca3f9925f1d3eca7b78d231837c0caf0c8c24/src/wp-admin/includes/class-wp-debug-data.php#L1579
*
* @var string
*/
public $client_info;
/**
* The MySQL-on-SQLite driver instance.
*
* @var WP_MySQL_On_SQLite
*/
private $mysql_on_sqlite_driver;
/**
* Results of the last emulated query.
*
* @var mixed
*/
private $last_result;
/**
* Constructor.
*
* Set up an SQLite connection and the MySQL-on-SQLite driver.
*
* @param WP_SQLite_Connection $connection A SQLite database connection.
* @param string $database The database name.
*
* @throws WP_SQLite_Driver_Exception When the driver initialization fails.
*/
public function __construct(
WP_SQLite_Connection $connection,
string $database,
int $mysql_version = 80038
) {
$this->mysql_on_sqlite_driver = new WP_MySQL_On_SQLite(
sprintf( 'mysql-on-sqlite:dbname=%s', $database ),
null,
null,
array(
'mysql_version' => $mysql_version,
'pdo' => $connection->get_pdo(),
'journal_mode' => $connection->query( 'PRAGMA journal_mode' )->fetchColumn(),
)
);
$this->main_db_name = $database;
$this->client_info = $this->mysql_on_sqlite_driver->client_info;
$connection->get_pdo()->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true );
}
/**
* Get the SQLite connection instance.
*
* @return WP_SQLite_Connection
*/
public function get_connection(): WP_SQLite_Connection {
return $this->mysql_on_sqlite_driver->get_connection();
}
/**
* Get the version of the SQLite engine.
*
* @return string SQLite engine version as a string.
*/
public function get_sqlite_version(): string {
return $this->mysql_on_sqlite_driver->get_sqlite_version();
}
/**
* Get the SQLite driver version saved in the database.
*
* The saved driver version corresponds to the latest version of the SQLite
* driver that was used to initialize and configure the SQLite database.
*
* @return string SQLite driver version as a string.
* @throws PDOException When the query execution fails.
*/
public function get_saved_driver_version(): string {
return $this->mysql_on_sqlite_driver->get_saved_driver_version();
}
/**
* Check if a specific SQL mode is active.
*
* @param string $mode The SQL mode to check.
* @return bool True if the SQL mode is active, false otherwise.
*/
public function is_sql_mode_active( string $mode ): bool {
return $this->mysql_on_sqlite_driver->is_sql_mode_active( $mode );
}
/**
* Get the last executed MySQL query.
*
* @return string|null
*/
public function get_last_mysql_query(): ?string {
return $this->mysql_on_sqlite_driver->get_last_mysql_query();
}
/**
* Get SQLite queries executed for the last MySQL query.
*
* @return array{ sql: string, params: array }[]
*/
public function get_last_sqlite_queries(): array {
return $this->mysql_on_sqlite_driver->get_last_sqlite_queries();
}
/**
* Get the auto-increment value generated for the last query.
*
* @return int|string
*/
public function get_insert_id() {
return $this->mysql_on_sqlite_driver->get_insert_id();
}
/**
* @param string $query Full SQL statement string.
* @param int $fetch_mode PDO fetch mode. Default is PDO::FETCH_OBJ.
* @param array ...$fetch_mode_args Additional fetch mode arguments.
*
* @return mixed Return value, depending on the query type.
*
* @throws WP_SQLite_Driver_Exception When the query execution fails.
*/
public function query( string $query, $fetch_mode = PDO::FETCH_OBJ, ...$fetch_mode_args ) {
$stmt = $this->mysql_on_sqlite_driver->query( $query, $fetch_mode, ...$fetch_mode_args );
if ( $stmt->columnCount() > 0 ) {
$this->last_result = $stmt->fetchAll( $fetch_mode );
} else {
$this->last_result = $stmt->rowCount();
}
return $this->last_result;
}
/**
* Tokenize a MySQL query and initialize a parser.
*
* @param string $query The MySQL query to parse.
* @return WP_MySQL_Parser A parser initialized for the MySQL query.
*/
public function create_parser( string $query ): WP_MySQL_Parser {
return $this->mysql_on_sqlite_driver->create_parser( $query );
}
/**
* Get results of the last query.
*
* @return mixed
*/
public function get_query_results() {
return $this->last_result;
}
/**
* Get return value of the last query() function call.
*
* @return mixed
*/
public function get_last_return_value() {
return $this->last_result;
}
/**
* Get the number of columns returned by the last emulated query.
*
* @return int
*/
public function get_last_column_count(): int {
return $this->mysql_on_sqlite_driver->get_last_column_count();
}
/**
* Get column metadata for results of the last emulated query.
*
* @return array
*/
public function get_last_column_meta(): array {
return $this->mysql_on_sqlite_driver->get_last_column_meta();
}
/**
* Execute a query in SQLite.
*
* @param string $sql The query to execute.
* @param array $params The query parameters.
* @throws PDOException When the query execution fails.
* @return PDOStatement The PDO statement object.
*/
public function execute_sqlite_query( string $sql, array $params = array() ): PDOStatement {
return $this->mysql_on_sqlite_driver->execute_sqlite_query( $sql, $params );
}
/**
* Begin a new transaction or nested transaction.
*/
public function beginTransaction(): void { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
$this->mysql_on_sqlite_driver->beginTransaction();
}
/**
* A temporary alias for back compatibility.
*
* @see self::beginTransaction()
*/
public function begin_transaction(): void {
$this->beginTransaction();
}
/**
* Commit the current transaction or nested transaction.
*/
public function commit(): void {
$this->mysql_on_sqlite_driver->commit();
}
/**
* Rollback the current transaction or nested transaction.
*/
public function rollback(): void {
$this->mysql_on_sqlite_driver->rollback();
}
/**
* Proxy also the private property "$main_db_name", as it is used in tests.
*/
public function __set( string $name, $value ): void {
if ( 'main_db_name' === $name ) {
$closure = function ( string $value ) {
$this->main_db_name = $value;
};
$closure->call( $this->mysql_on_sqlite_driver, $value );
}
}
/**
* Proxy also this private method, as it is used in tests.
*/
private function quote_mysql_utf8_string_literal( string $utf8_literal ): string {
$closure = function ( string $utf8_literal ) {
return $this->quote_mysql_utf8_string_literal( $utf8_literal );
};
return $closure->call( $this->mysql_on_sqlite_driver, $utf8_literal );
}
}