mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-24 02:12:09 -05:00
1037d6b0eb
This patch introduces the ability execute parsed SQL statements. The abstract AST Statement node now has a virtual 'execute' method. This method takes a Database object as parameter and returns a SQLResult object. Also introduced here is the CREATE SCHEMA statement. Tables live in a schema, and if no schema is present in a table reference the 'default' schema is implied. This schema is created if it doesn't yet exist when a Database object is created. Finally, as a proof of concept, the CREATE SCHEMA and CREATE TABLE statements received an 'execute' implementation. The CREATE TABLE method is not able to create tables created from SQL queries yet.
44 lines
1.7 KiB
C++
44 lines
1.7 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 {
|
|
|
|
RefPtr<SQLResult> CreateTable::execute(NonnullRefPtr<Database> database) const
|
|
{
|
|
auto schema_name = (!m_schema_name.is_null() && !m_schema_name.is_empty()) ? m_schema_name : "default";
|
|
auto schema_def = database->get_schema(schema_name);
|
|
if (!schema_def)
|
|
return SQLResult::construct(SQLCommand::Create, SQLErrorCode::SchemaDoesNotExist, m_schema_name);
|
|
auto table_def = database->get_table(schema_name, m_table_name);
|
|
if (table_def) {
|
|
if (m_is_error_if_table_exists) {
|
|
return SQLResult::construct(SQLCommand::Create, SQLErrorCode::TableExists, m_table_name);
|
|
} else {
|
|
return SQLResult::construct(SQLCommand::Create);
|
|
}
|
|
}
|
|
table_def = TableDef::construct(schema_def, m_table_name);
|
|
for (auto& column : m_columns) {
|
|
SQLType type;
|
|
if (column.type_name()->name() == "VARCHAR" || column.type_name()->name() == "TEXT") {
|
|
type = SQLType::Text;
|
|
} else if (column.type_name()->name() == "INT" || column.type_name()->name() == "INTEGER") {
|
|
type = SQLType::Integer;
|
|
} else if (column.type_name()->name() == "FLOAT" || column.type_name()->name() == "NUMBER") {
|
|
type = SQLType::Float;
|
|
} else {
|
|
return SQLResult::construct(SQLCommand::Create, SQLErrorCode::InvalidType, column.type_name()->name());
|
|
}
|
|
table_def->append_column(column.name(), type);
|
|
}
|
|
database->add_table(*table_def);
|
|
return SQLResult::construct(SQLCommand::Create, 0, 1);
|
|
}
|
|
|
|
}
|