diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-11-29 08:24:15 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-11-30 11:43:13 +0100 |
commit | 56843baff928a31e5b4520e38b36dd0de35acd9f (patch) | |
tree | 6a9270ad5a0db91ed9700fa6192b6fac2d6073fb /Userland/Libraries/LibSQL/AST | |
parent | 7464dfa974fce3845ab2fb609b2d1df31f911814 (diff) | |
download | serenity-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/AST')
-rw-r--r-- | Userland/Libraries/LibSQL/AST/CreateSchema.cpp | 12 | ||||
-rw-r--r-- | Userland/Libraries/LibSQL/AST/CreateTable.cpp | 9 |
2 files changed, 6 insertions, 15 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 }; |