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