Skip to content

Commit

Permalink
Add basic support for CREATE TABLE
Browse files Browse the repository at this point in the history
  • Loading branch information
JanJakes committed Nov 20, 2024
1 parent 00ec46d commit a02f751
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
27 changes: 26 additions & 1 deletion tests/WP_SQLite_Driver_Translation_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,41 @@ public function testDelete(): void {
);
}

public function testCreateTable(): void {
$this->assertQuery(
'CREATE TABLE "t" ( "id" INT )',
'CREATE TABLE t (id INT)'
);

$this->assertQuery(
'CREATE TABLE "t" ( "id" INT , "name" TEXT )',
'CREATE TABLE t (id INT, name TEXT)'
);

$this->assertQuery(
'CREATE TABLE "t" ( "id" INT NOT NULL PRIMARY KEY )',
'CREATE TABLE t (id INT NOT NULL PRIMARY KEY)'
);
}

private function assertQuery( $expected, string $query ): void {
$driver = new WP_SQLite_Driver( new PDO( 'sqlite::memory:' ) );
$driver->query( $query );

$executed_queries = array_column( $driver->executed_sqlite_queries, 'sql' );

// Remove BEGIN and COMMIT/ROLLBACK queries.
if ( count( $executed_queries ) > 2 ) {
// Remove BEGIN and COMMIT/ROLLBACK queries.
$executed_queries = array_values( array_slice( $executed_queries, 1, -1, true ) );
}

// Remove "select changes()" executed after some queries.
if (
count( $executed_queries ) > 1
&& 'select changes()' === $executed_queries[ count( $executed_queries ) - 1 ] ) {
array_pop( $executed_queries );
}

if ( ! is_array( $expected ) ) {
$expected = array( $expected );
}
Expand Down
23 changes: 23 additions & 0 deletions wp-includes/sqlite-ast/class-wp-sqlite-driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,25 @@ private function execute_mysql_query( WP_Parser_Node $ast ) {
$this->execute_sqlite_query( $query );
$this->set_result_from_affected_rows();
break;
case 'createStatement':
$this->query_type = 'CREATE';
$subtree = $ast->get_child_node();
switch ( $subtree->rule_name ) {
case 'createTable':
$query = $this->translate( $ast );
$this->execute_sqlite_query( $query );
$this->set_result_from_affected_rows();
break;
default:
throw new Exception(
sprintf(
'Unsupported statement type: "%s" > "%s"',
$ast->rule_name,
$subtree->rule_name
)
);
}
break;
default:
throw new Exception( sprintf( 'Unsupported statement type: "%s"', $ast->rule_name ) );
}
Expand All @@ -739,6 +758,8 @@ private function translate( $ast ) {
case 'qualifiedIdentifier':
case 'dotIdentifier':
return $this->translate_sequence( $ast->get_children(), '' );
case 'identifierKeyword':
return '"' . $this->translate( $ast->get_child() ) . '"';
case 'textStringLiteral':
if ( $ast->has_child_token( WP_MySQL_Lexer::DOUBLE_QUOTED_TEXT ) ) {
return WP_SQLite_Token_Factory::double_quoted_value(
Expand All @@ -763,6 +784,8 @@ private function translate_token( WP_MySQL_Token $token ) {
return null;
case WP_MySQL_Lexer::IDENTIFIER:
return '"' . trim( $token->value, '`"' ) . '"';
case WP_MySQL_Lexer::AUTO_INCREMENT_SYMBOL:
return 'AUTOINCREMENT';
default:
return $token->value;
}
Expand Down

0 comments on commit a02f751

Please sign in to comment.