1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:47:36 +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

@ -33,9 +33,9 @@ SQLStatement::SQLStatement(DatabaseConnection& connection, String sql)
s_statements.set(m_statement_id, *this);
}
void SQLStatement::report_error()
void SQLStatement::report_error(SQL::Result result)
{
dbgln_if(SQLSERVER_DEBUG, "SQLStatement::report_error(statement_id {}, error {}", statement_id(), m_result->error_string());
dbgln_if(SQLSERVER_DEBUG, "SQLStatement::report_error(statement_id {}, error {}", statement_id(), result.error_string());
auto client_connection = ClientConnection::client_connection_for(connection()->client_id());
@ -43,7 +43,7 @@ void SQLStatement::report_error()
remove_from_parent();
if (client_connection)
client_connection->async_execution_error(statement_id(), (int)m_result->error(), m_result->error_string());
client_connection->async_execution_error(statement_id(), (int)result.error(), result.error_string());
else
warnln("Cannot return execution error. Client disconnected");
@ -61,17 +61,17 @@ void SQLStatement::execute()
}
deferred_invoke([this]() mutable {
m_result = parse();
if (m_result.has_value()) {
report_error();
auto parse_result = parse();
if (parse_result.is_error()) {
report_error(parse_result.release_error());
return;
}
VERIFY(!connection()->database().is_null());
m_result = m_statement->execute(connection()->database().release_nonnull());
if (m_result->is_error()) {
report_error();
auto execution_result = m_statement->execute(connection()->database().release_nonnull());
if (execution_result.is_error()) {
report_error(execution_result.release_error());
return;
}
@ -80,15 +80,20 @@ void SQLStatement::execute()
warnln("Cannot return statement execution results. Client disconnected");
return;
}
client_connection->async_execution_success(statement_id(), m_result->has_results(), m_result->updated(), m_result->inserted(), m_result->deleted());
if (m_result->has_results()) {
m_result = execution_result.release_value();
if (should_send_result_rows()) {
client_connection->async_execution_success(statement_id(), true, 0, 0, 0);
m_index = 0;
next();
} else {
client_connection->async_execution_success(statement_id(), false, 0, m_result->size(), 0);
}
});
}
Optional<SQL::Result> SQLStatement::parse()
SQL::ResultOr<void> SQLStatement::parse()
{
auto parser = SQL::AST::Parser(SQL::AST::Lexer(m_sql));
m_statement = parser.next_statement();
@ -98,16 +103,32 @@ Optional<SQL::Result> SQLStatement::parse()
return {};
}
bool SQLStatement::should_send_result_rows() const
{
VERIFY(m_result.has_value());
if (m_result->is_empty())
return false;
switch (m_result->command()) {
case SQL::SQLCommand::Describe:
case SQL::SQLCommand::Select:
return true;
default:
return false;
}
}
void SQLStatement::next()
{
VERIFY(m_result->has_results());
VERIFY(!m_result->is_empty());
auto client_connection = ClientConnection::client_connection_for(connection()->client_id());
if (!client_connection) {
warnln("Cannot yield next result. Client disconnected");
return;
}
if (m_index < m_result->results().size()) {
auto& tuple = m_result->results()[m_index++].row;
if (m_index < m_result->size()) {
auto& tuple = m_result->at(m_index++).row;
client_connection->async_next_result(statement_id(), tuple.to_string_vector());
deferred_invoke([this]() {
next();