1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:37:35 +00:00

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.
This commit is contained in:
Timothy Flynn 2022-02-10 14:43:00 -05:00 committed by Andreas Kling
parent 6409618413
commit 2397836f8e
15 changed files with 259 additions and 330 deletions

View file

@ -9,8 +9,6 @@
#include <AK/Error.h>
#include <AK/Noncopyable.h>
#include <LibSQL/ResultSet.h>
#include <LibSQL/Tuple.h>
#include <LibSQL/Type.h>
namespace SQL {
@ -75,11 +73,8 @@ enum class SQLErrorCode {
class [[nodiscard]] Result {
public:
ALWAYS_INLINE Result(SQLCommand command, size_t update_count = 0, size_t insert_count = 0, size_t delete_count = 0)
ALWAYS_INLINE Result(SQLCommand command)
: m_command(command)
, m_update_count(update_count)
, m_insert_count(insert_count)
, m_delete_count(delete_count)
{
}
@ -109,19 +104,9 @@ public:
SQLErrorCode error() const { return m_error; }
String error_string() const;
void insert(Tuple const& row, Tuple const& sort_key);
void limit(size_t offset, size_t limit);
bool has_results() const { return m_result_set.has_value(); }
ResultSet const& results() const { return m_result_set.value(); }
size_t updated() const { return m_update_count; }
size_t inserted() const { return m_insert_count; }
size_t deleted() const { return m_delete_count; }
// These are for compatibility with the TRY() macro in AK.
[[nodiscard]] bool is_error() const { return m_error != SQLErrorCode::NoError; }
[[nodiscard]] ResultSet release_value() { return m_result_set.release_value(); }
[[nodiscard]] Result release_value() { return move(*this); }
Result release_error()
{
VERIFY(is_error());
@ -138,11 +123,9 @@ private:
SQLErrorCode m_error { SQLErrorCode::NoError };
Optional<String> m_error_message {};
Optional<ResultSet> m_result_set {};
size_t m_update_count { 0 };
size_t m_insert_count { 0 };
size_t m_delete_count { 0 };
};
template<typename ValueType>
using ResultOr = ErrorOr<ValueType, Result>;
}