Skip to content

Commit d903016

Browse files
committed
Select information_schema using a nested query
TODO: - support WHERE, ORDER... - support AS - ensure all schema fields are correct
1 parent 271163b commit d903016

2 files changed

Lines changed: 62 additions & 4 deletions

File tree

tests/WP_SQLite_Metadata_Tests.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,36 @@ public function testCountTables() {
7373
self::assertIsNumeric( $count );
7474
}
7575

76+
public function testInformationSchemaTables() {
77+
$result = $this->assertQuery( "SELECT * FROM information_schema.tables" );
78+
$this->assertEquals(
79+
array(
80+
'TABLE_NAME' => '_mysql_data_types_cache',
81+
'TABLE_TYPE' => 'BASE TABLE',
82+
'TABLE_SCHEMA' => 'database',
83+
'ENGINE' => 'InnoDB',
84+
'TABLE_COLLATION' => 'utf8mb4_general_ci',
85+
'TABLE_COMMENT' => '',
86+
'CREATE_TABLE' => 'CREATE TABLE _mysql_data_types_cache (
87+
`table` TEXT NOT NULL,
88+
`column_or_index` TEXT NOT NULL,
89+
`mysql_type` TEXT NOT NULL,
90+
PRIMARY KEY(`table`, `column_or_index`)
91+
)',
92+
'AUTO_INCREMENT' => null,
93+
'CREATE_TIME' => null,
94+
'UPDATE_TIME' => null,
95+
'CHECK_TIME' => null,
96+
'TABLE_ROWS' => '0',
97+
'DATA_LENGTH' => '0',
98+
'INDEX_LENGTH' => '0',
99+
'DATA_FREE' => '0',
100+
'VERSION' => '10',
101+
),
102+
(array) $result[0]
103+
);
104+
}
105+
76106
private function assertQuery( $sql, $error_substring = null ) {
77107
$retval = $this->engine->query( $sql );
78108
if ( null === $error_substring ) {

wp-includes/sqlite/class-wp-sqlite-translator.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,9 +1508,37 @@ private function execute_select() {
15081508
$updated_query = $this->rewriter->get_updated_query();
15091509

15101510
if ( $table_name && str_starts_with( strtolower( $table_name ), 'information_schema' ) ) {
1511-
$this->is_information_schema_query = true;
1512-
$updated_query = $this->get_information_schema_query( $updated_query );
1513-
$params = array();
1511+
// $this->is_information_schema_query = true;
1512+
// $updated_query = $this->get_information_schema_query( $updated_query );
1513+
// $params = array();
1514+
$updated_query = str_replace(
1515+
$table_name.'.tables',
1516+
"(SELECT
1517+
name as TABLE_NAME,
1518+
'database' as TABLE_SCHEMA,
1519+
CASE type
1520+
WHEN 'table' THEN 'BASE TABLE'
1521+
WHEN 'view' THEN 'VIEW'
1522+
ELSE type
1523+
END as TABLE_TYPE,
1524+
'InnoDB' as ENGINE,
1525+
'utf8mb4_general_ci' as TABLE_COLLATION,
1526+
'' as TABLE_COMMENT,
1527+
sql as CREATE_TABLE,
1528+
NULL as AUTO_INCREMENT,
1529+
NULL as CREATE_TIME,
1530+
NULL as UPDATE_TIME,
1531+
NULL as CHECK_TIME,
1532+
0 as TABLE_ROWS,
1533+
0 as DATA_LENGTH,
1534+
0 as INDEX_LENGTH,
1535+
0 as DATA_FREE,
1536+
10 as VERSION
1537+
FROM sqlite_master
1538+
WHERE type IN ('table', 'view'))",
1539+
$updated_query
1540+
);
1541+
var_dump( $updated_query );
15141542
} elseif (
15151543
// Examples: @@SESSION.sql_mode, @@GLOBAL.max_allowed_packet, @@character_set_client
15161544
preg_match( '/@@((SESSION|GLOBAL)\s*\.\s*)?\w+\b/i', $updated_query ) === 1 ||
@@ -3271,7 +3299,7 @@ private function execute_alter() {
32713299
new WP_SQLite_Token( ' ', WP_SQLite_Token::TYPE_WHITESPACE ),
32723300
new WP_SQLite_Token( 'ON', WP_SQLite_Token::TYPE_KEYWORD, WP_SQLite_Token::FLAG_KEYWORD_RESERVED ),
32733301
new WP_SQLite_Token( ' ', WP_SQLite_Token::TYPE_WHITESPACE ),
3274-
new WP_SQLite_Token( '"' . $this->table_name . '"', WP_SQLite_Token::TYPE_STRING, WP_SQLite_Token::FLAG_STRING_DOUBLE_QUOTES ),
3302+
new WP_SQLite_Token( "\"$this->table_name\"", WP_SQLite_Token::TYPE_STRING, WP_SQLite_Token::FLAG_STRING_DOUBLE_QUOTES ),
32753303
new WP_SQLite_Token( ' ', WP_SQLite_Token::TYPE_WHITESPACE ),
32763304
new WP_SQLite_Token( '(', WP_SQLite_Token::TYPE_OPERATOR ),
32773305
)

0 commit comments

Comments
 (0)