Skip to content
This repository was archived by the owner on Jun 2, 2025. It is now read-only.

Commit a220093

Browse files
committed
Implement SHOW TABLE STATUS
1 parent d39678e commit a220093

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

tests/WP_SQLite_Driver_Tests.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,14 @@ public function testShowTableStatusLike() {
768768
);"
769769
);
770770

771+
$this->assertQuery(
772+
"CREATE TABLE _another_table (
773+
ID INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
774+
option_name TEXT NOT NULL default '',
775+
option_value TEXT NOT NULL default ''
776+
);"
777+
);
778+
771779
$this->assertQuery(
772780
"SHOW TABLE STATUS LIKE '_tmp_table%';"
773781
);
@@ -781,6 +789,40 @@ public function testShowTableStatusLike() {
781789
);
782790
}
783791

792+
public function testShowTableStatusWhere() {
793+
// Created in setUp() function
794+
$this->assertQuery( 'DROP TABLE _options' );
795+
$this->assertQuery( 'DROP TABLE _dates' );
796+
797+
$this->assertQuery(
798+
"CREATE TABLE _tmp_table1 (
799+
ID INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
800+
option_name TEXT NOT NULL default '',
801+
option_value TEXT NOT NULL default ''
802+
);"
803+
);
804+
805+
$this->assertQuery(
806+
"CREATE TABLE _tmp_table2 (
807+
ID INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
808+
option_name TEXT NOT NULL default '',
809+
option_value TEXT NOT NULL default ''
810+
);"
811+
);
812+
813+
$this->assertQuery(
814+
"SHOW TABLE STATUS WHERE SUBSTR(table_name, 11, 1) = '1'"
815+
);
816+
$this->assertCount(
817+
1,
818+
$this->engine->get_query_results()
819+
);
820+
$this->assertEquals(
821+
'_tmp_table1',
822+
$this->engine->get_query_results()[0]->Name
823+
);
824+
}
825+
784826
public function testCreateTable() {
785827
$result = $this->assertQuery(
786828
"CREATE TABLE wptests_users (

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,7 @@ private function execute_drop_table_statement( WP_Parser_Node $node ): void {
13331333
$this->execute_sqlite_query( $query );
13341334
}
13351335
$this->results = $this->last_exec_returned;
1336+
$this->information_schema_builder->record_drop_table( $node );
13361337
}
13371338

13381339
private function execute_show_statement( WP_Parser_Node $node ): void {
@@ -1379,6 +1380,9 @@ private function execute_show_statement( WP_Parser_Node $node ): void {
13791380
)
13801381
);
13811382
return;
1383+
case WP_MySQL_Lexer::TABLE_SYMBOL:
1384+
$this->execute_show_table_status_statement( $node );
1385+
break;
13821386
default:
13831387
// @TODO
13841388
}
@@ -1413,6 +1417,75 @@ private function execute_show_index_statement( string $table_name ): void {
14131417
$this->set_results_from_fetched_data( $index_info );
14141418
}
14151419

1420+
private function execute_show_table_status_statement( WP_Parser_Node $node ): void {
1421+
// @TODO: Handle FROM/IN db_name.
1422+
1423+
// LIKE and WHERE clauses.
1424+
$like_or_where = $node->get_child_node( 'likeOrWhere' );
1425+
if ( null !== $like_or_where ) {
1426+
$like_clause = $like_or_where->get_child_node( 'likeClause' );
1427+
$where_clause = $like_or_where->get_child_node( 'whereClause' );
1428+
1429+
if ( null !== $like_clause ) {
1430+
$like_value = $this->translate(
1431+
$like_clause->get_child_node( 'textStringLiteral' )
1432+
);
1433+
}
1434+
1435+
if ( null !== $where_clause ) {
1436+
$where_value = $this->translate(
1437+
$where_clause->get_child_node( 'expr' )
1438+
);
1439+
}
1440+
}
1441+
1442+
// Fetch table information.
1443+
$table_info = $this->execute_sqlite_query(
1444+
sprintf(
1445+
"
1446+
SELECT *
1447+
FROM _mysql_information_schema_tables
1448+
WHERE table_type = 'BASE TABLE'
1449+
%s
1450+
%s
1451+
",
1452+
isset( $like_value ) ? "AND table_name LIKE {$like_value} ESCAPE '\\'" : '',
1453+
isset( $where_value ) ? "AND {$where_value}" : ''
1454+
)
1455+
)->fetchAll( PDO::FETCH_ASSOC );
1456+
1457+
if ( false === $table_info ) {
1458+
$this->set_results_from_fetched_data( array() );
1459+
}
1460+
1461+
// Format the results.
1462+
$tables = array();
1463+
foreach ( $table_info as $value ) {
1464+
$tables[] = (object) array(
1465+
'Name' => $value['TABLE_NAME'],
1466+
'Engine' => $value['ENGINE'],
1467+
'Version' => $value['VERSION'],
1468+
'Row_format' => $value['ROW_FORMAT'],
1469+
'Rows' => $value['TABLE_ROWS'],
1470+
'Avg_row_length' => $value['AVG_ROW_LENGTH'],
1471+
'Data_length' => $value['DATA_LENGTH'],
1472+
'Max_data_length' => $value['MAX_DATA_LENGTH'],
1473+
'Index_length' => $value['INDEX_LENGTH'],
1474+
'Data_free' => $value['DATA_FREE'],
1475+
'Auto_increment' => $value['AUTO_INCREMENT'],
1476+
'Create_time' => $value['CREATE_TIME'],
1477+
'Update_time' => $value['UPDATE_TIME'],
1478+
'Check_time' => $value['CHECK_TIME'],
1479+
'Collation' => $value['TABLE_COLLATION'],
1480+
'Checksum' => $value['CHECKSUM'],
1481+
'Create_options' => $value['CREATE_OPTIONS'],
1482+
'Comment' => $value['TABLE_COMMENT'],
1483+
);
1484+
}
1485+
1486+
$this->set_results_from_fetched_data( $tables );
1487+
}
1488+
14161489
private function execute_describe_statement( WP_Parser_Node $node ): void {
14171490
$table_name = $this->unquote_sqlite_identifier(
14181491
$this->translate( $node->get_child_node( 'tableRef' ) )

0 commit comments

Comments
 (0)