From 1c50e9aadcb16ac5963013444392bab21155bad3 Mon Sep 17 00:00:00 2001 From: Jan de Visser Date: Tue, 2 Nov 2021 16:43:57 -0400 Subject: [PATCH] LibSQL: Add current statement to the ExecutionContext Because SQL is the craptastic language that it is, sometimes expressions need to know details about the calling statement. For example the tables in the 'FROM' clause may be needed to determine which columns are referenced in 'WHERE' expressions. So the current statement is added to the ExecutionContext and a new 'execute' overload on Statement is created which takes the Database and the Statement and builds an ExecutionContaxt from those. --- Tests/LibSQL/TestSqlStatementExecution.cpp | 3 +-- Userland/Libraries/LibSQL/AST/AST.h | 2 ++ Userland/Libraries/LibSQL/AST/Statement.cpp | 20 ++++++++++++++++++++ Userland/Libraries/LibSQL/CMakeLists.txt | 1 + Userland/Services/SQLServer/SQLStatement.cpp | 3 +-- 5 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 Userland/Libraries/LibSQL/AST/Statement.cpp diff --git a/Tests/LibSQL/TestSqlStatementExecution.cpp b/Tests/LibSQL/TestSqlStatementExecution.cpp index e64ea6b32b1..a7f0c0fbe40 100644 --- a/Tests/LibSQL/TestSqlStatementExecution.cpp +++ b/Tests/LibSQL/TestSqlStatementExecution.cpp @@ -25,8 +25,7 @@ RefPtr execute(NonnullRefPtr database, String con EXPECT(!parser.has_errors()); if (parser.has_errors()) outln("{}", parser.errors()[0].to_string()); - SQL::AST::ExecutionContext context { database }; - auto result = statement->execute(context); + auto result = statement->execute(move(database)); if (result->error().code != SQL::SQLErrorCode::NoError) outln("{}", result->error().to_string()); return result; diff --git a/Userland/Libraries/LibSQL/AST/AST.h b/Userland/Libraries/LibSQL/AST/AST.h index ca2c50946bb..b0ee4c72622 100644 --- a/Userland/Libraries/LibSQL/AST/AST.h +++ b/Userland/Libraries/LibSQL/AST/AST.h @@ -298,6 +298,7 @@ private: struct ExecutionContext { NonnullRefPtr database; RefPtr result { nullptr }; + class Statement const* statement; Tuple* current_row { nullptr }; }; @@ -722,6 +723,7 @@ private: class Statement : public ASTNode { public: + RefPtr execute(AK::NonnullRefPtr database) const; virtual RefPtr execute(ExecutionContext&) const { return nullptr; } }; diff --git a/Userland/Libraries/LibSQL/AST/Statement.cpp b/Userland/Libraries/LibSQL/AST/Statement.cpp new file mode 100644 index 00000000000..b9c379da173 --- /dev/null +++ b/Userland/Libraries/LibSQL/AST/Statement.cpp @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2021, Jan de Visser + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include + +namespace SQL::AST { + +RefPtr Statement::execute(AK::NonnullRefPtr database) const +{ + ExecutionContext context { move(database), nullptr, this, nullptr }; + return execute(context); +} + +} diff --git a/Userland/Libraries/LibSQL/CMakeLists.txt b/Userland/Libraries/LibSQL/CMakeLists.txt index 744d7980aac..1bfa86baa1e 100644 --- a/Userland/Libraries/LibSQL/CMakeLists.txt +++ b/Userland/Libraries/LibSQL/CMakeLists.txt @@ -6,6 +6,7 @@ set(SOURCES AST/Lexer.cpp AST/Parser.cpp AST/Select.cpp + AST/Statement.cpp AST/SyntaxHighlighter.cpp AST/Token.cpp BTree.cpp diff --git a/Userland/Services/SQLServer/SQLStatement.cpp b/Userland/Services/SQLServer/SQLStatement.cpp index 87c726beee3..5c62b7a9a7f 100644 --- a/Userland/Services/SQLServer/SQLStatement.cpp +++ b/Userland/Services/SQLServer/SQLStatement.cpp @@ -67,8 +67,7 @@ void SQLStatement::execute() return; } VERIFY(!connection()->database().is_null()); - SQL::AST::ExecutionContext context { connection()->database().release_nonnull() }; - m_result = m_statement->execute(context); + m_result = m_statement->execute(connection()->database().release_nonnull()); if (m_result->error().code != SQL::SQLErrorCode::NoError) { report_error(m_result->error()); return;