Skip to content

Commit cf6008d

Browse files
committed
Implement DESCRIBE using information schema
1 parent f11fb09 commit cf6008d

File tree

2 files changed

+80
-33
lines changed

2 files changed

+80
-33
lines changed

tests/WP_SQLite_Driver_Tests.php

+39-33
Original file line numberDiff line numberDiff line change
@@ -724,14 +724,14 @@ public function testCreateTable() {
724724
'Type' => 'bigint(20) unsigned',
725725
'Null' => 'NO',
726726
'Key' => 'PRI',
727-
'Default' => '0',
728-
'Extra' => '',
727+
'Default' => null,
728+
'Extra' => 'auto_increment',
729729
),
730730
(object) array(
731731
'Field' => 'user_login',
732732
'Type' => 'varchar(60)',
733733
'Null' => 'NO',
734-
'Key' => '',
734+
'Key' => 'MUL',
735735
'Default' => '',
736736
'Extra' => '',
737737
),
@@ -747,15 +747,15 @@ public function testCreateTable() {
747747
'Field' => 'user_nicename',
748748
'Type' => 'varchar(50)',
749749
'Null' => 'NO',
750-
'Key' => '',
750+
'Key' => 'MUL',
751751
'Default' => '',
752752
'Extra' => '',
753753
),
754754
(object) array(
755755
'Field' => 'user_email',
756756
'Type' => 'varchar(100)',
757757
'Null' => 'NO',
758-
'Key' => '',
758+
'Key' => 'MUL',
759759
'Default' => '',
760760
'Extra' => '',
761761
),
@@ -848,8 +848,8 @@ enum_column ENUM('a', 'b', 'c') NOT NULL DEFAULT 'a',
848848
'Type' => 'bigint(20) unsigned',
849849
'Null' => 'NO',
850850
'Key' => 'PRI',
851-
'Default' => '0',
852-
'Extra' => '',
851+
'Default' => null,
852+
'Extra' => 'auto_increment',
853853
),
854854
(object) array(
855855
'Field' => 'decimal_column',
@@ -1052,7 +1052,7 @@ public function testColumnWithOnUpdate() {
10521052
'Type' => 'int(11)',
10531053
'Null' => 'NO',
10541054
'Key' => '',
1055-
'Default' => '0',
1055+
'Default' => null,
10561056
'Extra' => '',
10571057
),
10581058
(object) array(
@@ -1061,7 +1061,7 @@ public function testColumnWithOnUpdate() {
10611061
'Null' => 'YES',
10621062
'Key' => '',
10631063
'Default' => null,
1064-
'Extra' => '',
1064+
'Extra' => 'on update CURRENT_TIMESTAMP',
10651065
),
10661066
),
10671067
$results
@@ -1079,7 +1079,7 @@ public function testColumnWithOnUpdate() {
10791079
'Type' => 'int(11)',
10801080
'Null' => 'NO',
10811081
'Key' => '',
1082-
'Default' => '0',
1082+
'Default' => null,
10831083
'Extra' => '',
10841084
),
10851085
(object) array(
@@ -1088,15 +1088,15 @@ public function testColumnWithOnUpdate() {
10881088
'Null' => 'YES',
10891089
'Key' => '',
10901090
'Default' => null,
1091-
'Extra' => '',
1091+
'Extra' => 'on update CURRENT_TIMESTAMP',
10921092
),
10931093
(object) array(
10941094
'Field' => 'updated_at',
10951095
'Type' => 'timestamp',
10961096
'Null' => 'YES',
10971097
'Key' => '',
10981098
'Default' => null,
1099-
'Extra' => '',
1099+
'Extra' => 'on update CURRENT_TIMESTAMP',
11001100
),
11011101
),
11021102
$results
@@ -1221,7 +1221,7 @@ public function testChangeColumnWithOnUpdate() {
12211221
'Type' => 'int(11)',
12221222
'Null' => 'NO',
12231223
'Key' => '',
1224-
'Default' => '0',
1224+
'Default' => null,
12251225
'Extra' => '',
12261226
),
12271227
(object) array(
@@ -1254,7 +1254,7 @@ public function testChangeColumnWithOnUpdate() {
12541254
'Type' => 'int(11)',
12551255
'Null' => 'NO',
12561256
'Key' => '',
1257-
'Default' => '0',
1257+
'Default' => null,
12581258
'Extra' => '',
12591259
),
12601260
(object) array(
@@ -1263,7 +1263,7 @@ public function testChangeColumnWithOnUpdate() {
12631263
'Null' => 'YES',
12641264
'Key' => '',
12651265
'Default' => null,
1266-
'Extra' => '',
1266+
'Extra' => 'on update CURRENT_TIMESTAMP',
12671267
),
12681268
),
12691269
$results
@@ -1286,7 +1286,7 @@ public function testChangeColumnWithOnUpdate() {
12861286
'Type' => 'int(11)',
12871287
'Null' => 'NO',
12881288
'Key' => '',
1289-
'Default' => '0',
1289+
'Default' => null,
12901290
'Extra' => '',
12911291
),
12921292
(object) array(
@@ -1328,15 +1328,15 @@ public function testAlterTableWithColumnFirstAndAfter() {
13281328
'Type' => 'int(11)',
13291329
'Null' => 'NO',
13301330
'Key' => '',
1331-
'Default' => '0',
1331+
'Default' => null,
13321332
'Extra' => '',
13331333
),
13341334
(object) array(
13351335
'Field' => 'name',
13361336
'Type' => 'varchar(20)',
13371337
'Null' => 'NO',
13381338
'Key' => '',
1339-
'Default' => null,
1339+
'Default' => '',
13401340
'Extra' => '',
13411341
),
13421342
(object) array(
@@ -1363,7 +1363,7 @@ public function testAlterTableWithColumnFirstAndAfter() {
13631363
'Type' => 'int(11)',
13641364
'Null' => 'NO',
13651365
'Key' => '',
1366-
'Default' => '0',
1366+
'Default' => null,
13671367
'Extra' => '',
13681368
),
13691369
(object) array(
@@ -1457,7 +1457,7 @@ public function testAlterTableWithColumnFirstAndAfter() {
14571457
'Type' => 'varchar(20)',
14581458
'Null' => 'NO',
14591459
'Key' => '',
1460-
'Default' => null,
1460+
'Default' => '',
14611461
'Extra' => '',
14621462
),
14631463
(object) array(
@@ -1503,7 +1503,7 @@ public function testAlterTableWithMultiColumnFirstAndAfter() {
15031503
'Type' => 'int(11)',
15041504
'Null' => 'NO',
15051505
'Key' => '',
1506-
'Default' => '0',
1506+
'Default' => null,
15071507
'Extra' => '',
15081508
),
15091509
(object) array(
@@ -1549,7 +1549,7 @@ public function testAlterTableWithMultiColumnFirstAndAfter() {
15491549
'Type' => 'int(11)',
15501550
'Null' => 'NO',
15511551
'Key' => '',
1552-
'Default' => '0',
1552+
'Default' => null,
15531553
'Extra' => '',
15541554
),
15551555
(object) array(
@@ -1600,7 +1600,7 @@ public function testAlterTableAddIndex() {
16001600
'Table' => '_tmp_table',
16011601
'Non_unique' => '1',
16021602
'Key_name' => 'name',
1603-
'Seq_in_index' => '0',
1603+
'Seq_in_index' => '1',
16041604
'Column_name' => 'name',
16051605
'Collation' => 'A',
16061606
'Cardinality' => '0',
@@ -1610,6 +1610,8 @@ public function testAlterTableAddIndex() {
16101610
'Index_type' => 'BTREE',
16111611
'Comment' => '',
16121612
'Index_comment' => '',
1613+
'Visible' => 'YES',
1614+
'Expression' => null,
16131615
),
16141616
),
16151617
$results
@@ -1635,16 +1637,18 @@ public function testAlterTableAddUniqueIndex() {
16351637
'Table' => '_tmp_table',
16361638
'Non_unique' => '0',
16371639
'Key_name' => 'name',
1638-
'Seq_in_index' => '0',
1640+
'Seq_in_index' => '1',
16391641
'Column_name' => 'name',
16401642
'Collation' => 'A',
16411643
'Cardinality' => '0',
1642-
'Sub_part' => null,
1644+
'Sub_part' => '20',
16431645
'Packed' => null,
16441646
'Null' => '',
16451647
'Index_type' => 'BTREE',
16461648
'Comment' => '',
16471649
'Index_comment' => '',
1650+
'Visible' => 'YES',
1651+
'Expression' => null,
16481652
),
16491653
),
16501654
$results
@@ -1670,16 +1674,18 @@ public function testAlterTableAddFulltextIndex() {
16701674
'Table' => '_tmp_table',
16711675
'Non_unique' => '1',
16721676
'Key_name' => 'name',
1673-
'Seq_in_index' => '0',
1677+
'Seq_in_index' => '1',
16741678
'Column_name' => 'name',
1675-
'Collation' => 'A',
1679+
'Collation' => null,
16761680
'Cardinality' => '0',
16771681
'Sub_part' => null,
16781682
'Packed' => null,
16791683
'Null' => '',
16801684
'Index_type' => 'FULLTEXT',
16811685
'Comment' => '',
16821686
'Index_comment' => '',
1687+
'Visible' => 'YES',
1688+
'Expression' => null,
16831689
),
16841690
),
16851691
$results
@@ -2167,15 +2173,14 @@ public function testNestedTransactionWorkComplexModify() {
21672173
$fields = $this->engine->get_query_results();
21682174

21692175
$this->assertEquals(
2170-
$fields,
21712176
array(
21722177
(object) array(
21732178
'Field' => 'ID',
2174-
'Type' => 'integer',
2179+
'Type' => 'int',
21752180
'Null' => 'NO',
21762181
'Key' => 'PRI',
2177-
'Default' => '0',
2178-
'Extra' => '',
2182+
'Default' => null,
2183+
'Extra' => 'auto_increment',
21792184
),
21802185
(object) array(
21812186
'Field' => 'option_name',
@@ -2193,7 +2198,8 @@ public function testNestedTransactionWorkComplexModify() {
21932198
'Default' => '',
21942199
'Extra' => '',
21952200
),
2196-
)
2201+
),
2202+
$fields
21972203
);
21982204
}
21992205

@@ -2426,7 +2432,7 @@ public function testDescribeAccurate() {
24262432
'Field' => 'term_name',
24272433
'Type' => 'varchar(11)',
24282434
'Null' => 'NO',
2429-
'Key' => '',
2435+
'Key' => 'MUL',
24302436
'Default' => '0',
24312437
'Extra' => '',
24322438
),

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

+41
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,23 @@ private function execute_mysql_query( WP_Parser_Node $ast ) {
917917
$this->query_type = 'SHOW';
918918
$this->execute_show_statement( $ast );
919919
break;
920+
case 'utilityStatement':
921+
$this->query_type = 'DESCRIBE';
922+
$subtree = $ast->get_child_node();
923+
switch ( $subtree->rule_name ) {
924+
case 'describeStatement':
925+
$this->execute_describe_statement( $subtree );
926+
break;
927+
default:
928+
throw new Exception(
929+
sprintf(
930+
'Unsupported statement type: "%s" > "%s"',
931+
$ast->rule_name,
932+
$subtree->rule_name
933+
)
934+
);
935+
}
936+
break;
920937
default:
921938
throw new Exception( sprintf( 'Unsupported statement type: "%s"', $ast->rule_name ) );
922939
}
@@ -1209,6 +1226,30 @@ private function execute_show_index_statement( string $table_name ): void {
12091226
$this->set_results_from_fetched_data( $index_info );
12101227
}
12111228

1229+
private function execute_describe_statement( WP_Parser_Node $node ): void {
1230+
$table_name = $this->unquote_sqlite_identifier(
1231+
$this->translate( $node->get_child_node( 'tableRef' ) )
1232+
);
1233+
1234+
$column_info = $this->execute_sqlite_query(
1235+
'
1236+
SELECT
1237+
column_name AS `Field`,
1238+
column_type AS `Type`,
1239+
is_nullable AS `Null`,
1240+
column_key AS `Key`,
1241+
column_default AS `Default`,
1242+
extra AS Extra
1243+
FROM _mysql_information_schema_columns
1244+
WHERE table_schema = ?
1245+
AND table_name = ?
1246+
',
1247+
array( $this->db_name, $table_name )
1248+
)->fetchAll( PDO::FETCH_OBJ );
1249+
1250+
$this->set_results_from_fetched_data( $column_info );
1251+
}
1252+
12121253
private function translate( $ast ) {
12131254
if ( null === $ast ) {
12141255
return null;

0 commit comments

Comments
 (0)