sqlite-database-integration icon indicating copy to clipboard operation
sqlite-database-integration copied to clipboard

MySQL on SQLite: Unquoted SQLite identifiers in LOCK TABLES, SHOW TABLES, and compound primary key indexes

Open 0xABCD01 opened this issue 1 month ago • 0 comments

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():

  1. LOCK TABLES (execute_transaction_or_locking_statement()): $table_name is unquoted via $this->unquote_sqlite_identifier( $this->translate( $table_ref ) ) and interpolated directly into SELECT 1 FROM %s LIMIT 0. When locking an existing persistent or TEMPORARY table 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 as SQLSTATE[42S02]: Base table or view not found: 1146 Table 'wp.table name' doesn't exist.

  2. SHOW [FULL] TABLES (execute_show_tables_statement()): The Tables_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 FROM dbname` or a configured dbname containing a backtick), the unescaped backtick breaks the generated SQLite SELECT alias with a syntax error.

  3. Compound PRIMARY KEY with AUTO_INCREMENT (get_sqlite_create_table_statement()): When emulating a compound PRIMARY KEY whose first column is AUTO_INCREMENT, the generated unique index query CREATE UNIQUE INDEX %s ON %s (%s) passes self::RESERVED_PREFIX . $sqlite_index_name unquoted (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.

0xABCD01 avatar Nov 01 '26 07:11 0xABCD01