Skip to content

Commit ff09e42

Browse files
authored
Bugfixes in SHOW CREATE TABLE and SHOW TABLES (#117)
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
1 parent e476698 commit ff09e42

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

tests/WP_SQLite_Translator_Tests.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,14 @@ public function testSelectFromDual() {
253253
$this->assertEquals( 1, $result[0]->output );
254254
}
255255

256+
public function testShowCreateTableNotFound() {
257+
$this->assertQuery(
258+
'SHOW CREATE TABLE _no_such_table;'
259+
);
260+
$results = $this->engine->get_query_results();
261+
$this->assertCount( 0, $results );
262+
}
263+
256264
public function testShowCreateTable1() {
257265
$this->assertQuery(
258266
"CREATE TABLE _tmp_table (
@@ -281,6 +289,34 @@ public function testShowCreateTable1() {
281289
);
282290
}
283291

292+
public function testShowCreateTableQuoted() {
293+
$this->assertQuery(
294+
"CREATE TABLE _tmp_table (
295+
ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
296+
option_name VARCHAR(255) default '',
297+
option_value TEXT NOT NULL,
298+
UNIQUE KEY option_name (option_name),
299+
KEY composite (option_name, option_value)
300+
);"
301+
);
302+
303+
$this->assertQuery(
304+
'SHOW CREATE TABLE `_tmp_table`;'
305+
);
306+
$results = $this->engine->get_query_results();
307+
# TODO: Should we fix mismatch with original `option_value` text NOT NULL,` without default?
308+
$this->assertEquals(
309+
"CREATE TABLE _tmp_table (
310+
`ID` bigint PRIMARY KEY AUTO_INCREMENT NOT NULL,
311+
`option_name` varchar(255) DEFAULT '',
312+
`option_value` text NOT NULL DEFAULT '',
313+
KEY _tmp_table__composite (option_name, option_value),
314+
UNIQUE KEY _tmp_table__option_name (option_name)
315+
);",
316+
$results[0]->{'Create Table'}
317+
);
318+
}
319+
284320
public function testShowCreateTableSimpleTable() {
285321
$this->assertQuery(
286322
'CREATE TABLE _tmp_table (
@@ -417,9 +453,7 @@ public function testShowTablesLike() {
417453
);
418454
$this->assertEquals(
419455
array(
420-
(object) array(
421-
'Tables_in_db' => '_tmp_table',
422-
),
456+
'_tmp_table',
423457
),
424458
$this->engine->get_query_results()
425459
);

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3238,8 +3238,8 @@ private function execute_drop() {
32383238
*/
32393239
private function execute_show() {
32403240
$this->rewriter->skip();
3241-
$what1 = $this->rewriter->consume()->token;
3242-
$what2 = $this->rewriter->consume()->token;
3241+
$what1 = strtoupper( $this->rewriter->consume()->token );
3242+
$what2 = strtoupper( $this->rewriter->consume()->token );
32433243
$what = $what1 . ' ' . $what2;
32443244
switch ( $what ) {
32453245
case 'CREATE PROCEDURE':
@@ -3338,10 +3338,18 @@ private function execute_show() {
33383338
return;
33393339

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

3346+
if ( empty( $columns ) ) {
3347+
$this->set_results_from_fetched_data(
3348+
array()
3349+
);
3350+
return;
3351+
}
3352+
33453353
foreach ( $columns as $column ) {
33463354
$column = (array) $column;
33473355
$definition = '';
@@ -3452,8 +3460,12 @@ private function execute_show() {
34523460
':param' => $table_expression->value,
34533461
)
34543462
);
3463+
34553464
$this->set_results_from_fetched_data(
3456-
$stmt->fetchAll( $this->pdo_fetch_mode )
3465+
array_column(
3466+
$stmt->fetchAll( $this->pdo_fetch_mode ),
3467+
'Tables_in_db'
3468+
)
34573469
);
34583470
return;
34593471

@@ -3464,7 +3476,10 @@ private function execute_show() {
34643476
"SELECT name FROM sqlite_master WHERE type='table'"
34653477
);
34663478
$this->set_results_from_fetched_data(
3467-
$stmt->fetchAll( $this->pdo_fetch_mode )
3479+
array_column(
3480+
$stmt->fetchAll( $this->pdo_fetch_mode ),
3481+
'Tables_in_db'
3482+
)
34683483
);
34693484
return;
34703485

0 commit comments

Comments
 (0)