@@ -3976,4 +3976,54 @@ public function getInformationSchemaIsReadonlyWithUseTestData(): array {
39763976 array ( 'TRUNCATE tables ' ),
39773977 );
39783978 }
3979+
3980+ public function testTemporaryTableHasPriorityOverStandardTable (): void {
3981+ // Create a standard and a temporary table with the same name.
3982+ $ this ->assertQuery ( 'CREATE TABLE t (a INT, INDEX ia(a)) ' );
3983+ $ this ->assertQuery ( 'CREATE TEMPORARY TABLE t (b INT, INDEX ib(b)) ' );
3984+
3985+ // SHOW CREATE TABLE will show the temporary table.
3986+ $ result = $ this ->assertQuery ( 'SHOW CREATE TABLE t ' );
3987+ $ this ->assertEquals (
3988+ "CREATE TEMPORARY TABLE `t` ( \n"
3989+ . " `b` int DEFAULT NULL, \n"
3990+ . " KEY `ib` (`b`) \n"
3991+ . ') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ' ,
3992+ $ result [0 ]->{'Create Table ' }
3993+ );
3994+
3995+ // SHOW COLUMNS FROM will show the temporary table.
3996+ $ result = $ this ->assertQuery ( 'SHOW COLUMNS FROM t ' );
3997+ $ this ->assertEquals ( 'b ' , $ result [0 ]->Field );
3998+
3999+ // DESCRIBE will show the temporary table.
4000+ $ result = $ this ->assertQuery ( 'DESCRIBE t ' );
4001+ $ this ->assertEquals ( 'b ' , $ result [0 ]->Field );
4002+
4003+ // SHOW INDEXES FROM will show the temporary table.
4004+ $ result = $ this ->assertQuery ( 'SHOW INDEXES FROM t ' );
4005+ $ this ->assertEquals ( 'ib ' , $ result [0 ]->Key_name );
4006+
4007+ // ALTER TABLE will use the temporary table.
4008+ $ this ->assertQuery ( 'ALTER TABLE t ADD COLUMN c INT ' );
4009+ $ result = $ this ->assertQuery ( 'SHOW COLUMNS FROM t ' );
4010+ $ this ->assertEquals ( 'b ' , $ result [0 ]->Field );
4011+ $ this ->assertEquals ( 'c ' , $ result [1 ]->Field );
4012+
4013+ // The temporary table doesn't show up in information schema.
4014+ $ result = $ this ->assertQuery ( 'SELECT * FROM information_schema.columns WHERE table_name = "t" ' );
4015+ $ this ->assertCount ( 1 , $ result );
4016+ $ this ->assertEquals ( 'a ' , $ result [0 ]->COLUMN_NAME );
4017+
4018+ // First DROP TABLE removes the temporary table.
4019+ $ this ->assertQuery ( 'DROP TABLE t ' );
4020+ $ result = $ this ->assertQuery ( 'SHOW COLUMNS FROM t ' );
4021+ $ this ->assertEquals ( 'a ' , $ result [0 ]->Field );
4022+
4023+ // Second DROP TABLE removes the standard table.
4024+ $ this ->expectException ( WP_SQLite_Driver_Exception::class );
4025+ $ this ->expectExceptionMessage ( "Table 'wp.t' doesn't exist " );
4026+ $ this ->assertQuery ( 'DROP TABLE t ' );
4027+ $ result = $ this ->assertQuery ( 'SHOW COLUMNS FROM t ' );
4028+ }
39794029}
0 commit comments