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.
This commit is contained in:
Jan de Visser 2021-07-19 19:48:46 -04:00 committed by Andreas Kling
parent 230118c4b2
commit d074a601df
Notes: sideshowbarker 2024-07-18 05:26:13 +09:00
12 changed files with 329 additions and 16 deletions

View file

@ -9,13 +9,13 @@
namespace SQL::AST {
RefPtr<SQLResult> CreateTable::execute(NonnullRefPtr<Database> database) const
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 = database->get_schema(schema_name);
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 = database->get_table(schema_name, m_table_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);
@ -37,7 +37,7 @@ RefPtr<SQLResult> CreateTable::execute(NonnullRefPtr<Database> database) const
}
table_def->append_column(column.name(), type);
}
database->add_table(*table_def);
context.database->add_table(*table_def);
return SQLResult::construct(SQLCommand::Create, 0, 1);
}