1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-23 18:35:07 +00:00

LibSQL+SQLServer: Return a NonnullRefPtr from Database::get_table

Database::get_table currently either returns a RefPtr to an existing
table, a nullptr if the table doesn't exist, or an Error if some
internal error occured. Change this to return a NonnullRefPtr to an
exisiting table, or a SQL::Result with any error, including if the
table 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:47:22 -05:00 committed by Linus Groh
parent 56843baff9
commit 4b70908dc4
8 changed files with 56 additions and 79 deletions

View file

@ -12,16 +12,9 @@ namespace SQL::AST {
ResultOr<ResultSet> CreateTable::execute(ExecutionContext& context) const
{
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 };
return ResultSet { SQLCommand::Create };
}
auto table_def = TableDef::construct(schema_def, m_table_name);
table_def = TableDef::construct(schema_def, m_table_name);
for (auto& column : m_columns) {
for (auto const& column : m_columns) {
SQLType type;
if (column.type_name()->name().is_one_of("VARCHAR"sv, "TEXT"sv))
@ -38,7 +31,11 @@ ResultOr<ResultSet> CreateTable::execute(ExecutionContext& context) const
table_def->append_column(column.name(), type);
}
TRY(context.database->add_table(*table_def));
if (auto result = context.database->add_table(*table_def); result.is_error()) {
if (result.error().error() != SQLErrorCode::TableExists || m_is_error_if_table_exists)
return result.release_error();
}
return ResultSet { SQLCommand::Create };
}