1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 04:28:13 +00:00

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.
This commit is contained in:
Timothy Flynn 2022-11-29 08:24:15 -05:00 committed by Linus Groh
parent 7464dfa974
commit 56843baff9
7 changed files with 49 additions and 57 deletions

View file

@ -172,7 +172,6 @@ TEST_CASE(get_schema_from_database)
EXPECT(!db->open().is_error()); EXPECT(!db->open().is_error());
auto schema_or_error = db->get_schema("TestSchema"); auto schema_or_error = db->get_schema("TestSchema");
EXPECT(!schema_or_error.is_error()); EXPECT(!schema_or_error.is_error());
EXPECT(schema_or_error.value());
} }
} }

View file

@ -71,7 +71,6 @@ TEST_CASE(create_schema)
create_schema(database); create_schema(database);
auto schema_or_error = database->get_schema("TESTSCHEMA"); auto schema_or_error = database->get_schema("TESTSCHEMA");
EXPECT(!schema_or_error.is_error()); EXPECT(!schema_or_error.is_error());
EXPECT(schema_or_error.value());
} }
TEST_CASE(create_table) TEST_CASE(create_table)

View file

@ -12,17 +12,13 @@ namespace SQL::AST {
ResultOr<ResultSet> CreateSchema::execute(ExecutionContext& context) const 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 (auto result = context.database->add_schema(*schema_def); result.is_error()) {
if (m_is_error_if_schema_exists) if (result.error().error() != SQLErrorCode::SchemaExists || m_is_error_if_schema_exists)
return Result { SQLCommand::Create, SQLErrorCode::SchemaExists, m_schema_name }; return result.release_error();
return ResultSet { SQLCommand::Create };
} }
schema_def = SchemaDef::construct(m_schema_name);
TRY(context.database->add_schema(*schema_def));
return ResultSet { SQLCommand::Create }; return ResultSet { SQLCommand::Create };
} }

View file

@ -11,13 +11,8 @@ namespace SQL::AST {
ResultOr<ResultSet> CreateTable::execute(ExecutionContext& context) const 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(m_schema_name));
auto table_def = TRY(context.database->get_table(m_schema_name, m_table_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));
if (table_def) { if (table_def) {
if (m_is_error_if_table_exists) if (m_is_error_if_table_exists)
return Result { SQLCommand::Create, SQLErrorCode::TableExists, m_table_name }; return Result { SQLCommand::Create, SQLErrorCode::TableExists, m_table_name };

View file

@ -24,9 +24,10 @@ Database::Database(String name)
{ {
} }
ErrorOr<void> Database::open() ResultOr<void> Database::open()
{ {
TRY(m_heap->open()); TRY(m_heap->open());
m_schemas = BTree::construct(m_serializer, SchemaDef::index_def()->to_tuple_descriptor(), m_heap->schemas_root()); m_schemas = BTree::construct(m_serializer, SchemaDef::index_def()->to_tuple_descriptor(), m_heap->schemas_root());
m_schemas->on_new_root = [&]() { m_schemas->on_new_root = [&]() {
m_heap->set_schemas_root(m_schemas->root()); m_heap->set_schemas_root(m_schemas->root());
@ -43,17 +44,22 @@ ErrorOr<void> Database::open()
}; };
m_open = true; 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")); auto ensure_schema_exists = [&](auto schema_name) -> ResultOr<NonnullRefPtr<SchemaDef>> {
if (!master_schema) { if (auto result = get_schema(schema_name); result.is_error()) {
master_schema = SchemaDef::construct("master"); if (result.error().error() != SQLErrorCode::SchemaDoesNotExist)
TRY(add_schema(*master_schema)); 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")); auto table_def = TRY(get_table("master", "internal_describe_table"));
if (!table_def) { if (!table_def) {
@ -75,13 +81,12 @@ ErrorOr<void> Database::commit()
return {}; return {};
} }
ErrorOr<void> Database::add_schema(SchemaDef const& schema) ResultOr<void> Database::add_schema(SchemaDef const& schema)
{ {
VERIFY(is_open()); VERIFY(is_open());
if (!m_schemas->insert(schema.key())) {
warnln("Duplicate schema name {}"sv, schema.name()); if (!m_schemas->insert(schema.key()))
return Error::from_string_literal("Duplicate schema name"); return Result { SQLCommand::Unknown, SQLErrorCode::SchemaExists, schema.name() };
}
return {}; return {};
} }
@ -92,24 +97,25 @@ Key Database::get_schema_key(String const& schema_name)
return key; return key;
} }
ErrorOr<RefPtr<SchemaDef>> Database::get_schema(String const& schema) ResultOr<NonnullRefPtr<SchemaDef>> Database::get_schema(String const& schema)
{ {
VERIFY(is_open()); VERIFY(is_open());
auto schema_name = schema; auto schema_name = schema;
if (schema.is_null() || schema.is_empty()) if (schema.is_empty())
schema_name = "default"; schema_name = "default"sv;
Key key = get_schema_key(schema_name); Key key = get_schema_key(schema_name);
auto schema_def_opt = m_schema_cache.get(key.hash()); if (auto it = m_schema_cache.find(key.hash()); it != m_schema_cache.end())
if (schema_def_opt.has_value()) { return it->value;
return RefPtr<SchemaDef>(schema_def_opt.value());
}
auto schema_iterator = m_schemas->find(key); auto schema_iterator = m_schemas->find(key);
if (schema_iterator.is_end() || (*schema_iterator != key)) { if (schema_iterator.is_end() || (*schema_iterator != key))
return RefPtr<SchemaDef>(nullptr); return Result { SQLCommand::Unknown, SQLErrorCode::SchemaDoesNotExist, schema_name };
}
auto ret = SchemaDef::construct(*schema_iterator); auto schema_def = SchemaDef::construct(*schema_iterator);
m_schema_cache.set(key.hash(), ret); m_schema_cache.set(key.hash(), schema_def);
return RefPtr<SchemaDef>(ret); return schema_def;
} }
ErrorOr<void> Database::add_table(TableDef& table) 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; 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()); VERIFY(is_open());
auto schema_name = schema; auto schema_name = schema;
@ -147,10 +153,6 @@ ErrorOr<RefPtr<TableDef>> Database::get_table(String const& schema, String const
return RefPtr<TableDef>(nullptr); return RefPtr<TableDef>(nullptr);
} }
auto schema_def = TRY(get_schema(schema)); 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); auto ret = TableDef::construct(schema_def, name);
ret->set_pointer((*table_iterator).pointer()); ret->set_pointer((*table_iterator).pointer());
m_table_cache.set(key.hash(), ret); m_table_cache.set(key.hash(), ret);

View file

@ -13,6 +13,7 @@
#include <LibSQL/Forward.h> #include <LibSQL/Forward.h>
#include <LibSQL/Heap.h> #include <LibSQL/Heap.h>
#include <LibSQL/Meta.h> #include <LibSQL/Meta.h>
#include <LibSQL/Result.h>
#include <LibSQL/Serializer.h> #include <LibSQL/Serializer.h>
namespace SQL { namespace SQL {
@ -28,17 +29,17 @@ class Database : public Core::Object {
public: public:
~Database() override; ~Database() override;
ErrorOr<void> open(); ResultOr<void> open();
bool is_open() const { return m_open; } bool is_open() const { return m_open; }
ErrorOr<void> commit(); ErrorOr<void> commit();
ErrorOr<void> add_schema(SchemaDef const&); ResultOr<void> add_schema(SchemaDef const&);
static Key get_schema_key(String 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); ErrorOr<void> add_table(TableDef& table);
static Key get_table_key(String const&, String const&); 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>> select_all(TableDef const&);
ErrorOr<Vector<Row>> match(TableDef const&, Key const&); ErrorOr<Vector<Row>> match(TableDef const&, Key const&);
@ -55,7 +56,7 @@ private:
RefPtr<BTree> m_tables; RefPtr<BTree> m_tables;
RefPtr<BTree> m_table_columns; 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; HashMap<u32, RefPtr<TableDef>> m_table_cache;
}; };

View file

@ -41,7 +41,7 @@ DatabaseConnection::DatabaseConnection(String database_name, int client_id)
m_database = SQL::Database::construct(String::formatted("/home/anon/sql/{}.db", m_database_name)); m_database = SQL::Database::construct(String::formatted("/home/anon/sql/{}.db", m_database_name));
auto client_connection = ConnectionFromClient::client_connection_for(m_client_id); auto client_connection = ConnectionFromClient::client_connection_for(m_client_id);
if (auto maybe_error = m_database->open(); maybe_error.is_error()) { if (auto maybe_error = m_database->open(); maybe_error.is_error()) {
client_connection->async_connection_error(m_connection_id, (int)SQL::SQLErrorCode::InternalError, maybe_error.error().string_literal()); client_connection->async_connection_error(m_connection_id, to_underlying(maybe_error.error().error()), maybe_error.error().error_string());
return; return;
} }
m_accept_statements = true; m_accept_statements = true;