Serenity Operating System
1/*
2 * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/DeprecatedString.h>
10#include <AK/NonnullRefPtr.h>
11#include <AK/Vector.h>
12#include <LibCore/Object.h>
13#include <LibSQL/AST/AST.h>
14#include <LibSQL/Result.h>
15#include <LibSQL/ResultSet.h>
16#include <LibSQL/Type.h>
17#include <SQLServer/DatabaseConnection.h>
18#include <SQLServer/Forward.h>
19
20namespace SQLServer {
21
22class SQLStatement final : public Core::Object {
23 C_OBJECT_ABSTRACT(SQLStatement)
24
25public:
26 static SQL::ResultOr<NonnullRefPtr<SQLStatement>> create(DatabaseConnection&, StringView sql);
27 ~SQLStatement() override = default;
28
29 static RefPtr<SQLStatement> statement_for(SQL::StatementID statement_id);
30 SQL::StatementID statement_id() const { return m_statement_id; }
31 DatabaseConnection* connection() { return dynamic_cast<DatabaseConnection*>(parent()); }
32 Optional<SQL::ExecutionID> execute(Vector<SQL::Value> placeholder_values);
33
34private:
35 SQLStatement(DatabaseConnection&, NonnullRefPtr<SQL::AST::Statement> statement);
36
37 bool should_send_result_rows(SQL::ResultSet const& result) const;
38 void next(SQL::ExecutionID execution_id, SQL::ResultSet result, size_t result_size);
39 void report_error(SQL::Result, SQL::ExecutionID execution_id);
40
41 SQL::StatementID m_statement_id { 0 };
42
43 HashTable<SQL::ExecutionID> m_ongoing_executions;
44 SQL::ExecutionID m_next_execution_id { 0 };
45
46 NonnullRefPtr<SQL::AST::Statement> m_statement;
47};
48
49}