@@ -8164,7 +8164,11 @@ public function testCompoundPrimaryKeyWithAutoincrement(): void {
81648164 /**
81658165 * @dataProvider getReservedPrefixTestData
81668166 */
8167- public function testReservedPrefix( string $query, string $error ): void {
8167+ public function testReservedPrefix( string $query, string $error, bool $ansi_quotes = false ): void {
8168+ if ( $ansi_quotes ) {
8169+ $this->assertQuery( "SET sql_mode = 'ANSI_QUOTES'" );
8170+ }
8171+
81688172 $this->expectException( WP_MySQL_On_SQLite_Exception::class );
81698173 $this->expectExceptionMessage( $error );
81708174 $this->assertQuery( $query );
@@ -8196,6 +8200,169 @@ public function getReservedPrefixTestData(): array {
81968200 'DROP TABLE _wp_sqlite_t',
81978201 "Invalid identifier '_wp_sqlite_t', prefix '_wp_sqlite_' is reserved",
81988202 ),
8203+ array(
8204+ 'SELECT COUNT(*) FROM _WP_SQLITE_mysql_information_schema_tables',
8205+ "Invalid identifier '_WP_SQLITE_mysql_information_schema_tables', prefix '_wp_sqlite_' is reserved",
8206+ ),
8207+ array(
8208+ 'SELECT COUNT(*) FROM _Wp_SqLiTe_mysql_information_schema_tables',
8209+ "Invalid identifier '_Wp_SqLiTe_mysql_information_schema_tables', prefix '_wp_sqlite_' is reserved",
8210+ ),
8211+ array(
8212+ 'SELECT COUNT(*) FROM `wp`.`_Wp_SqLiTe_mysql_information_schema_tables`',
8213+ "Invalid identifier '_Wp_SqLiTe_mysql_information_schema_tables', prefix '_wp_sqlite_' is reserved",
8214+ ),
8215+ array(
8216+ 'SELECT COUNT(*) FROM "_WP_SQLITE_mysql_information_schema_tables"',
8217+ "Invalid identifier '_WP_SQLITE_mysql_information_schema_tables', prefix '_wp_sqlite_' is reserved",
8218+ true,
8219+ ),
8220+ array(
8221+ "INSERT INTO _Wp_SqLiTe_global_variables (name, value) VALUES ('test', 'value')",
8222+ "Invalid identifier '_Wp_SqLiTe_global_variables', prefix '_wp_sqlite_' is reserved",
8223+ ),
8224+ array(
8225+ 'DELETE FROM _Wp_SqLiTe_mysql_information_schema_tables',
8226+ "Invalid identifier '_Wp_SqLiTe_mysql_information_schema_tables', prefix '_wp_sqlite_' is reserved",
8227+ ),
8228+ array(
8229+ 'DROP TABLE _Wp_SqLiTe_mysql_information_schema_tables',
8230+ "Invalid identifier '_Wp_SqLiTe_mysql_information_schema_tables', prefix '_wp_sqlite_' is reserved",
8231+ ),
8232+ array(
8233+ 'TRUNCATE TABLE _Wp_SqLiTe_mysql_information_schema_tables',
8234+ "Invalid identifier '_Wp_SqLiTe_mysql_information_schema_tables', prefix '_wp_sqlite_' is reserved",
8235+ ),
8236+ array(
8237+ 'CREATE TEMPORARY TABLE _Wp_SqLiTe_mysql_information_schema_tables (id INT)',
8238+ "Invalid identifier '_Wp_SqLiTe_mysql_information_schema_tables', prefix '_wp_sqlite_' is reserved",
8239+ ),
8240+ array(
8241+ 'CREATE TABLE _WP_SQLITE_ (id INT)',
8242+ "Invalid identifier '_WP_SQLITE_', prefix '_wp_sqlite_' is reserved",
8243+ ),
8244+ );
8245+ }
8246+
8247+ public function testReservedPrefixInSavepoint(): void {
8248+ $this->assertQuery( 'START TRANSACTION' );
8249+ $this->assertQueryError(
8250+ 'SAVEPOINT _Wp_SqLiTe_savepoint_wrapper',
8251+ "Invalid identifier '_Wp_SqLiTe_savepoint_wrapper', prefix '_wp_sqlite_' is reserved"
8252+ );
8253+ $this->assertTrue( $this->engine->inTransaction() );
8254+ $this->assertQuery( 'ROLLBACK' );
8255+ }
8256+
8257+ /**
8258+ * @dataProvider getReservedForeignKeyIdentifiersTestData
8259+ */
8260+ public function testReservedForeignKeyIdentifiers( string $query, string $identifier, bool $ansi_quotes = false ): void {
8261+ if ( $ansi_quotes ) {
8262+ $this->assertQuery( "SET sql_mode = 'ANSI_QUOTES'" );
8263+ }
8264+ $this->assertQuery( 'CREATE TABLE child (n VARCHAR(64))' );
8265+ $this->assertQuery( "INSERT INTO child VALUES ('kept')" );
8266+ $schema = $this->assertQuery( 'SHOW CREATE TABLE child' );
8267+
8268+ $this->assertQueryError( $query, "Invalid identifier '$identifier', prefix '_wp_sqlite_' is reserved" );
8269+
8270+ $this->assertEquals( $schema, $this->assertQuery( 'SHOW CREATE TABLE child' ) );
8271+ $this->assertEquals( array( (object) array( 'n' => 'kept' ) ), $this->assertQuery( 'SELECT * FROM child' ) );
8272+ $this->assertSame( 0, (int) $this->sqlite->query( "SELECT COUNT(*) FROM sqlite_temp_master WHERE name = 'new_child'" )->fetchColumn() );
8273+
8274+ // Rejected references must leave no metadata behind or block later drops.
8275+ $this->assertQuery( 'CREATE TABLE new_child (id INT)' );
8276+ $this->assertQuery( 'DROP TABLE new_child, child' );
8277+ }
8278+
8279+ public function getReservedForeignKeyIdentifiersTestData(): array {
8280+ return array(
8281+ array(
8282+ 'CREATE TABLE new_child (n VARCHAR(64), FOREIGN KEY (n) REFERENCES _wp_sqlite_mysql_information_schema_tables (TABLE_NAME))',
8283+ '_wp_sqlite_mysql_information_schema_tables',
8284+ ),
8285+ array(
8286+ 'CREATE TABLE new_child (n VARCHAR(64), FOREIGN KEY (n) REFERENCES _WP_SQLITE_global_variables (name))',
8287+ '_WP_SQLITE_global_variables',
8288+ ),
8289+ array(
8290+ 'CREATE TABLE new_child (n VARCHAR(64), FOREIGN KEY (n) REFERENCES `wp`.`_Wp_SqLiTe_mysql_information_schema_tables` (`TABLE_NAME`))',
8291+ '_Wp_SqLiTe_mysql_information_schema_tables',
8292+ ),
8293+ array(
8294+ 'CREATE TABLE new_child (n VARCHAR(64), FOREIGN KEY (n) REFERENCES "_Wp_SqLiTe_global_variables" ("name"))',
8295+ '_Wp_SqLiTe_global_variables',
8296+ true,
8297+ ),
8298+ array(
8299+ 'CREATE TEMPORARY TABLE new_child (n VARCHAR(64), FOREIGN KEY (n) REFERENCES _Wp_SqLiTe_mysql_information_schema_tmp_tables (TABLE_NAME))',
8300+ '_Wp_SqLiTe_mysql_information_schema_tmp_tables',
8301+ ),
8302+ array(
8303+ 'ALTER TABLE child ADD FOREIGN KEY (n) REFERENCES _wp_sqlite_mysql_information_schema_tables (TABLE_NAME)',
8304+ '_wp_sqlite_mysql_information_schema_tables',
8305+ ),
8306+ array(
8307+ 'ALTER TABLE child ADD COLUMN extra INT, ADD CONSTRAINT fk FOREIGN KEY (n) REFERENCES _Wp_SqLiTe_global_variables (name)',
8308+ '_Wp_SqLiTe_global_variables',
8309+ ),
8310+ array(
8311+ 'CREATE TABLE new_child (n VARCHAR(64) REFERENCES _wp_sqlite_global_variables (name))',
8312+ '_wp_sqlite_global_variables',
8313+ ),
8314+ array(
8315+ 'CREATE TABLE new_child (n VARCHAR(64), FOREIGN KEY (n) REFERENCES _WP_SQLITE_db.parent (n))',
8316+ '_WP_SQLITE_db',
8317+ ),
8318+ array(
8319+ 'CREATE TABLE new_child (n VARCHAR(64), FOREIGN KEY (n) REFERENCES child (_Wp_SqLiTe_column))',
8320+ '_Wp_SqLiTe_column',
8321+ ),
8322+ );
8323+ }
8324+
8325+ public function testForeignKeyWithSimilarPrefix(): void {
8326+ $this->assertQuery( 'CREATE TABLE `_WP_SQLITE` (n VARCHAR(64) PRIMARY KEY)' );
8327+ $this->assertQuery( "INSERT INTO `_WP_SQLITE` VALUES ('_wp_sqlite_t')" );
8328+ $this->assertQuery(
8329+ "CREATE TABLE child (
8330+ id INT,
8331+ n VARCHAR(64) DEFAULT '_wp_sqlite_t',
8332+ FOREIGN KEY (n) REFERENCES `wp`.`_WP_SQLITE` (n)
8333+ )"
8334+ );
8335+ $this->assertQuery( 'CREATE TABLE other_child (n VARCHAR(64))' );
8336+ $this->assertQuery( 'ALTER TABLE other_child ADD FOREIGN KEY (n) REFERENCES `_WP_SQLITE` (n)' );
8337+ $this->assertQuery( 'INSERT INTO child (id) VALUES (1)' );
8338+ $this->assertQuery( "INSERT INTO other_child VALUES ('_wp_sqlite_t')" );
8339+ $this->assertEquals( array( (object) array( 'n' => '_wp_sqlite_t' ) ), $this->assertQuery( 'SELECT n FROM child' ) );
8340+ $this->assertQueryError(
8341+ "INSERT INTO child (n) VALUES ('missing')",
8342+ 'SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed'
8343+ );
8344+ $this->assertQueryError(
8345+ "INSERT INTO other_child VALUES ('missing')",
8346+ 'SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed'
8347+ );
8348+ }
8349+
8350+ /**
8351+ * @dataProvider getNamesWithoutReservedPrefixTestData
8352+ */
8353+ public function testNamesWithoutReservedPrefix( string $identifier ): void {
8354+ $name = $this->engine->get_connection()->quote_identifier( $identifier );
8355+ $this->assertQuery( "CREATE TABLE $name ($name INT)" );
8356+ $this->assertQuery( "INSERT INTO $name ($name) VALUES (42)" );
8357+ $result = $this->assertQuery( "SELECT wp.$name.$name AS $name FROM wp.$name" );
8358+ $this->assertEquals( array( (object) array( $identifier => '42' ) ), $result );
8359+ }
8360+
8361+ public function getNamesWithoutReservedPrefixTestData(): array {
8362+ return array(
8363+ array( '_WP_SQLITE' ),
8364+ array( 'x_WP_SQLITE_t' ),
8365+ array( '_Wp_SqLiTeX_t' ),
81998366 );
82008367 }
82018368
0 commit comments