Skip to content

Commit 76a739a

Browse files
committed
Fix usage of fully qualified table names
1 parent 581d361 commit 76a739a

File tree

3 files changed

+419
-100
lines changed

3 files changed

+419
-100
lines changed

tests/WP_SQLite_Driver_Tests.php

Lines changed: 185 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4195,12 +4195,22 @@ public function testInformationSchemaIsReadonly( string $query ): void {
41954195
public function getInformationSchemaIsReadonlyTestData(): array {
41964196
return array(
41974197
array( 'INSERT INTO information_schema.tables (table_name) VALUES ("t")' ),
4198+
array( 'REPLACE INTO information_schema.tables (table_name) VALUES ("t")' ),
41984199
array( 'UPDATE information_schema.tables SET table_name = "new_t" WHERE table_name = "t"' ),
4200+
array( 'UPDATE information_schema.tables, information_schema.columns SET table_name = "new_t" WHERE table_name = "t"' ),
41994201
array( 'DELETE FROM information_schema.tables WHERE table_name = "t"' ),
4202+
array( 'DELETE it FROM t, information_schema.tables it WHERE table_name = "t"' ),
4203+
array( 'TRUNCATE information_schema.tables' ),
42004204
array( 'CREATE TABLE information_schema.new_table (id INT)' ),
42014205
array( 'ALTER TABLE information_schema.tables ADD COLUMN new_column INT' ),
42024206
array( 'DROP TABLE information_schema.tables' ),
4203-
array( 'TRUNCATE information_schema.tables' ),
4207+
array( 'LOCK TABLES information_schema.tables READ' ),
4208+
array( 'CREATE INDEX idx_name ON information_schema.tables (name)' ),
4209+
array( 'DROP INDEX `PRIMARY` ON information_schema.tables' ),
4210+
array( 'ANALYZE TABLE information_schema.tables' ),
4211+
array( 'CHECK TABLE information_schema.tables' ),
4212+
array( 'OPTIMIZE TABLE information_schema.tables' ),
4213+
array( 'REPAIR TABLE information_schema.tables' ),
42044214
);
42054215
}
42064216

@@ -4218,12 +4228,22 @@ public function testInformationSchemaIsReadonlyWithUse( string $query ): void {
42184228
public function getInformationSchemaIsReadonlyWithUseTestData(): array {
42194229
return array(
42204230
array( 'INSERT INTO tables (table_name) VALUES ("t")' ),
4231+
array( 'REPLACE INTOtables (table_name) VALUES ("t")' ),
42214232
array( 'UPDATE tables SET table_name = "new_t" WHERE table_name = "t"' ),
4233+
array( 'UPDATE tables, columns SET table_name = "new_t" WHERE table_name = "t"' ),
42224234
array( 'DELETE FROM tables WHERE table_name = "t"' ),
4235+
array( 'DELETE it FROM t, tables it WHERE table_name = "t"' ),
4236+
array( 'TRUNCATE tables' ),
42234237
array( 'CREATE TABLE new_table (id INT)' ),
42244238
array( 'ALTER TABLE tables ADD COLUMN new_column INT' ),
42254239
array( 'DROP TABLE tables' ),
4226-
array( 'TRUNCATE tables' ),
4240+
array( 'LOCK TABLES tables READ' ),
4241+
array( 'CREATE INDEX idx_name ON tables (name)' ),
4242+
array( 'DROP INDEX `PRIMARY` ON tables' ),
4243+
array( 'ANALYZE TABLE tables' ),
4244+
array( 'CHECK TABLE tables' ),
4245+
array( 'OPTIMIZE TABLE tables' ),
4246+
array( 'REPAIR TABLE tables' ),
42274247
);
42284248
}
42294249

@@ -9439,4 +9459,167 @@ public function testCastExpression(): void {
94399459
$result
94409460
);
94419461
}
9462+
9463+
public function testFullyQualifiedTableName(): void {
9464+
// Ensure "information_schema.tables" is empty.
9465+
$this->assertQuery( 'DROP TABLE _options, _dates' );
9466+
$result = $this->assertQuery( 'SELECT * FROM information_schema.tables' );
9467+
$this->assertCount( 0, $result );
9468+
9469+
// Switch to the "information_schema" database.
9470+
$this->assertQuery( 'USE information_schema' );
9471+
9472+
// CREATE TABLE
9473+
$this->assertQuery( 'CREATE TABLE wp.t (id INT PRIMARY KEY)' );
9474+
$result = $this->assertQuery( 'SHOW TABLES FROM wp' );
9475+
$this->assertCount( 1, $result );
9476+
9477+
// INSERT
9478+
$this->assertQuery( 'INSERT INTO wp.t (id) VALUES (1)' );
9479+
$result = $this->assertQuery( 'SELECT * FROM wp.t' );
9480+
$this->assertEquals( array( (object) array( 'id' => '1' ) ), $result );
9481+
9482+
// SELECT
9483+
$result = $this->assertQuery( 'SELECT * FROM wp.t' );
9484+
$this->assertEquals( array( (object) array( 'id' => '1' ) ), $result );
9485+
9486+
// UPDATE
9487+
$this->assertQuery( 'UPDATE wp.t SET id = 2' );
9488+
$result = $this->assertQuery( 'SELECT * FROM wp.t' );
9489+
$this->assertEquals( array( (object) array( 'id' => '2' ) ), $result );
9490+
9491+
// DELETE
9492+
$this->assertQuery( 'DELETE FROM wp.t' );
9493+
$result = $this->assertQuery( 'SELECT * FROM wp.t' );
9494+
$this->assertCount( 0, $result );
9495+
9496+
// TRUNCATE TABLE
9497+
$this->assertQuery( 'INSERT INTO wp.t (id) VALUES (1)' );
9498+
$this->assertQuery( 'TRUNCATE TABLE wp.t' );
9499+
$result = $this->assertQuery( 'SELECT * FROM wp.t' );
9500+
$this->assertCount( 0, $result );
9501+
9502+
// SHOW CREATE TABLE
9503+
$result = $this->assertQuery( 'SHOW CREATE TABLE wp.t' );
9504+
$this->assertEquals(
9505+
array(
9506+
(object) array(
9507+
'Create Table' => implode(
9508+
"\n",
9509+
array(
9510+
'CREATE TABLE `t` (',
9511+
' `id` int NOT NULL,',
9512+
' PRIMARY KEY (`id`)',
9513+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
9514+
)
9515+
),
9516+
),
9517+
),
9518+
$result
9519+
);
9520+
9521+
// SHOW COLUMNS
9522+
$result = $this->assertQuery( 'SHOW COLUMNS FROM wp.t' );
9523+
$this->assertEquals(
9524+
array(
9525+
(object) array(
9526+
'Field' => 'id',
9527+
'Type' => 'int',
9528+
'Null' => 'NO',
9529+
'Key' => 'PRI',
9530+
'Default' => null,
9531+
'Extra' => '',
9532+
),
9533+
),
9534+
$result
9535+
);
9536+
9537+
// SHOW COLUMNS with both qualified table name and "FROM database" clause.
9538+
// In case both are present, the "FROM database" clause takes precedence.
9539+
$result = $this->assertQuery( 'SHOW COLUMNS FROM information_schema.t FROM wp' );
9540+
$this->assertEquals(
9541+
array(
9542+
(object) array(
9543+
'Field' => 'id',
9544+
'Type' => 'int',
9545+
'Null' => 'NO',
9546+
'Key' => 'PRI',
9547+
'Default' => null,
9548+
'Extra' => '',
9549+
),
9550+
),
9551+
$result
9552+
);
9553+
9554+
// SHOW INDEXES
9555+
$result = $this->assertQuery( 'SHOW INDEXES FROM wp.t' );
9556+
$this->assertCount( 1, $result );
9557+
$this->assertEquals( 'PRIMARY', $result[0]->Key_name );
9558+
9559+
// SHOW INDEXES with both qualified table name and "FROM database" clause.
9560+
// In case both are present, the "FROM database" clause takes precedence.
9561+
$result = $this->assertQuery( 'SHOW INDEXES FROM information_schema.t FROM wp' );
9562+
$this->assertCount( 1, $result );
9563+
$this->assertEquals( 'PRIMARY', $result[0]->Key_name );
9564+
9565+
// DESCRIBE
9566+
$result = $this->assertQuery( 'DESCRIBE wp.t' );
9567+
$this->assertCount( 1, $result );
9568+
$this->assertEquals( 'id', $result[0]->Field );
9569+
$this->assertEquals( 'int', $result[0]->Type );
9570+
$this->assertEquals( 'NO', $result[0]->Null );
9571+
$this->assertEquals( 'PRI', $result[0]->Key );
9572+
$this->assertEquals( null, $result[0]->Default );
9573+
$this->assertEquals( '', $result[0]->Extra );
9574+
9575+
// SHOW TABLES
9576+
$result = $this->assertQuery( 'SHOW TABLES FROM wp' );
9577+
$this->assertCount( 1, $result );
9578+
$this->assertEquals( 't', $result[0]->Tables_in_wp );
9579+
9580+
// SHOW TABLE STATUS
9581+
$result = $this->assertQuery( 'SHOW TABLE STATUS FROM wp' );
9582+
$this->assertCount( 1, $result );
9583+
$this->assertEquals( 't', $result[0]->Name );
9584+
9585+
// ALTER TABLE
9586+
$this->assertQuery( 'ALTER TABLE wp.t ADD COLUMN name VARCHAR(255)' );
9587+
$result = $this->assertQuery( 'SHOW COLUMNS FROM wp.t' );
9588+
$this->assertCount( 2, $result );
9589+
9590+
// CREATE INDEX
9591+
$this->assertQuery( 'CREATE INDEX idx_name ON wp.t (name)' );
9592+
$result = $this->assertQuery( 'SHOW INDEXES FROM wp.t' );
9593+
$this->assertCount( 2, $result );
9594+
$this->assertEquals( 'idx_name', $result[1]->Key_name );
9595+
9596+
// DROP INDEX
9597+
$this->assertQuery( 'DROP INDEX idx_name ON wp.t' );
9598+
$result = $this->assertQuery( 'SHOW INDEXES FROM wp.t' );
9599+
$this->assertCount( 1, $result );
9600+
$this->assertEquals( 'PRIMARY', $result[0]->Key_name );
9601+
9602+
// LOCK TABLE
9603+
$this->assertQuery( 'LOCK TABLES wp.t READ' );
9604+
9605+
// UNLOCK TABLE
9606+
$this->assertQuery( 'UNLOCK TABLES' );
9607+
9608+
// ANALYZE TABLE
9609+
$this->assertQuery( 'ANALYZE TABLE wp.t' );
9610+
9611+
// CHECK TABLE
9612+
$this->assertQuery( 'CHECK TABLE wp.t' );
9613+
9614+
// OPTIMIZE TABLE
9615+
$this->assertQuery( 'OPTIMIZE TABLE wp.t' );
9616+
9617+
// REPAIR TABLE
9618+
$this->assertQuery( 'REPAIR TABLE wp.t' );
9619+
9620+
// DROP TABLE
9621+
$this->assertQuery( 'DROP TABLE wp.t' );
9622+
$result = $this->assertQuery( 'SHOW TABLES FROM wp' );
9623+
$this->assertCount( 0, $result );
9624+
}
94429625
}

0 commit comments

Comments
 (0)