1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-18 16:15:08 +00:00
serenity/Userland/Services/SQLServer/SQLStatement.h
Timothy Flynn 2397836f8e LibSQL+SQLServer: Introduce and use ResultOr<ValueType>
The result of a SQL statement execution is either:
    1. An error.
    2. The list of rows inserted, deleted, selected, etc.

(2) is currently represented by a combination of the Result class and
the ResultSet list it holds. This worked okay, but issues start to
arise when trying to use Result in non-statement contexts (for example,
when introducing Result to SQL expression execution).

What we really need is for Result to be a thin wrapper that represents
both (1) and (2), and to not have any explicit members like a ResultSet.
So this commit removes ResultSet from Result, and introduces ResultOr,
which is just an alias for AK::ErrorOrr. Statement execution now returns
ResultOr<ResultSet> instead of Result. This further opens the door for
expression execution to return ResultOr<Value> in the future.

Lastly, this moves some other context held by Result over to ResultSet.
This includes the row count (which is really just the size of ResultSet)
and the command for which the result is for.
2022-02-10 23:11:13 +01:00

46 lines
1.1 KiB
C++

/*
* Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/NonnullRefPtr.h>
#include <AK/String.h>
#include <LibCore/Object.h>
#include <LibSQL/AST/AST.h>
#include <LibSQL/Result.h>
#include <LibSQL/ResultSet.h>
#include <SQLServer/DatabaseConnection.h>
#include <SQLServer/Forward.h>
namespace SQLServer {
class SQLStatement final : public Core::Object {
C_OBJECT(SQLStatement)
public:
~SQLStatement() override = default;
static RefPtr<SQLStatement> statement_for(int statement_id);
int statement_id() const { return m_statement_id; }
String const& sql() const { return m_sql; }
DatabaseConnection* connection() { return dynamic_cast<DatabaseConnection*>(parent()); }
void execute();
private:
SQLStatement(DatabaseConnection&, String sql);
SQL::ResultOr<void> parse();
bool should_send_result_rows() const;
void next();
void report_error(SQL::Result);
int m_statement_id;
String m_sql;
size_t m_index { 0 };
RefPtr<SQL::AST::Statement> m_statement { nullptr };
Optional<SQL::ResultSet> m_result {};
};
}