summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibSQL
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-11-29 08:24:15 -0500
committerLinus Groh <mail@linusgroh.de>2022-11-30 11:43:13 +0100
commit56843baff928a31e5b4520e38b36dd0de35acd9f (patch)
tree6a9270ad5a0db91ed9700fa6192b6fac2d6073fb /Userland/Libraries/LibSQL
parent7464dfa974fce3845ab2fb609b2d1df31f911814 (diff)
downloadserenity-56843baff928a31e5b4520e38b36dd0de35acd9f.zip
LibSQL+SQLServer: Return a NonnullRefPtr from Database::get_schema
Database::get_schema currently either returns a RefPtr to an existing schema, a nullptr if the schema doesn't exist, or an Error if some internal error occured. Change this to return a NonnullRefPtr to an exisiting schema, or a SQL::Result with any error, including if the schema was not found. Callers can then handle that specific error code if they want. Returning a NonnullRefPtr will enable some further cleanup. This had some fallout of needing to change some other methods' return types from AK::ErrorOr to SQL::Result so that TRY may continue to be used.
Diffstat (limited to 'Userland/Libraries/LibSQL')
-rw-r--r--Userland/Libraries/LibSQL/AST/CreateSchema.cpp12
-rw-r--r--Userland/Libraries/LibSQL/AST/CreateTable.cpp9
-rw-r--r--Userland/Libraries/LibSQL/Database.cpp70
-rw-r--r--Userland/Libraries/LibSQL/Database.h11
4 files changed, 48 insertions, 54 deletions
diff --git a/Userland/Libraries/LibSQL/AST/CreateSchema.cpp b/Userland/Libraries/LibSQL/AST/CreateSchema.cpp
index a41cc3a269..5f60f009a0 100644
--- a/Userland/Libraries/LibSQL/AST/CreateSchema.cpp
+++ b/Userland/Libraries/LibSQL/AST/CreateSchema.cpp
@@ -12,17 +12,13 @@ namespace SQL::AST {
ResultOr<ResultSet> CreateSchema::execute(ExecutionContext& context) const
{
- auto schema_def = TRY(context.database->get_schema(m_schema_name));
+ auto schema_def = SchemaDef::construct(m_schema_name);
- if (schema_def) {
- if (m_is_error_if_schema_exists)
- return Result { SQLCommand::Create, SQLErrorCode::SchemaExists, m_schema_name };
- return ResultSet { SQLCommand::Create };
+ if (auto result = context.database->add_schema(*schema_def); result.is_error()) {
+ if (result.error().error() != SQLErrorCode::SchemaExists || m_is_error_if_schema_exists)
+ return result.release_error();
}
- schema_def = SchemaDef::construct(m_schema_name);
- TRY(context.database->add_schema(*schema_def));
-
return ResultSet { SQLCommand::Create };
}
diff --git a/Userland/Libraries/LibSQL/AST/CreateTable.cpp b/Userland/Libraries/LibSQL/AST/CreateTable.cpp
index a427e59f25..2312e0f23d 100644
--- a/Userland/Libraries/LibSQL/AST/CreateTable.cpp
+++ b/Userland/Libraries/LibSQL/AST/CreateTable.cpp
@@ -11,13 +11,8 @@ namespace SQL::AST {
ResultOr<ResultSet> CreateTable::execute(ExecutionContext& context) const
{
- auto schema_name = m_schema_name.is_empty() ? String { "default"sv } : m_schema_name;
-
- auto schema_def = TRY(context.database->get_schema(schema_name));
- if (!schema_def)
- return Result { SQLCommand::Create, SQLErrorCode::SchemaDoesNotExist, schema_name };
-
- auto table_def = TRY(context.database->get_table(schema_name, m_table_name));
+ auto schema_def = TRY(context.database->get_schema(m_schema_name));
+ auto table_def = TRY(context.database->get_table(m_schema_name, m_table_name));
if (table_def) {
if (m_is_error_if_table_exists)
return Result { SQLCommand::Create, SQLErrorCode::TableExists, m_table_name };
diff --git a/Userland/Libraries/LibSQL/Database.cpp b/Userland/Libraries/LibSQL/Database.cpp
index 424c0e7ba8..401d18fb21 100644
--- a/Userland/Libraries/LibSQL/Database.cpp
+++ b/Userland/Libraries/LibSQL/Database.cpp
@@ -24,9 +24,10 @@ Database::Database(String name)
{
}
-ErrorOr<void> Database::open()
+ResultOr<void> Database::open()
{
TRY(m_heap->open());
+
m_schemas = BTree::construct(m_serializer, SchemaDef::index_def()->to_tuple_descriptor(), m_heap->schemas_root());
m_schemas->on_new_root = [&]() {
m_heap->set_schemas_root(m_schemas->root());
@@ -43,17 +44,22 @@ ErrorOr<void> Database::open()
};
m_open = true;
- auto default_schema = TRY(get_schema("default"));
- if (!default_schema) {
- default_schema = SchemaDef::construct("default");
- TRY(add_schema(*default_schema));
- }
- auto master_schema = TRY(get_schema("master"));
- if (!master_schema) {
- master_schema = SchemaDef::construct("master");
- TRY(add_schema(*master_schema));
- }
+ auto ensure_schema_exists = [&](auto schema_name) -> ResultOr<NonnullRefPtr<SchemaDef>> {
+ if (auto result = get_schema(schema_name); result.is_error()) {
+ if (result.error().error() != SQLErrorCode::SchemaDoesNotExist)
+ return result.release_error();
+
+ auto schema_def = SchemaDef::construct(schema_name);
+ TRY(add_schema(*schema_def));
+ return schema_def;
+ } else {
+ return result.release_value();
+ }
+ };
+
+ (void)TRY(ensure_schema_exists("default"sv));
+ auto master_schema = TRY(ensure_schema_exists("master"sv));
auto table_def = TRY(get_table("master", "internal_describe_table"));
if (!table_def) {
@@ -75,13 +81,12 @@ ErrorOr<void> Database::commit()
return {};
}
-ErrorOr<void> Database::add_schema(SchemaDef const& schema)
+ResultOr<void> Database::add_schema(SchemaDef const& schema)
{
VERIFY(is_open());
- if (!m_schemas->insert(schema.key())) {
- warnln("Duplicate schema name {}"sv, schema.name());
- return Error::from_string_literal("Duplicate schema name");
- }
+
+ if (!m_schemas->insert(schema.key()))
+ return Result { SQLCommand::Unknown, SQLErrorCode::SchemaExists, schema.name() };
return {};
}
@@ -92,24 +97,25 @@ Key Database::get_schema_key(String const& schema_name)
return key;
}
-ErrorOr<RefPtr<SchemaDef>> Database::get_schema(String const& schema)
+ResultOr<NonnullRefPtr<SchemaDef>> Database::get_schema(String const& schema)
{
VERIFY(is_open());
+
auto schema_name = schema;
- if (schema.is_null() || schema.is_empty())
- schema_name = "default";
+ if (schema.is_empty())
+ schema_name = "default"sv;
+
Key key = get_schema_key(schema_name);
- auto schema_def_opt = m_schema_cache.get(key.hash());
- if (schema_def_opt.has_value()) {
- return RefPtr<SchemaDef>(schema_def_opt.value());
- }
+ if (auto it = m_schema_cache.find(key.hash()); it != m_schema_cache.end())
+ return it->value;
+
auto schema_iterator = m_schemas->find(key);
- if (schema_iterator.is_end() || (*schema_iterator != key)) {
- return RefPtr<SchemaDef>(nullptr);
- }
- auto ret = SchemaDef::construct(*schema_iterator);
- m_schema_cache.set(key.hash(), ret);
- return RefPtr<SchemaDef>(ret);
+ if (schema_iterator.is_end() || (*schema_iterator != key))
+ return Result { SQLCommand::Unknown, SQLErrorCode::SchemaDoesNotExist, schema_name };
+
+ auto schema_def = SchemaDef::construct(*schema_iterator);
+ m_schema_cache.set(key.hash(), schema_def);
+ return schema_def;
}
ErrorOr<void> Database::add_table(TableDef& table)
@@ -132,7 +138,7 @@ Key Database::get_table_key(String const& schema_name, String const& table_name)
return key;
}
-ErrorOr<RefPtr<TableDef>> Database::get_table(String const& schema, String const& name)
+ResultOr<RefPtr<TableDef>> Database::get_table(String const& schema, String const& name)
{
VERIFY(is_open());
auto schema_name = schema;
@@ -147,10 +153,6 @@ ErrorOr<RefPtr<TableDef>> Database::get_table(String const& schema, String const
return RefPtr<TableDef>(nullptr);
}
auto schema_def = TRY(get_schema(schema));
- if (!schema_def) {
- warnln("Schema '{}' does not exist"sv, schema);
- return Error::from_string_literal("Schema does not exist");
- }
auto ret = TableDef::construct(schema_def, name);
ret->set_pointer((*table_iterator).pointer());
m_table_cache.set(key.hash(), ret);
diff --git a/Userland/Libraries/LibSQL/Database.h b/Userland/Libraries/LibSQL/Database.h
index cafd08cd59..29c97c089c 100644
--- a/Userland/Libraries/LibSQL/Database.h
+++ b/Userland/Libraries/LibSQL/Database.h
@@ -13,6 +13,7 @@
#include <LibSQL/Forward.h>
#include <LibSQL/Heap.h>
#include <LibSQL/Meta.h>
+#include <LibSQL/Result.h>
#include <LibSQL/Serializer.h>
namespace SQL {
@@ -28,17 +29,17 @@ class Database : public Core::Object {
public:
~Database() override;
- ErrorOr<void> open();
+ ResultOr<void> open();
bool is_open() const { return m_open; }
ErrorOr<void> commit();
- ErrorOr<void> add_schema(SchemaDef const&);
+ ResultOr<void> add_schema(SchemaDef const&);
static Key get_schema_key(String const&);
- ErrorOr<RefPtr<SchemaDef>> get_schema(String const&);
+ ResultOr<NonnullRefPtr<SchemaDef>> get_schema(String const&);
ErrorOr<void> add_table(TableDef& table);
static Key get_table_key(String const&, String const&);
- ErrorOr<RefPtr<TableDef>> get_table(String const&, String const&);
+ ResultOr<RefPtr<TableDef>> get_table(String const&, String const&);
ErrorOr<Vector<Row>> select_all(TableDef const&);
ErrorOr<Vector<Row>> match(TableDef const&, Key const&);
@@ -55,7 +56,7 @@ private:
RefPtr<BTree> m_tables;
RefPtr<BTree> m_table_columns;
- HashMap<u32, RefPtr<SchemaDef>> m_schema_cache;
+ HashMap<u32, NonnullRefPtr<SchemaDef>> m_schema_cache;
HashMap<u32, RefPtr<TableDef>> m_table_cache;
};