Skip to content

Commit a02f751

Browse files
committed
Add basic support for CREATE TABLE
1 parent 00ec46d commit a02f751

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

tests/WP_SQLite_Driver_Translation_Tests.php

+26-1
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,41 @@ public function testDelete(): void {
179179
);
180180
}
181181

182+
public function testCreateTable(): void {
183+
$this->assertQuery(
184+
'CREATE TABLE "t" ( "id" INT )',
185+
'CREATE TABLE t (id INT)'
186+
);
187+
188+
$this->assertQuery(
189+
'CREATE TABLE "t" ( "id" INT , "name" TEXT )',
190+
'CREATE TABLE t (id INT, name TEXT)'
191+
);
192+
193+
$this->assertQuery(
194+
'CREATE TABLE "t" ( "id" INT NOT NULL PRIMARY KEY )',
195+
'CREATE TABLE t (id INT NOT NULL PRIMARY KEY)'
196+
);
197+
}
198+
182199
private function assertQuery( $expected, string $query ): void {
183200
$driver = new WP_SQLite_Driver( new PDO( 'sqlite::memory:' ) );
184201
$driver->query( $query );
185202

186203
$executed_queries = array_column( $driver->executed_sqlite_queries, 'sql' );
204+
205+
// Remove BEGIN and COMMIT/ROLLBACK queries.
187206
if ( count( $executed_queries ) > 2 ) {
188-
// Remove BEGIN and COMMIT/ROLLBACK queries.
189207
$executed_queries = array_values( array_slice( $executed_queries, 1, -1, true ) );
190208
}
191209

210+
// Remove "select changes()" executed after some queries.
211+
if (
212+
count( $executed_queries ) > 1
213+
&& 'select changes()' === $executed_queries[ count( $executed_queries ) - 1 ] ) {
214+
array_pop( $executed_queries );
215+
}
216+
192217
if ( ! is_array( $expected ) ) {
193218
$expected = array( $expected );
194219
}

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

+23
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,25 @@ private function execute_mysql_query( WP_Parser_Node $ast ) {
716716
$this->execute_sqlite_query( $query );
717717
$this->set_result_from_affected_rows();
718718
break;
719+
case 'createStatement':
720+
$this->query_type = 'CREATE';
721+
$subtree = $ast->get_child_node();
722+
switch ( $subtree->rule_name ) {
723+
case 'createTable':
724+
$query = $this->translate( $ast );
725+
$this->execute_sqlite_query( $query );
726+
$this->set_result_from_affected_rows();
727+
break;
728+
default:
729+
throw new Exception(
730+
sprintf(
731+
'Unsupported statement type: "%s" > "%s"',
732+
$ast->rule_name,
733+
$subtree->rule_name
734+
)
735+
);
736+
}
737+
break;
719738
default:
720739
throw new Exception( sprintf( 'Unsupported statement type: "%s"', $ast->rule_name ) );
721740
}
@@ -739,6 +758,8 @@ private function translate( $ast ) {
739758
case 'qualifiedIdentifier':
740759
case 'dotIdentifier':
741760
return $this->translate_sequence( $ast->get_children(), '' );
761+
case 'identifierKeyword':
762+
return '"' . $this->translate( $ast->get_child() ) . '"';
742763
case 'textStringLiteral':
743764
if ( $ast->has_child_token( WP_MySQL_Lexer::DOUBLE_QUOTED_TEXT ) ) {
744765
return WP_SQLite_Token_Factory::double_quoted_value(
@@ -763,6 +784,8 @@ private function translate_token( WP_MySQL_Token $token ) {
763784
return null;
764785
case WP_MySQL_Lexer::IDENTIFIER:
765786
return '"' . trim( $token->value, '`"' ) . '"';
787+
case WP_MySQL_Lexer::AUTO_INCREMENT_SYMBOL:
788+
return 'AUTOINCREMENT';
766789
default:
767790
return $token->value;
768791
}

0 commit comments

Comments
 (0)