Skip to content

Commit

Permalink
Bugfixes in SHOW CREATE TABLE and SHOW TABLES (#117)
Browse files Browse the repository at this point in the history
Fixes a series of crashes encountered when

* SHOW TABLES now returns a flat list, not an array of objects – I'm not
sure here. It makes `$tables = $wpdb->get_results("SHOW TABLES LIKE
'{$prefix}%'", ARRAY_N);` work as expected
[here](https://github.com/WordPress/playground-tools/blob/128123e84e25cf0421770f6885c6f26aaf0b05dc/packages/playground/src/playground-db.php#L90-L91),
but is the right thing to return, or is this more about correct handling
of the fetch mode, like ARRAY_N?
* `SHOW tables` isn't all uppercase
* There's no table tho SHOW CREATE: `SHOW CREATE TABLE _no_such_table;`
* The table identifier passed to SHOW CREATE is quoted:

```
'SHOW CREATETABLE `_tmp_table`;'
```

This PR makes the [Sandbox site
plugin](https://wordpress.org/plugins/playground/) work in Playground

cc @bgrgicak @brandonpayton @wojtekn
  • Loading branch information
adamziel authored Jun 5, 2024
1 parent e476698 commit ff09e42
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
40 changes: 37 additions & 3 deletions tests/WP_SQLite_Translator_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,14 @@ public function testSelectFromDual() {
$this->assertEquals( 1, $result[0]->output );
}

public function testShowCreateTableNotFound() {
$this->assertQuery(
'SHOW CREATE TABLE _no_such_table;'
);
$results = $this->engine->get_query_results();
$this->assertCount( 0, $results );
}

public function testShowCreateTable1() {
$this->assertQuery(
"CREATE TABLE _tmp_table (
Expand Down Expand Up @@ -281,6 +289,34 @@ public function testShowCreateTable1() {
);
}

public function testShowCreateTableQuoted() {
$this->assertQuery(
"CREATE TABLE _tmp_table (
ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
option_name VARCHAR(255) default '',
option_value TEXT NOT NULL,
UNIQUE KEY option_name (option_name),
KEY composite (option_name, option_value)
);"
);

$this->assertQuery(
'SHOW CREATE TABLE `_tmp_table`;'
);
$results = $this->engine->get_query_results();
# TODO: Should we fix mismatch with original `option_value` text NOT NULL,` without default?
$this->assertEquals(
"CREATE TABLE _tmp_table (
`ID` bigint PRIMARY KEY AUTO_INCREMENT NOT NULL,
`option_name` varchar(255) DEFAULT '',
`option_value` text NOT NULL DEFAULT '',
KEY _tmp_table__composite (option_name, option_value),
UNIQUE KEY _tmp_table__option_name (option_name)
);",
$results[0]->{'Create Table'}
);
}

public function testShowCreateTableSimpleTable() {
$this->assertQuery(
'CREATE TABLE _tmp_table (
Expand Down Expand Up @@ -417,9 +453,7 @@ public function testShowTablesLike() {
);
$this->assertEquals(
array(
(object) array(
'Tables_in_db' => '_tmp_table',
),
'_tmp_table',
),
$this->engine->get_query_results()
);
Expand Down
25 changes: 20 additions & 5 deletions wp-includes/sqlite/class-wp-sqlite-translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3238,8 +3238,8 @@ private function execute_drop() {
*/
private function execute_show() {
$this->rewriter->skip();
$what1 = $this->rewriter->consume()->token;
$what2 = $this->rewriter->consume()->token;
$what1 = strtoupper( $this->rewriter->consume()->token );
$what2 = strtoupper( $this->rewriter->consume()->token );
$what = $what1 . ' ' . $what2;
switch ( $what ) {
case 'CREATE PROCEDURE':
Expand Down Expand Up @@ -3338,10 +3338,18 @@ private function execute_show() {
return;

case 'CREATE TABLE':
$table_name = $this->rewriter->consume()->token;
// Value is unquoted table name
$table_name = $this->rewriter->consume()->value;
$columns = $this->get_columns_from( $table_name );
$keys = $this->get_keys( $table_name );

if ( empty( $columns ) ) {
$this->set_results_from_fetched_data(
array()
);
return;
}

foreach ( $columns as $column ) {
$column = (array) $column;
$definition = '';
Expand Down Expand Up @@ -3452,8 +3460,12 @@ private function execute_show() {
':param' => $table_expression->value,
)
);

$this->set_results_from_fetched_data(
$stmt->fetchAll( $this->pdo_fetch_mode )
array_column(
$stmt->fetchAll( $this->pdo_fetch_mode ),
'Tables_in_db'
)
);
return;

Expand All @@ -3464,7 +3476,10 @@ private function execute_show() {
"SELECT name FROM sqlite_master WHERE type='table'"
);
$this->set_results_from_fetched_data(
$stmt->fetchAll( $this->pdo_fetch_mode )
array_column(
$stmt->fetchAll( $this->pdo_fetch_mode ),
'Tables_in_db'
)
);
return;

Expand Down

0 comments on commit ff09e42

Please sign in to comment.