ladybird/Userland/Libraries/LibSQL/AST/CreateTable.cpp
Timothy Flynn 56843baff9 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.
2022-11-30 11:43:13 +01:00

45 lines
1.4 KiB
C++

/*
* Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibSQL/AST/AST.h>
#include <LibSQL/Database.h>
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 };
}
table_def = TableDef::construct(schema_def, m_table_name);
for (auto& column : m_columns) {
SQLType type;
if (column.type_name()->name().is_one_of("VARCHAR"sv, "TEXT"sv))
type = SQLType::Text;
else if (column.type_name()->name().is_one_of("INT"sv, "INTEGER"sv))
type = SQLType::Integer;
else if (column.type_name()->name().is_one_of("FLOAT"sv, "NUMBER"sv))
type = SQLType::Float;
else if (column.type_name()->name().is_one_of("BOOL"sv, "BOOLEAN"sv))
type = SQLType::Boolean;
else
return Result { SQLCommand::Create, SQLErrorCode::InvalidType, column.type_name()->name() };
table_def->append_column(column.name(), type);
}
TRY(context.database->add_table(*table_def));
return ResultSet { SQLCommand::Create };
}
}