ladybird/Userland/Libraries/LibSQL/AST/CreateSchema.cpp
Jan de Visser 1037d6b0eb LibSQL: Invent statement execution machinery and CREATE SCHEMA statement
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.
2021-07-08 17:55:59 +04:30

28 lines
755 B
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>
#include <LibSQL/Meta.h>
namespace SQL::AST {
RefPtr<SQLResult> CreateSchema::execute(NonnullRefPtr<Database> database) const
{
auto schema_def = database->get_schema(m_schema_name);
if (schema_def) {
if (m_is_error_if_schema_exists) {
return SQLResult::construct(SQLCommand::Create, SQLErrorCode::SchemaExists, m_schema_name);
}
return SQLResult::construct(SQLCommand::Create);
}
schema_def = SchemaDef::construct(m_schema_name);
database->add_schema(*schema_def);
return SQLResult::construct(SQLCommand::Create, 0, 1);
}
}