| 1 |
<?php |
| 2 |
/** |
| 3 |
* Extend and replace the wpdb class. |
| 4 |
* |
| 5 |
* @package wp-sqlite-integration |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* This class extends wpdb and replaces it. |
| 11 |
* |
| 12 |
* It also rewrites some methods that use mysql specific functions. |
| 13 |
*/ |
| 14 |
class WP_SQLite_DB extends wpdb { |
| 15 |
|
| 16 |
/** |
| 17 |
* Database Handle |
| 18 |
* |
| 19 |
* @var WP_SQLite_Translator |
| 20 |
*/ |
| 21 |
protected $dbh; |
| 22 |
|
| 23 |
/** |
| 24 |
* Constructor |
| 25 |
* |
| 26 |
* Unlike wpdb, no credentials are needed. |
| 27 |
*/ |
| 28 |
public function __construct() { |
| 29 |
parent::__construct( '', '', '', '' ); |
| 30 |
$this->charset = 'utf8mb4'; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Method to set character set for the database. |
| 35 |
* |
| 36 |
* This overrides wpdb::set_charset(), only to dummy out the MySQL function. |
| 37 |
* |
| 38 |
* @see wpdb::set_charset() |
| 39 |
* |
| 40 |
* @param resource $dbh The resource given by mysql_connect. |
| 41 |
* @param string $charset Optional. The character set. Default null. |
| 42 |
* @param string $collate Optional. The collation. Default null. |
| 43 |
*/ |
| 44 |
public function set_charset( $dbh, $charset = null, $collate = null ) { |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Method to get the character set for the database. |
| 49 |
* Hardcoded to utf8mb4 for now. |
| 50 |
* |
| 51 |
* @param string $table The table name. |
| 52 |
* @param string $column The column name. |
| 53 |
* |
| 54 |
* @return string The character set. |
| 55 |
*/ |
| 56 |
public function get_col_charset( $table, $column ) { |
| 57 |
// Hardcoded for now. |
| 58 |
return 'utf8mb4'; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Method to dummy out wpdb::set_sql_mode() |
| 63 |
* |
| 64 |
* @see wpdb::set_sql_mode() |
| 65 |
* |
| 66 |
* @param array $modes Optional. A list of SQL modes to set. |
| 67 |
*/ |
| 68 |
public function set_sql_mode( $modes = array() ) { |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Closes the current database connection. |
| 73 |
* Noop in SQLite. |
| 74 |
* |
| 75 |
* @return bool True to indicate the connection was successfully closed. |
| 76 |
*/ |
| 77 |
public function close() { |
| 78 |
return true; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Method to select the database connection. |
| 83 |
* |
| 84 |
* This overrides wpdb::select(), only to dummy out the MySQL function. |
| 85 |
* |
| 86 |
* @see wpdb::select() |
| 87 |
* |
| 88 |
* @param string $db MySQL database name. Not used. |
| 89 |
* @param resource|null $dbh Optional link identifier. |
| 90 |
*/ |
| 91 |
public function select( $db, $dbh = null ) { |
| 92 |
$this->ready = true; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Method to escape characters. |
| 97 |
* |
| 98 |
* This overrides wpdb::_real_escape() to avoid using mysql_real_escape_string(). |
| 99 |
* |
| 100 |
* @see wpdb::_real_escape() |
| 101 |
* |
| 102 |
* @param string $str The string to escape. |
| 103 |
* |
| 104 |
* @return string escaped |
| 105 |
*/ |
| 106 |
public function _real_escape( $str ) { |
| 107 |
return addslashes( $str ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Method to dummy out wpdb::esc_like() function. |
| 112 |
* |
| 113 |
* WordPress 4.0.0 introduced esc_like() function that adds backslashes to %, |
| 114 |
* underscore and backslash, which is not interpreted as escape character |
| 115 |
* by SQLite. So we override it and dummy out this function. |
| 116 |
* |
| 117 |
* @param string $text The raw text to be escaped. The input typed by the user should have no |
| 118 |
* extra or deleted slashes. |
| 119 |
* |
| 120 |
* @return string Text in the form of a LIKE phrase. The output is not SQL safe. Call $wpdb::prepare() |
| 121 |
* or real_escape next. |
| 122 |
*/ |
| 123 |
public function esc_like( $text ) { |
| 124 |
return $text; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Method to put out the error message. |
| 129 |
* |
| 130 |
* This overrides wpdb::print_error(), for we can't use the parent class method. |
| 131 |
* |
| 132 |
* @see wpdb::print_error() |
| 133 |
* |
| 134 |
* @global array $EZSQL_ERROR Stores error information of query and error string. |
| 135 |
* |
| 136 |
* @param string $str The error to display. |
| 137 |
* |
| 138 |
* @return bool|void False if the showing of errors is disabled. |
| 139 |
*/ |
| 140 |
public function print_error( $str = '' ) { |
| 141 |
global $EZSQL_ERROR; |
| 142 |
|
| 143 |
if ( ! $str ) { |
| 144 |
$err = $this->dbh->get_error_message() ? $this->dbh->get_error_message() : ''; |
| 145 |
$str = empty( $err ) ? '' : $err[2]; |
| 146 |
} |
| 147 |
$EZSQL_ERROR[] = array( |
| 148 |
'query' => $this->last_query, |
| 149 |
'error_str' => $str, |
| 150 |
); |
| 151 |
|
| 152 |
if ( $this->suppress_errors ) { |
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
wp_load_translations_early(); |
| 157 |
|
| 158 |
$caller = $this->get_caller(); |
| 159 |
$caller = $caller ? $caller : '(unknown)'; |
| 160 |
|
| 161 |
$error_str = sprintf( |
| 162 |
'WordPress database error %1$s for query %2$s made by %3$s', |
| 163 |
$str, |
| 164 |
$this->last_query, |
| 165 |
$caller |
| 166 |
); |
| 167 |
|
| 168 |
error_log( $error_str ); |
| 169 |
|
| 170 |
if ( ! $this->show_errors ) { |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
if ( is_multisite() ) { |
| 175 |
$msg = "WordPress database error: [$str]\n{$this->last_query}\n"; |
| 176 |
if ( defined( 'ERRORLOGFILE' ) ) { |
| 177 |
error_log( $msg, 3, ERRORLOGFILE ); |
| 178 |
} |
| 179 |
if ( defined( 'DIEONDBERROR' ) ) { |
| 180 |
wp_die( $msg ); |
| 181 |
} |
| 182 |
} else { |
| 183 |
$str = htmlspecialchars( $str, ENT_QUOTES ); |
| 184 |
$query = htmlspecialchars( $this->last_query, ENT_QUOTES ); |
| 185 |
|
| 186 |
printf( |
| 187 |
'<div id="error"><p class="wpdberror">WordPress database error: [%1$s] %2$s</p></div>', |
| 188 |
$str, |
| 189 |
'<code>' . $query . '</code>' |
| 190 |
); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Method to flush cached data. |
| 196 |
* |
| 197 |
* This overrides wpdb::flush(). This is not necessarily overridden, because |
| 198 |
* $result will never be resource. |
| 199 |
* |
| 200 |
* @see wpdb::flush |
| 201 |
*/ |
| 202 |
public function flush() { |
| 203 |
$this->last_result = array(); |
| 204 |
$this->col_info = null; |
| 205 |
$this->last_query = null; |
| 206 |
$this->rows_affected = 0; |
| 207 |
$this->num_rows = 0; |
| 208 |
$this->last_error = ''; |
| 209 |
$this->result = null; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Method to do the database connection. |
| 214 |
* |
| 215 |
* This overrides wpdb::db_connect() to avoid using MySQL function. |
| 216 |
* |
| 217 |
* @see wpdb::db_connect() |
| 218 |
* |
| 219 |
* @param bool $allow_bail Not used. |
| 220 |
* @return void |
| 221 |
*/ |
| 222 |
public function db_connect( $allow_bail = true ) { |
| 223 |
if ( $this->dbh ) { |
| 224 |
return; |
| 225 |
} |
| 226 |
$this->init_charset(); |
| 227 |
|
| 228 |
$pdo = null; |
| 229 |
if ( isset( $GLOBALS['@pdo'] ) ) { |
| 230 |
$pdo = $GLOBALS['@pdo']; |
| 231 |
} |
| 232 |
$this->dbh = new WP_SQLite_Translator( $pdo ); |
| 233 |
$this->last_error = $this->dbh->get_error_message(); |
| 234 |
if ( $this->last_error ) { |
| 235 |
return false; |
| 236 |
} |
| 237 |
$GLOBALS['@pdo'] = $this->dbh->get_pdo(); |
| 238 |
$this->ready = true; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Method to dummy out wpdb::check_connection() |
| 243 |
* |
| 244 |
* @param bool $allow_bail Not used. |
| 245 |
* |
| 246 |
* @return bool |
| 247 |
*/ |
| 248 |
public function check_connection( $allow_bail = true ) { |
| 249 |
return true; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Method to execute the query. |
| 254 |
* |
| 255 |
* This overrides wpdb::query(). In fact, this method does all the database |
| 256 |
* access jobs. |
| 257 |
* |
| 258 |
* @see wpdb::query() |
| 259 |
* |
| 260 |
* @param string $query Database query. |
| 261 |
* |
| 262 |
* @return int|false Number of rows affected/selected or false on error |
| 263 |
*/ |
| 264 |
public function query( $query ) { |
| 265 |
if ( ! $this->ready ) { |
| 266 |
return false; |
| 267 |
} |
| 268 |
|
| 269 |
$query = apply_filters( 'query', $query ); |
| 270 |
|
| 271 |
$this->flush(); |
| 272 |
|
| 273 |
$this->func_call = "\$db->query(\"$query\")"; |
| 274 |
$this->last_query = $query; |
| 275 |
|
| 276 |
if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) { |
| 277 |
$this->timer_start(); |
| 278 |
} |
| 279 |
|
| 280 |
$this->result = $this->dbh->query( $query ); |
| 281 |
++$this->num_queries; |
| 282 |
|
| 283 |
if ( defined( 'SAVEQUERIES' ) && SAVEQUERIES ) { |
| 284 |
$this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() ); |
| 285 |
} |
| 286 |
|
| 287 |
$this->last_error = $this->dbh->get_error_message(); |
| 288 |
if ( $this->last_error ) { |
| 289 |
$this->print_error( $this->last_error ); |
| 290 |
return false; |
| 291 |
} |
| 292 |
|
| 293 |
if ( preg_match( '/^\\s*(set|create|alter|truncate|drop|optimize)\\s*/i', $query ) ) { |
| 294 |
return $this->dbh->get_return_value(); |
| 295 |
} |
| 296 |
|
| 297 |
if ( preg_match( '/^\\s*(insert|delete|update|replace)\s/i', $query ) ) { |
| 298 |
$this->rows_affected = $this->dbh->get_affected_rows(); |
| 299 |
if ( preg_match( '/^\s*(insert|replace)\s/i', $query ) ) { |
| 300 |
$this->insert_id = $this->dbh->get_insert_id(); |
| 301 |
} |
| 302 |
return $this->rows_affected; |
| 303 |
} |
| 304 |
|
| 305 |
$this->last_result = $this->dbh->get_query_results(); |
| 306 |
$this->num_rows = $this->dbh->get_num_rows(); |
| 307 |
return $this->num_rows; |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Method to set the class variable $col_info. |
| 312 |
* |
| 313 |
* This overrides wpdb::load_col_info(), which uses a mysql function. |
| 314 |
* |
| 315 |
* @see wpdb::load_col_info() |
| 316 |
*/ |
| 317 |
protected function load_col_info() { |
| 318 |
if ( $this->col_info ) { |
| 319 |
return; |
| 320 |
} |
| 321 |
$this->col_info = $this->dbh->get_columns(); |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* Method to return what the database can do. |
| 326 |
* |
| 327 |
* This overrides wpdb::has_cap() to avoid using MySQL functions. |
| 328 |
* SQLite supports subqueries, but not support collation, group_concat and set_charset. |
| 329 |
* |
| 330 |
* @see wpdb::has_cap() |
| 331 |
* |
| 332 |
* @param string $db_cap The feature to check for. Accepts 'collation', |
| 333 |
* 'group_concat', 'subqueries', 'set_charset', |
| 334 |
* 'utf8mb4', or 'utf8mb4_520'. |
| 335 |
* |
| 336 |
* @return bool Whether the database feature is supported, false otherwise. |
| 337 |
*/ |
| 338 |
public function has_cap( $db_cap ) { |
| 339 |
return 'subqueries' === strtolower( $db_cap ); |
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* Method to return database version number. |
| 344 |
* |
| 345 |
* This overrides wpdb::db_version() to avoid using MySQL function. |
| 346 |
* It returns mysql version number, but it means nothing for SQLite. |
| 347 |
* So it return the newest mysql version. |
| 348 |
* |
| 349 |
* @see wpdb::db_version() |
| 350 |
*/ |
| 351 |
public function db_version() { |
| 352 |
return '8.0'; |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* Retrieves full database server information. |
| 357 |
* |
| 358 |
* @return string|false Server info on success, false on failure. |
| 359 |
*/ |
| 360 |
public function db_server_info() { |
| 361 |
return SQLite3::version()['versionString']; |
| 362 |
} |
| 363 |
} |
| 364 |
|