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

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
15 changed files with 355 additions and 140 deletions

View file

@ -24,26 +24,29 @@ class Database : public Core::Object {
C_OBJECT(Database);
public:
~Database() override = default;
~Database() override;
void commit() { m_heap->flush(); }
ErrorOr<void> open();
bool is_open() const { return m_open; }
ErrorOr<void> commit();
void add_schema(SchemaDef const&);
ErrorOr<void> add_schema(SchemaDef const&);
static Key get_schema_key(String const&);
RefPtr<SchemaDef> get_schema(String const&);
ErrorOr<RefPtr<SchemaDef>> get_schema(String const&);
void add_table(TableDef& table);
ErrorOr<void> add_table(TableDef& table);
static Key get_table_key(String const&, String const&);
RefPtr<TableDef> get_table(String const&, String const&);
ErrorOr<RefPtr<TableDef>> get_table(String const&, String const&);
Vector<Row> select_all(TableDef const&);
Vector<Row> match(TableDef const&, Key const&);
bool insert(Row&);
bool update(Row&);
ErrorOr<Vector<Row>> select_all(TableDef const&);
ErrorOr<Vector<Row>> match(TableDef const&, Key const&);
ErrorOr<void> insert(Row&);
ErrorOr<void> update(Row&);
private:
explicit Database(String);
bool m_open { false };
NonnullRefPtr<Heap> m_heap;
Serializer m_serializer;
RefPtr<BTree> m_schemas;