LibSQL: Improve error handling

The handling of filesystem level errors was basically non-existing or
consisting of `VERIFY_NOT_REACHED` assertions. Addressed this by
* Adding `open` methods to `Heap` and `Database` which return errors.
* Changing the interface of methods of these classes and clients
downstream to propagate these errors.

The constructors of `Heap` and `Database` don't open the underlying
filesystem file anymore.

The SQL statement handlers return an `SQLErrorCode::InternalError`
error code if an error comes back from the lower levels. Note that some
of these errors are things like duplicate index entry errors that should
be caught before the SQL layer attempts to actually update the database.

Added tests to catch attempts to open weird or non-existent files as
databases.

Finally, in between me writing this patch and submitting the PR the
AK::Result<Foo, Bar> template got deprecated in favour of ErrorOr<Foo>.
This resulted in more busywork.
This commit is contained in:
Jan de Visser 2021-11-05 19:05:59 -04:00 committed by Ali Mohammad Pur
parent 108de5dea0
commit 001949d77a
Notes: sideshowbarker 2024-07-17 23:11:33 +09:00
15 changed files with 355 additions and 140 deletions

View file

@ -12,10 +12,16 @@ namespace SQL::AST {
RefPtr<SQLResult> CreateTable::execute(ExecutionContext& context) const
{
auto schema_name = (!m_schema_name.is_null() && !m_schema_name.is_empty()) ? m_schema_name : "default";
auto schema_def = context.database->get_schema(schema_name);
auto schema_def_or_error = context.database->get_schema(schema_name);
if (schema_def_or_error.is_error())
return SQLResult::construct(SQLCommand::Create, SQLErrorCode::InternalError, schema_def_or_error.error());
auto schema_def = schema_def_or_error.release_value();
if (!schema_def)
return SQLResult::construct(SQLCommand::Create, SQLErrorCode::SchemaDoesNotExist, m_schema_name);
auto table_def = context.database->get_table(schema_name, m_table_name);
auto table_def_or_error = context.database->get_table(schema_name, m_table_name);
if (table_def_or_error.is_error())
return SQLResult::construct(SQLCommand::Create, SQLErrorCode::InternalError, table_def_or_error.error());
auto table_def = table_def_or_error.release_value();
if (table_def) {
if (m_is_error_if_table_exists) {
return SQLResult::construct(SQLCommand::Create, SQLErrorCode::TableExists, m_table_name);
@ -37,7 +43,8 @@ RefPtr<SQLResult> CreateTable::execute(ExecutionContext& context) const
}
table_def->append_column(column.name(), type);
}
context.database->add_table(*table_def);
if (auto maybe_error = context.database->add_table(*table_def); maybe_error.is_error())
return SQLResult::construct(SQLCommand::Create, SQLErrorCode::InternalError, maybe_error.release_error());
return SQLResult::construct(SQLCommand::Create, 0, 1);
}