MySQL on SQLite: Unquoted SQLite identifiers in LOCK TABLES, SHOW TABLES, and compound primary key indexes
Description
In WP_MySQL_On_SQLite (packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php), three SQLite query construction paths interpolate unquoted identifier strings directly into SQL templates instead of quoting them with $this->quote_sqlite_identifier():
-
LOCK TABLES(execute_transaction_or_locking_statement()):$table_nameis unquoted via$this->unquote_sqlite_identifier( $this->translate( $table_ref ) )and interpolated directly intoSELECT 1 FROM %s LIMIT 0. When locking an existing persistent orTEMPORARYtable whose name contains spaces, hyphens, escaped backticks, or SQLite keywords (e.g.,`table name`,`my-table`,`select`), SQLite raises a syntax error that is caught and misreported asSQLSTATE[42S02]: Base table or view not found: 1146 Table 'wp.table name' doesn't exist. -
SHOW [FULL] TABLES(execute_show_tables_statement()): TheTables_in_<db>column alias is formatted with raw backticks (sprintf( 'table_name AS \Tables_in_%s`', $database )). If the database identifier contains an escaped backtick (valid in MySQL, e.g., ``SHOW TABLES FROMdbname`or a configureddbnamecontaining a backtick), the unescaped backtick breaks the generated SQLiteSELECTalias with a syntax error. -
Compound
PRIMARY KEYwithAUTO_INCREMENT(get_sqlite_create_table_statement()): When emulating a compoundPRIMARY KEYwhose first column isAUTO_INCREMENT, the generated unique index queryCREATE UNIQUE INDEX %s ON %s (%s)passesself::RESERVED_PREFIX . $sqlite_index_nameunquoted (unlike regular indexes right below it, which call$this->quote_sqlite_identifier( $sqlite_index_name )). Creating or rebuilding (ALTER TABLE) such a table fails with an SQLite syntax error whenever the table name contains spaces, hyphens, or special characters.
Steps to Reproduce
$db = new WP_MySQL_On_SQLite( 'mysql-on-sqlite:path=:memory:;dbname=wp' );
// 1. LOCK TABLES on a quoted table name with spaces or keywords:
$db->query( 'CREATE TABLE `table name` (id INT)' );
$db->query( 'LOCK TABLES `table name` READ' );
// Throws: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'wp.table name' doesn't exist
// 2. Compound PRIMARY KEY + AUTO_INCREMENT on a quoted table name:
$db->query( 'CREATE TABLE `compound table` (id INT AUTO_INCREMENT, name VARCHAR(32), PRIMARY KEY(id, name))' );
// Throws: SQLSTATE[HY000]: General error: 1 near "table__primary": syntax error
// 3. SHOW TABLES with a database identifier containing a backtick:
$db2 = new WP_MySQL_On_SQLite( 'mysql-on-sqlite:path=:memory:;dbname=db`name' );
$db2->query( 'SHOW TABLES FROM `db``name`' );
// Throws: SQLSTATE[HY000]: General error: 1 near "name": syntax error
Expected Behavior
All three statements should quote the generated SQLite identifiers via $this->quote_sqlite_identifier() and execute identically to MySQL for all valid quoted identifiers.