Skip to content

Commit 8767a36

Browse files
authored
Finalize MySQL-on-SQLite 3.0 refinements (#482)
## Summary This completes four API and compatibility refinements before 3.0: - Prevents `PDOStatement::execute()` from bypassing MySQL emulation. Marking it not implemented for now. - Normalizes missing table failures to the MySQL-compatible SQLSTATE, error code, and message. - Marks the legacy database constants as deprecated. - Removes `ext-filter` dependency when reconstructing hexadecimal defaults.
1 parent 8354291 commit 8767a36

8 files changed

Lines changed: 153 additions & 44 deletions

‎packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite-statement.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,10 @@ public function __construct(
208208
* @return bool True on success, false on failure.
209209
*/
210210
public function execute( $params = null ): bool {
211-
return $this->statement->execute( $params );
211+
// The wrapped SQLite statement represents the result of MySQL emulation.
212+
// Re-executing it would not repeat the original MySQL operation.
213+
// TODO: Implement statement execution together with the prepare() flow.
214+
throw new RuntimeException( 'Not implemented' );
212215
}
213216

214217
/**
@@ -340,7 +343,7 @@ public function nextRowset(): bool {
340343
}
341344

342345
/**
343-
* Closes the cursor, enabling the statement to be executed again.
346+
* Close the cursor and release the associated resources.
344347
*
345348
* @return bool True on success, false on failure.
346349
*/

‎packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php‎

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,11 +1191,15 @@ public function query( string $query, ?int $fetch_mode = null, ...$fetch_mode_ar
11911191
if ( $e instanceof WP_SQLite_Information_Schema_Exception ) {
11921192
$e = $this->convert_information_schema_exception( $e );
11931193
}
1194-
if ( ! ( $e instanceof WP_MySQL_On_SQLite_Exception ) ) {
1195-
$e = $this->new_driver_exception( $e->getMessage(), $e->getCode(), $e );
1194+
if ( $e instanceof WP_MySQL_On_SQLite_Exception ) {
1195+
$driver_exception = $e;
1196+
} elseif ( $e instanceof PDOException ) {
1197+
$driver_exception = $this->convert_sqlite_exception( $e );
1198+
} else {
1199+
$driver_exception = $this->new_driver_exception( $e->getMessage(), $e->getCode(), $e );
11961200
}
11971201

1198-
return $this->handle_pdo_error( $e );
1202+
return $this->handle_pdo_error( $driver_exception );
11991203
} finally {
12001204
// A query that doesn't return any rows or fails sets found rows to 0.
12011205
if ( ! $this->is_readonly || isset( $e ) ) {
@@ -2192,9 +2196,9 @@ private function execute_transaction_or_locking_statement( WP_Parser_Node $node
21922196
sprintf( 'SELECT 1 FROM %s LIMIT 0', $table_name )
21932197
);
21942198
} catch ( PDOException $e ) {
2195-
throw $this->new_driver_exception(
2196-
sprintf( "Table '%s.%s' doesn't exist", $this->db_name, $table_name ),
2197-
'42S02'
2199+
throw $this->new_table_not_found_exception(
2200+
sprintf( '%s.%s', $this->db_name, $table_name ),
2201+
$e
21982202
);
21992203
}
22002204
}
@@ -3650,10 +3654,7 @@ private function execute_show_columns_statement( WP_Parser_Node $node ): void {
36503654
)->fetchColumn();
36513655

36523656
if ( ! $table_exists ) {
3653-
throw $this->new_driver_exception(
3654-
sprintf( "Table '%s.%s' doesn't exist", $database, $table_name ),
3655-
'42S02'
3656-
);
3657+
throw $this->new_table_not_found_exception( sprintf( '%s.%s', $database, $table_name ) );
36573658
}
36583659

36593660
// LIKE and WHERE clauses.
@@ -4072,7 +4073,9 @@ private function execute_administration_statement( WP_Parser_Node $node ): void
40724073
);
40734074
}
40744075
} catch ( PDOException $e ) {
4075-
if ( 'HY000' === $e->getCode() ) {
4076+
if ( '42S02' === $e->getCode() && isset( $e->errorInfo[2] ) ) {
4077+
$errors = array( $e->errorInfo[2] );
4078+
} elseif ( 'HY000' === $e->getCode() ) {
40764079
$errors = array( "Table '$table_name' doesn't exist" );
40774080
} else {
40784081
$errors = array( $e->getMessage() );
@@ -5774,13 +5777,7 @@ private function translate_insert_or_replace_body(
57745777

57755778
// Check if the table exists.
57765779
if ( 0 === count( $columns ) ) {
5777-
throw $this->new_driver_exception(
5778-
sprintf(
5779-
"SQLSTATE[42S02]: Base table or view not found: 1146 Table '%s' doesn't exist",
5780-
$table_name
5781-
),
5782-
'42S02'
5783-
);
5780+
throw $this->new_table_not_found_exception( $table_name );
57845781
}
57855782

57865783
// Get a list of columns that are targeted by the INSERT or REPLACE query.
@@ -6088,13 +6085,7 @@ private function translate_update_list( string $table_name, WP_Parser_Node $pare
60886085

60896086
// Check if the table exists.
60906087
if ( 0 === count( $columns ) ) {
6091-
throw $this->new_driver_exception(
6092-
sprintf(
6093-
"SQLSTATE[42S02]: Base table or view not found: 1146 Table '%s' doesn't exist",
6094-
$table_name
6095-
),
6096-
'42S02'
6097-
);
6088+
throw $this->new_table_not_found_exception( $table_name );
60986089
}
60996090

61006091
$column_map = array_combine( array_column( $columns, 'COLUMN_NAME' ), $columns );
@@ -6833,10 +6824,7 @@ private function get_sqlite_create_table_statement(
68336824
)->fetch( PDO::FETCH_ASSOC );
68346825

68356826
if ( false === $table_info ) {
6836-
throw $this->new_driver_exception(
6837-
sprintf( "Table '%s' doesn't exist", $table_name ),
6838-
'42S02'
6839-
);
6827+
throw $this->new_table_not_found_exception( $table_name );
68406828
}
68416829

68426830
// 2. Get column info.
@@ -7808,6 +7796,26 @@ private function new_not_supported_exception( string $cause ): WP_MySQL_On_SQLit
78087796
);
78097797
}
78107798

7799+
/**
7800+
* Create a MySQL-compatible table-not-found exception.
7801+
*
7802+
* @param string $table_name The missing table name.
7803+
* @param Throwable|null $previous The previous exception.
7804+
* @return WP_MySQL_On_SQLite_Exception
7805+
*/
7806+
private function new_table_not_found_exception(
7807+
string $table_name,
7808+
?Throwable $previous = null
7809+
): WP_MySQL_On_SQLite_Exception {
7810+
$driver_message = sprintf( "Table '%s' doesn't exist", $table_name );
7811+
return $this->new_driver_exception(
7812+
'SQLSTATE[42S02]: Base table or view not found: 1146 ' . $driver_message,
7813+
'42S02',
7814+
$previous,
7815+
array( '42S02', 1146, $driver_message )
7816+
);
7817+
}
7818+
78117819
/**
78127820
* Create a MySQL-compatible exception for an invalid SQL mode value.
78137821
*
@@ -7836,6 +7844,25 @@ private function new_access_denied_to_information_schema_exception(): WP_MySQL_O
78367844
);
78377845
}
78387846

7847+
/**
7848+
* Convert a SQLite PDO exception to a MySQL-on-SQLite driver exception.
7849+
*
7850+
* @param PDOException $e The SQLite PDO exception.
7851+
* @return WP_MySQL_On_SQLite_Exception
7852+
*/
7853+
private function convert_sqlite_exception( PDOException $e ): WP_MySQL_On_SQLite_Exception {
7854+
$error_info = is_array( $e->errorInfo ) ? $e->errorInfo : array();
7855+
$sqlstate = $error_info[0] ?? null;
7856+
$error_message = $error_info[2] ?? null;
7857+
7858+
if ( 'HY000' === $sqlstate && 0 === strpos( $error_message, 'no such table: ' ) ) {
7859+
$table_name = substr( $error_message, strlen( 'no such table: ' ) );
7860+
return $this->new_table_not_found_exception( $table_name, $e );
7861+
}
7862+
7863+
return $this->new_driver_exception( $e->getMessage(), $e->getCode(), $e );
7864+
}
7865+
78397866
/**
78407867
* Convert an information schema exception to a MySQL-like driver exception.
78417868
*

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-reconstructor.php‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,11 @@ private function generate_column_default( string $mysql_type, ?string $default_v
574574
}
575575

576576
// HEX literals (numeric). E.g.: 0x1a2f, 0X1A2F
577-
$value = filter_var( $no_underscore_default_value, FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_HEX );
578-
if ( false !== $value ) {
579-
return $value;
577+
if ( 1 === preg_match( '/^0[xX][0-9a-fA-F]+$/D', $no_underscore_default_value ) ) {
578+
// Convert to signed 64-bit decimal text in SQLite to avoid PHP integer size limits.
579+
return (string) $this->connection->query(
580+
'SELECT CAST(' . $no_underscore_default_value . ' AS TEXT)'
581+
)->fetchColumn();
580582
}
581583

582584
// BLOB literals (string). E.g.: x'1a2f', X'1A2F'

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Metadata_Tests.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,9 @@ public function testTruncateTable() {
957957
}
958958

959959
public function testBogusQuery() {
960-
$this->expectExceptionMessage( 'no such table: bogus' );
960+
$this->expectExceptionMessage(
961+
"SQLSTATE[42S02]: Base table or view not found: 1146 Table 'bogus' doesn't exist"
962+
);
961963
$this->assertQuery(
962964
'SELECT 1, BOGUS(1) FROM bogus;'
963965
);

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_PDO_API_Tests.php‎

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,45 @@ public function test_driver_exception_exposes_originating_driver(): void {
228228
}
229229

230230
public function test_driver_exception_preserves_pdo_error_information(): void {
231+
$previous = new PDOException( 'Test PDO error.' );
232+
$previous->errorInfo = array( 'HY000', 1, 'Test PDO error.' );
233+
$exception = new WP_MySQL_On_SQLite_Exception(
234+
$this->driver,
235+
$previous->getMessage(),
236+
$previous->getCode(),
237+
$previous
238+
);
239+
240+
$this->assertSame( $previous->errorInfo, $exception->errorInfo );
241+
}
242+
243+
/**
244+
* @dataProvider data_missing_table_queries
245+
*/
246+
public function test_missing_table_errors_use_mysql_identity( string $query ): void {
247+
$driver_message = "Table 'missing_table' doesn't exist";
248+
$message = 'SQLSTATE[42S02]: Base table or view not found: 1146 ' . $driver_message;
249+
231250
try {
232-
$this->driver->query( 'SELECT * FROM missing_table' );
251+
$this->driver->query( $query );
233252
$this->fail( 'Expected query() to throw an exception.' );
234253
} catch ( WP_MySQL_On_SQLite_Exception $exception ) {
235-
$this->assertSame( 'HY000', $exception->errorInfo[0] );
236-
$this->assertSame( 1, $exception->errorInfo[1] );
237-
$this->assertSame( 'no such table: missing_table', $exception->errorInfo[2] );
254+
$this->assertSame( '42S02', $exception->getCode() );
255+
$this->assertSame( $message, $exception->getMessage() );
256+
$this->assertSame( array( '42S02', 1146, $driver_message ), $exception->errorInfo );
238257
}
258+
259+
$this->assertSame( '42S02', $this->driver->errorCode() );
260+
$this->assertSame( array( '42S02', 1146, $driver_message ), $this->driver->errorInfo() );
261+
}
262+
263+
public function data_missing_table_queries(): array {
264+
return array(
265+
'Select' => array( 'SELECT * FROM missing_table' ),
266+
'Insert' => array( 'INSERT INTO missing_table VALUES (1)' ),
267+
'Update' => array( 'UPDATE missing_table SET value = 1' ),
268+
'Delete' => array( 'DELETE FROM missing_table' ),
269+
);
239270
}
240271

241272
public function test_emulated_driver_exception_exposes_mysql_error_information(): void {
@@ -724,9 +755,11 @@ public function test_silent_error_mode(): void {
724755
$this->driver->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT );
725756

726757
$this->assertFalse( $this->driver->query( 'SELECT * FROM missing_table' ) );
727-
$this->assertSame( 'HY000', $this->driver->errorCode() );
728-
$this->assertSame( 1, $this->driver->errorInfo()[1] );
729-
$this->assertSame( 'no such table: missing_table', $this->driver->errorInfo()[2] );
758+
$this->assertSame( '42S02', $this->driver->errorCode() );
759+
$this->assertSame(
760+
array( '42S02', 1146, "Table 'missing_table' doesn't exist" ),
761+
$this->driver->errorInfo()
762+
);
730763
$this->assertFalse( $this->driver->exec( 'SELECT * FROM missing_table' ) );
731764

732765
$this->assertInstanceOf( PDOStatement::class, $this->driver->query( 'SELECT 1' ) );
@@ -752,7 +785,10 @@ function ( $level, $message ) use ( &$warning ) {
752785
restore_error_handler();
753786
}
754787

755-
$this->assertStringContainsString( 'no such table: missing_table', $warning );
788+
$this->assertSame(
789+
"SQLSTATE[42S02]: Base table or view not found: 1146 Table 'missing_table' doesn't exist",
790+
$warning
791+
);
756792
}
757793

758794
public function test_quote_matches_mysql_escaping(): void {

‎packages/mysql-on-sqlite/tests/WP_SQLite_Information_Schema_Reconstructor_Tests.php‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,36 @@ public function testDefaultValues(): void {
268268
);
269269
}
270270

271+
public function testHexadecimalDefaultValues(): void {
272+
$this->engine->get_connection()->query(
273+
'
274+
CREATE TABLE t (
275+
leading_zeroes int DEFAULT 0x00000000000000000001,
276+
max_positive int DEFAULT 0x7fffffffffffffff,
277+
min_negative int DEFAULT 0x8000000000000000,
278+
minus_one int DEFAULT 0xffffffffffffffff
279+
)
280+
'
281+
);
282+
283+
$this->reconstructor->ensure_correct_information_schema();
284+
$result = $this->assertQuery( 'SHOW CREATE TABLE t' );
285+
$this->assertSame(
286+
implode(
287+
"\n",
288+
array(
289+
'CREATE TABLE `t` (',
290+
" `leading_zeroes` int DEFAULT '1',",
291+
" `max_positive` int DEFAULT '9223372036854775807',",
292+
" `min_negative` int DEFAULT '-9223372036854775808',",
293+
" `minus_one` int DEFAULT '-1'",
294+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
295+
)
296+
),
297+
$result[0]->{'Create Table'}
298+
);
299+
}
300+
271301
public function testReconstructBitDefaultValuesFromMysqlDataTypesCache(): void {
272302
$connection = $this->engine->get_connection();
273303

‎packages/plugin-sqlite-database-integration/constants.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
/**
2424
* FQDBDIR is a directory where the sqlite database file is placed.
2525
* If DB_DIR is defined, it is used as FQDBDIR.
26+
*
27+
* @deprecated 3.0.0 Define DB_DIR instead of overriding FQDBDIR.
2628
*/
2729
if ( ! defined( 'FQDBDIR' ) ) {
2830
if ( defined( 'DB_DIR' ) ) {
@@ -37,6 +39,8 @@
3739
/**
3840
* FQDB is a database file name. If DB_FILE is defined, it is used
3941
* as FQDB.
42+
*
43+
* @deprecated 3.0.0 Define DB_DIR and DB_FILE instead of overriding FQDB.
4044
*/
4145
if ( ! defined( 'FQDB' ) ) {
4246
if ( defined( 'DB_FILE' ) ) {

‎packages/plugin-sqlite-database-integration/db.copy‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@ if ( ! $sqlite_plugin_implementation_folder_path || ! file_exists( $sqlite_plugi
2222
return;
2323
}
2424

25-
// Constant for backward compatibility.
25+
/**
26+
* Legacy database type marker.
27+
*
28+
* @deprecated 3.0.0 Use DB_ENGINE instead.
29+
*/
2630
if ( ! defined( 'DATABASE_TYPE' ) ) {
2731
define( 'DATABASE_TYPE', 'sqlite' );
2832
}
33+
2934
// Define SQLite constant.
3035
if ( ! defined( 'DB_ENGINE' ) ) {
3136
define( 'DB_ENGINE', 'sqlite' );

0 commit comments

Comments
 (0)