|
| 1 | +<?php |
| 2 | + |
| 3 | +define( 'WP_DEBUG', false ); |
| 4 | + |
| 5 | +require_once __DIR__ . '/../../../version.php'; |
| 6 | +require_once __DIR__ . '/../../../wp-includes/parser/class-wp-parser-grammar.php'; |
| 7 | +require_once __DIR__ . '/../../../wp-includes/parser/class-wp-parser.php'; |
| 8 | +require_once __DIR__ . '/../../../wp-includes/parser/class-wp-parser-node.php'; |
| 9 | +require_once __DIR__ . '/../../../wp-includes/parser/class-wp-parser-token.php'; |
| 10 | +require_once __DIR__ . '/../../../wp-includes/mysql/class-wp-mysql-token.php'; |
| 11 | +require_once __DIR__ . '/../../../wp-includes/mysql/class-wp-mysql-lexer.php'; |
| 12 | +require_once __DIR__ . '/../../../wp-includes/mysql/class-wp-mysql-parser.php'; |
| 13 | +require_once __DIR__ . '/../../../wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php'; |
| 14 | +require_once __DIR__ . '/../../../wp-includes/sqlite-ast/class-wp-sqlite-connection.php'; |
| 15 | +require_once __DIR__ . '/../../../wp-includes/sqlite-ast/class-wp-sqlite-configurator.php'; |
| 16 | +require_once __DIR__ . '/../../../wp-includes/sqlite-ast/class-wp-sqlite-driver.php'; |
| 17 | +require_once __DIR__ . '/../../../wp-includes/sqlite-ast/class-wp-sqlite-driver-exception.php'; |
| 18 | +require_once __DIR__ . '/../../../wp-includes/sqlite-ast/class-wp-sqlite-information-schema-builder.php'; |
| 19 | +require_once __DIR__ . '/../../../wp-includes/sqlite-ast/class-wp-sqlite-information-schema-exception.php'; |
| 20 | +require_once __DIR__ . '/../../../wp-includes/sqlite-ast/class-wp-sqlite-information-schema-reconstructor.php'; |
| 21 | + |
| 22 | +class SQLiteTranslationHandler implements MySQLQueryHandler { |
| 23 | + /** @var WP_SQLite_Driver */ |
| 24 | + private $sqlite_driver; |
| 25 | + |
| 26 | + public function __construct($sqlite_database_path) { |
| 27 | + define('FQDB', $sqlite_database_path); |
| 28 | + define('FQDBDIR', dirname(FQDB) . '/'); |
| 29 | + |
| 30 | + $this->sqlite_driver = new WP_SQLite_Driver( |
| 31 | + new WP_SQLite_Connection( array( 'path' => $sqlite_database_path ) ), |
| 32 | + 'wordpress' |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + public function handleQuery(string $query): MySQLServerQueryResult { |
| 37 | + try { |
| 38 | + $rows = $this->sqlite_driver->query($query); |
| 39 | + if ( $this->sqlite_driver->get_last_column_count() > 0 ) { |
| 40 | + $columns = $this->computeColumnInfo(); |
| 41 | + return new SelectQueryResult($columns, $rows); |
| 42 | + } |
| 43 | + return new OkayPacketResult( |
| 44 | + $this->sqlite_driver->get_last_return_value() ?? 0, |
| 45 | + $this->sqlite_driver->get_insert_id() ?? 0 |
| 46 | + ); |
| 47 | + } catch (Throwable $e) { |
| 48 | + return new ErrorQueryResult($e->getMessage()); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public function computeColumnInfo() { |
| 53 | + $columns = []; |
| 54 | + |
| 55 | + $column_meta = $this->sqlite_driver->get_last_column_meta(); |
| 56 | + |
| 57 | + $types = [ |
| 58 | + 'DECIMAL' => MySQLProtocol::FIELD_TYPE_DECIMAL, |
| 59 | + 'TINY' => MySQLProtocol::FIELD_TYPE_TINY, |
| 60 | + 'SHORT' => MySQLProtocol::FIELD_TYPE_SHORT, |
| 61 | + 'LONG' => MySQLProtocol::FIELD_TYPE_LONG, |
| 62 | + 'FLOAT' => MySQLProtocol::FIELD_TYPE_FLOAT, |
| 63 | + 'DOUBLE' => MySQLProtocol::FIELD_TYPE_DOUBLE, |
| 64 | + 'NULL' => MySQLProtocol::FIELD_TYPE_NULL, |
| 65 | + 'TIMESTAMP' => MySQLProtocol::FIELD_TYPE_TIMESTAMP, |
| 66 | + 'LONGLONG' => MySQLProtocol::FIELD_TYPE_LONGLONG, |
| 67 | + 'INT24' => MySQLProtocol::FIELD_TYPE_INT24, |
| 68 | + 'DATE' => MySQLProtocol::FIELD_TYPE_DATE, |
| 69 | + 'TIME' => MySQLProtocol::FIELD_TYPE_TIME, |
| 70 | + 'DATETIME' => MySQLProtocol::FIELD_TYPE_DATETIME, |
| 71 | + 'YEAR' => MySQLProtocol::FIELD_TYPE_YEAR, |
| 72 | + 'NEWDATE' => MySQLProtocol::FIELD_TYPE_NEWDATE, |
| 73 | + 'VARCHAR' => MySQLProtocol::FIELD_TYPE_VARCHAR, |
| 74 | + 'BIT' => MySQLProtocol::FIELD_TYPE_BIT, |
| 75 | + 'NEWDECIMAL' => MySQLProtocol::FIELD_TYPE_NEWDECIMAL, |
| 76 | + 'ENUM' => MySQLProtocol::FIELD_TYPE_ENUM, |
| 77 | + 'SET' => MySQLProtocol::FIELD_TYPE_SET, |
| 78 | + 'TINY_BLOB' => MySQLProtocol::FIELD_TYPE_TINY_BLOB, |
| 79 | + 'MEDIUM_BLOB' => MySQLProtocol::FIELD_TYPE_MEDIUM_BLOB, |
| 80 | + 'LONG_BLOB' => MySQLProtocol::FIELD_TYPE_LONG_BLOB, |
| 81 | + 'BLOB' => MySQLProtocol::FIELD_TYPE_BLOB, |
| 82 | + 'VAR_STRING' => MySQLProtocol::FIELD_TYPE_VAR_STRING, |
| 83 | + 'STRING' => MySQLProtocol::FIELD_TYPE_STRING, |
| 84 | + 'GEOMETRY' => MySQLProtocol::FIELD_TYPE_GEOMETRY, |
| 85 | + ]; |
| 86 | + |
| 87 | + foreach ($column_meta as $column) { |
| 88 | + $type = $types[$column['native_type']] ?? null; |
| 89 | + if ( null === $type ) { |
| 90 | + throw new Exception('Unknown column type: ' . $column['native_type']); |
| 91 | + } |
| 92 | + $columns[] = [ |
| 93 | + 'name' => $column['name'], |
| 94 | + 'length' => $column['len'], |
| 95 | + 'type' => $type, |
| 96 | + 'flags' => 129, |
| 97 | + 'decimals' => $column['precision'] |
| 98 | + ]; |
| 99 | + } |
| 100 | + return $columns; |
| 101 | + } |
| 102 | +} |
0 commit comments