2021-06-27 21:32:22 -04:00
|
|
|
/*
|
|
|
|
|
* 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 {
|
|
|
|
|
|
2022-02-10 14:43:00 -05:00
|
|
|
ResultOr<ResultSet> CreateTable::execute(ExecutionContext& context) const
|
2021-06-27 21:32:22 -04:00
|
|
|
{
|
2022-11-29 08:24:15 -05:00
|
|
|
auto schema_def = TRY(context.database->get_schema(m_schema_name));
|
2023-08-07 11:58:03 -04:00
|
|
|
auto table_def = TRY(TableDef::create(schema_def, m_table_name));
|
2022-02-09 15:57:57 -05:00
|
|
|
|
2022-11-29 08:47:22 -05:00
|
|
|
for (auto const& column : m_columns) {
|
2021-06-27 21:32:22 -04:00
|
|
|
SQLType type;
|
2022-02-09 15:57:57 -05:00
|
|
|
|
2023-03-06 14:17:01 +01:00
|
|
|
if (column->type_name()->name().is_one_of("VARCHAR"sv, "TEXT"sv))
|
2021-06-27 21:32:22 -04:00
|
|
|
type = SQLType::Text;
|
2023-03-06 14:17:01 +01:00
|
|
|
else if (column->type_name()->name().is_one_of("INT"sv, "INTEGER"sv))
|
2021-06-27 21:32:22 -04:00
|
|
|
type = SQLType::Integer;
|
2023-03-06 14:17:01 +01:00
|
|
|
else if (column->type_name()->name().is_one_of("FLOAT"sv, "NUMBER"sv))
|
2021-06-27 21:32:22 -04:00
|
|
|
type = SQLType::Float;
|
2023-03-06 14:17:01 +01:00
|
|
|
else if (column->type_name()->name().is_one_of("BOOL"sv, "BOOLEAN"sv))
|
2022-10-27 09:05:16 -04:00
|
|
|
type = SQLType::Boolean;
|
2022-02-09 15:57:57 -05:00
|
|
|
else
|
2023-03-06 14:17:01 +01:00
|
|
|
return Result { SQLCommand::Create, SQLErrorCode::InvalidType, column->type_name()->name() };
|
2022-02-09 15:57:57 -05:00
|
|
|
|
2023-03-06 14:17:01 +01:00
|
|
|
table_def->append_column(column->name(), type);
|
2021-06-27 21:32:22 -04:00
|
|
|
}
|
2022-02-09 15:57:57 -05:00
|
|
|
|
2022-11-29 08:47:22 -05:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 14:43:00 -05:00
|
|
|
return ResultSet { SQLCommand::Create };
|
2021-06-27 21:32:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|