From 1037d6b0eb453a11d58ab7a821a2fe1c5e82f325 Mon Sep 17 00:00:00 2001 From: Jan de Visser Date: Sun, 27 Jun 2021 21:32:22 -0400 Subject: [PATCH] LibSQL: Invent statement execution machinery and CREATE SCHEMA statement This patch introduces the ability execute parsed SQL statements. The abstract AST Statement node now has a virtual 'execute' method. This method takes a Database object as parameter and returns a SQLResult object. Also introduced here is the CREATE SCHEMA statement. Tables live in a schema, and if no schema is present in a table reference the 'default' schema is implied. This schema is created if it doesn't yet exist when a Database object is created. Finally, as a proof of concept, the CREATE SCHEMA and CREATE TABLE statements received an 'execute' implementation. The CREATE TABLE method is not able to create tables created from SQL queries yet. --- Userland/Libraries/LibSQL/AST/AST.h | 22 ++++++++++ .../Libraries/LibSQL/AST/CreateSchema.cpp | 28 ++++++++++++ Userland/Libraries/LibSQL/AST/CreateTable.cpp | 44 +++++++++++++++++++ Userland/Libraries/LibSQL/AST/Parser.cpp | 22 +++++++++- Userland/Libraries/LibSQL/AST/Parser.h | 1 + Userland/Libraries/LibSQL/AST/Token.h | 1 + Userland/Libraries/LibSQL/CMakeLists.txt | 36 ++++++++------- Userland/Libraries/LibSQL/Database.cpp | 7 ++- Userland/Libraries/LibSQL/Tuple.h | 19 +++----- 9 files changed, 147 insertions(+), 33 deletions(-) create mode 100644 Userland/Libraries/LibSQL/AST/CreateSchema.cpp create mode 100644 Userland/Libraries/LibSQL/AST/CreateTable.cpp diff --git a/Userland/Libraries/LibSQL/AST/AST.h b/Userland/Libraries/LibSQL/AST/AST.h index 855c0bbd26..cd9001cf31 100644 --- a/Userland/Libraries/LibSQL/AST/AST.h +++ b/Userland/Libraries/LibSQL/AST/AST.h @@ -666,11 +666,31 @@ private: //================================================================================================== class Statement : public ASTNode { +public: + virtual RefPtr execute(NonnullRefPtr) const { return nullptr; } }; class ErrorStatement final : public Statement { }; +class CreateSchema : public Statement { +public: + CreateSchema(String schema_name, bool is_error_if_schema_exists) + : m_schema_name(move(schema_name)) + , m_is_error_if_schema_exists(is_error_if_schema_exists) + { + } + + const String& schema_name() const { return m_schema_name; } + bool is_error_if_schema_exists() const { return m_is_error_if_schema_exists; } + + RefPtr execute(NonnullRefPtr) const override; + +private: + String m_schema_name; + bool m_is_error_if_schema_exists; +}; + class CreateTable : public Statement { public: CreateTable(String schema_name, String table_name, RefPtr