From a02f751c089cdd64ffc1a28949916809522f70df Mon Sep 17 00:00:00 2001 From: Jan Jakes Date: Wed, 20 Nov 2024 17:00:36 +0100 Subject: [PATCH] Add basic support for CREATE TABLE --- tests/WP_SQLite_Driver_Translation_Tests.php | 27 ++++++++++++++++++- .../sqlite-ast/class-wp-sqlite-driver.php | 23 ++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/tests/WP_SQLite_Driver_Translation_Tests.php b/tests/WP_SQLite_Driver_Translation_Tests.php index 0b754242..2d8ac413 100644 --- a/tests/WP_SQLite_Driver_Translation_Tests.php +++ b/tests/WP_SQLite_Driver_Translation_Tests.php @@ -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 ); } diff --git a/wp-includes/sqlite-ast/class-wp-sqlite-driver.php b/wp-includes/sqlite-ast/class-wp-sqlite-driver.php index 328fc77c..a275f3ad 100644 --- a/wp-includes/sqlite-ast/class-wp-sqlite-driver.php +++ b/wp-includes/sqlite-ast/class-wp-sqlite-driver.php @@ -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 ) ); } @@ -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( @@ -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; }