Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for SHOW TABLE STATUS LIKE queries #77

Merged
merged 3 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions tests/WP_SQLite_Translator_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
50 changes: 47 additions & 3 deletions wp-includes/sqlite/class-wp-sqlite-translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"
<<<SQL
SELECT
name as `Name`,
'myisam' as `Engine`,
10 as `Version`,
'Fixed' as `Row_format`,
0 as `Rows`,
0 as `Avg_row_length`,
0 as `Data_length`,
0 as `Max_data_length`,
0 as `Index_length`,
0 as `Data_free` ,
0 as `Auto_increment`,
'2024-03-20 15:33:20' as `Create_time`,
'2024-03-20 15:33:20' as `Update_time`,
null as `Check_time`,
null as `Collation`,
null as `Checksum`,
'' as `Create_options`,
'' as `Comment`
FROM sqlite_master
WHERE
type='table'
AND name LIKE :pattern
ORDER BY name
SQL,

array(
':pattern' => $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
Expand Down