diff --git a/tests/WP_SQLite_Translator_Tests.php b/tests/WP_SQLite_Translator_Tests.php index d8537dca..02c285a5 100644 --- a/tests/WP_SQLite_Translator_Tests.php +++ b/tests/WP_SQLite_Translator_Tests.php @@ -224,6 +224,119 @@ public function testShowTablesLike() { ); } + public function testShowTableStatusFrom() + { + // Created in setUp() function + $this->assertQuery("DROP TABLE _options"); + $this->assertQuery("DROP TABLE _dates"); + + $this->assertQuery( + "CREATE TABLE _tmp_table ( + ID INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL, + option_name TEXT NOT NULL default '', + option_value TEXT NOT NULL default '' + );" + ); + + $this->assertQuery( + "SHOW TABLE STATUS FROM 'mydb';" + ); + + $this->assertCount( + 1, + $this->engine->get_query_results() + ); + } + + public function testShowTableStatusIn() + { + // Created in setUp() function + $this->assertQuery("DROP TABLE _options"); + $this->assertQuery("DROP TABLE _dates"); + + $this->assertQuery( + "CREATE TABLE _tmp_table ( + ID INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL, + option_name TEXT NOT NULL default '', + option_value TEXT NOT NULL default '' + );" + ); + + $this->assertQuery( + "SHOW TABLE STATUS IN 'mydb';" + ); + + $this->assertCount( + 1, + $this->engine->get_query_results() + ); + } + + public function testShowTableStatusInTwoTables() + { + // Created in setUp() function + $this->assertQuery("DROP TABLE _options"); + $this->assertQuery("DROP TABLE _dates"); + + $this->assertQuery( + "CREATE TABLE _tmp_table ( + ID INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL, + option_name TEXT NOT NULL default '', + option_value TEXT NOT NULL default '' + );" + ); + + $this->assertQuery( + "CREATE TABLE _tmp_table2 ( + ID INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL, + option_name TEXT NOT NULL default '', + option_value TEXT NOT NULL default '' + );" + ); + $this->assertQuery( + "SHOW TABLE STATUS IN 'mydb';" + ); + + $this->assertCount( + 2, + $this->engine->get_query_results() + ); + } + + public function testShowTableStatusLike() { + // Created in setUp() function + $this->assertQuery("DROP TABLE _options"); + $this->assertQuery("DROP TABLE _dates"); + + $this->assertQuery( + "CREATE TABLE _tmp_table1 ( + ID INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL, + option_name TEXT NOT NULL default '', + option_value TEXT NOT NULL default '' + );" + ); + + $this->assertQuery( + "CREATE TABLE _tmp_table2 ( + ID INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL, + option_name TEXT NOT NULL default '', + option_value TEXT NOT NULL default '' + );" + ); + + $this->assertQuery( + "SHOW TABLE STATUS LIKE '_tmp_table%';" + ); + $this->assertCount( + 2, + $this->engine->get_query_results() + ); + $this->assertEquals( + '_tmp_table1', + $this->engine->get_query_results()[0]->Name + ); + } + public function testCreateTable() { $result = $this->assertQuery( "CREATE TABLE wptests_users ( diff --git a/wp-includes/sqlite/class-wp-sqlite-translator.php b/wp-includes/sqlite/class-wp-sqlite-translator.php index e4899194..7054a445 100644 --- a/wp-includes/sqlite/class-wp-sqlite-translator.php +++ b/wp-includes/sqlite/class-wp-sqlite-translator.php @@ -3187,12 +3187,56 @@ function ( $row ) use ( $name_map ) { return; case 'TABLE STATUS': // FROM `database`. - $this->rewriter->skip(); + // Match the optional [{FROM | IN} db_name] + $database_expression = $this->rewriter->consume(); + if ( $database_expression->token === 'FROM' || $database_expression->token === 'IN' ) { + $this->rewriter->consume(); + $database_expression = $this->rewriter->consume(); + } + + $pattern = '%'; + // [LIKE 'pattern' | WHERE expr] + if($database_expression->token === 'LIKE') { + $pattern = $this->rewriter->consume()->value; + } else if($database_expression->token === 'WHERE') { + // @TODO Support me please. + } else if($database_expression->token !== ';') { + throw new Exception( 'Syntax error: Unexpected token ' . $database_expression->token .' in query '. $this->mysql_query ); + } + $database_expression = $this->rewriter->skip(); $stmt = $this->execute_sqlite_query( - "SELECT name as `Name`, 'myisam' as `Engine`, 0 as `Data_length`, 0 as `Index_length`, 0 as `Data_free` FROM sqlite_master WHERE type='table' ORDER BY name" + << $pattern, + ) ); - $tables = $this->strip_sqlite_system_tables( $stmt->fetchAll( $this->pdo_fetch_mode ) ); foreach ( $tables as $table ) { $table_name = $table->Name; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase