PluginProbe
SQLite Database Integration / 2.2.23
SQLite Database Integration v2.2.23
3.0.2 3.0.1 trunk 2.1.13 2.1.14 2.1.15 2.1.16 2.2.0 2.2.1 2.2.10 2.2.11 2.2.12 2.2.13 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.2.2 2.2.20 2.2.21 2.2.22 2.2.23 2.2.3 All 32 releases
sqlite-database-integration / wp-includes / database / sqlite / class-wp-sqlite-driver.php

class-wp-sqlite-driver.php in SQLite Database Integration 2.2.23, at wp-includes/database/sqlite/class-wp-sqlite-driver.php

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