Skip to content

Commit 35211d7

Browse files
committed
Enable read usage of information schema tables in write queries
1 parent 76a739a commit 35211d7

File tree

2 files changed

+89
-5
lines changed

2 files changed

+89
-5
lines changed

tests/WP_SQLite_Driver_Tests.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9622,4 +9622,93 @@ public function testFullyQualifiedTableName(): void {
96229622
$result = $this->assertQuery( 'SHOW TABLES FROM wp' );
96239623
$this->assertCount( 0, $result );
96249624
}
9625+
9626+
public function testWriteWithUsageOfInformationSchemaTables(): void {
9627+
// Ensure "information_schema.tables" is empty.
9628+
$this->assertQuery( 'DROP TABLE _options, _dates' );
9629+
$result = $this->assertQuery( 'SELECT * FROM information_schema.tables' );
9630+
$this->assertCount( 0, $result );
9631+
9632+
// Create a table.
9633+
$this->assertQuery( 'CREATE TABLE t (id INT, value VARCHAR(255))' );
9634+
9635+
// INSERT with SELECT from information schema.
9636+
$this->assertQuery( 'INSERT INTO t (id, value) SELECT 1, table_name FROM information_schema.tables' );
9637+
$result = $this->assertQuery( 'SELECT * FROM t' );
9638+
$this->assertCount( 1, $result );
9639+
$this->assertEquals(
9640+
array(
9641+
(object) array(
9642+
'id' => '1',
9643+
'value' => 't',
9644+
),
9645+
),
9646+
$result
9647+
);
9648+
9649+
// INSERT with subselect from information schema.
9650+
$this->assertQuery( 'INSERT INTO t (id, value) SELECT 2, table_name FROM (SELECT table_name FROM information_schema.tables)' );
9651+
$result = $this->assertQuery( 'SELECT * FROM t' );
9652+
$this->assertCount( 2, $result );
9653+
$this->assertEquals(
9654+
array(
9655+
(object) array(
9656+
'id' => '1',
9657+
'value' => 't',
9658+
),
9659+
(object) array(
9660+
'id' => '2',
9661+
'value' => 't',
9662+
),
9663+
),
9664+
$result
9665+
);
9666+
9667+
// INSERT with JOIN on information schema.
9668+
$this->assertQuery(
9669+
'INSERT INTO t (id, value)
9670+
SELECT 3, it.table_name
9671+
FROM information_schema.schemata s
9672+
JOIN information_schema.tables it ON s.schema_name = it.table_schema'
9673+
);
9674+
$result = $this->assertQuery( 'SELECT * FROM t' );
9675+
$this->assertCount( 3, $result );
9676+
$this->assertEquals(
9677+
array(
9678+
(object) array(
9679+
'id' => '1',
9680+
'value' => 't',
9681+
),
9682+
(object) array(
9683+
'id' => '2',
9684+
'value' => 't',
9685+
),
9686+
(object) array(
9687+
'id' => '3',
9688+
'value' => 't',
9689+
),
9690+
),
9691+
$result
9692+
);
9693+
9694+
// TODO: UPDATE with JOIN on information schema is not supported yet.
9695+
9696+
// DELETE with JOIN on information schema.
9697+
$this->assertQuery( 'UPDATE t SET value = "other" WHERE id > 1' );
9698+
$this->assertQuery( 'DELETE t FROM t JOIN information_schema.tables it ON t.value = it.table_name' );
9699+
$result = $this->assertQuery( 'SELECT * FROM t' );
9700+
$this->assertEquals(
9701+
array(
9702+
(object) array(
9703+
'id' => '2',
9704+
'value' => 'other',
9705+
),
9706+
(object) array(
9707+
'id' => '3',
9708+
'value' => 'other',
9709+
),
9710+
),
9711+
$result
9712+
);
9713+
}
96259714
}

wp-includes/sqlite-ast/class-wp-sqlite-driver.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4083,11 +4083,6 @@ public function translate_select_item( WP_Parser_Node $node ): string {
40834083
* @throws WP_SQLite_Driver_Exception When the translation fails.
40844084
*/
40854085
public function translate_table_ref( WP_Parser_Node $node ): string {
4086-
// Information schema is currently accessible only in read-only queries.
4087-
if ( ! $this->is_readonly ) {
4088-
return $this->translate_sequence( $node->get_children() );
4089-
}
4090-
40914086
// The table reference is in "<schema>.<table>" or "<table>" format.
40924087
$parts = $node->get_descendant_nodes( 'identifier' );
40934088
$table = array_pop( $parts );

0 commit comments

Comments
 (0)