ladybird/Userland/Libraries/LibSQL/AST/CreateTable.cpp
Jan de Visser d074a601df LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
   SELECT INTO schema.table (column1, column2, ....) VALUES
      (value11, value21, ...), (value12, value22, ...), ...
   where the values are literals.
- The SELECT statement is even more limited, and is only provided to
  allow verification of the INSERT statement. The only form implemented
  is: SELECT * FROM schema.table

These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.

This API change dragged SQLServer::SQLStatement into the patch.

Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.

Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-08-21 22:03:30 +02:00

45 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(ExecutionContext& context) const
{
auto schema_name = (!m_schema_name.is_null() && !m_schema_name.is_empty()) ? m_schema_name : "default";
auto schema_def = context.database->get_schema(schema_name);
if (!schema_def)
return SQLResult::construct(SQLCommand::Create, SQLErrorCode::SchemaDoesNotExist, m_schema_name);
auto table_def = context.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);
}
context.database->add_table(*table_def);
return SQLResult::construct(SQLCommand::Create, 0, 1);
}
}