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

@ -21,23 +21,23 @@ static bool does_value_data_type_match(SQLType expected, SQLType actual)
return expected == actual;
}
Result Insert::execute(ExecutionContext& context) const
ResultOr<ResultSet> Insert::execute(ExecutionContext& context) const
{
auto table_def = TRY(context.database->get_table(m_schema_name, m_table_name));
if (!table_def) {
auto schema_name = m_schema_name.is_empty() ? String("default"sv) : m_schema_name;
return { SQLCommand::Insert, SQLErrorCode::TableDoesNotExist, String::formatted("{}.{}", schema_name, m_table_name) };
return Result { SQLCommand::Insert, SQLErrorCode::TableDoesNotExist, String::formatted("{}.{}", schema_name, m_table_name) };
}
Row row(table_def);
for (auto& column : m_column_names) {
if (!row.has(column))
return { SQLCommand::Insert, SQLErrorCode::ColumnDoesNotExist, column };
return Result { SQLCommand::Insert, SQLErrorCode::ColumnDoesNotExist, column };
}
Vector<Row> inserted_rows;
TRY(inserted_rows.try_ensure_capacity(m_chained_expressions.size()));
ResultSet result { SQLCommand::Insert };
TRY(result.try_ensure_capacity(m_chained_expressions.size()));
context.result = Result { SQLCommand::Insert };
@ -55,7 +55,7 @@ Result Insert::execute(ExecutionContext& context) const
auto values = row_value.to_vector().value();
if (m_column_names.is_empty() && values.size() != row.size())
return { SQLCommand::Insert, SQLErrorCode::InvalidNumberOfValues, String::empty() };
return Result { SQLCommand::Insert, SQLErrorCode::InvalidNumberOfValues, String::empty() };
for (auto ix = 0u; ix < values.size(); ix++) {
auto input_value_type = values[ix].type();
@ -65,18 +65,16 @@ Result Insert::execute(ExecutionContext& context) const
auto element_type = tuple_descriptor[element_index].type;
if (!does_value_data_type_match(element_type, input_value_type))
return { SQLCommand::Insert, SQLErrorCode::InvalidValueType, table_def->columns()[element_index].name() };
return Result { SQLCommand::Insert, SQLErrorCode::InvalidValueType, table_def->columns()[element_index].name() };
row[element_index] = values[ix];
}
inserted_rows.append(row);
TRY(context.database->insert(row));
result.insert_row(row, {});
}
for (auto& inserted_row : inserted_rows)
TRY(context.database->insert(inserted_row));
return { SQLCommand::Insert, 0, m_chained_expressions.size() };
return result;
}
}