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-07-19 19:48:46 -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>
|
|
|
|
|
#include <LibSQL/Meta.h>
|
|
|
|
|
#include <LibSQL/Row.h>
|
|
|
|
|
|
|
|
|
|
namespace SQL::AST {
|
|
|
|
|
|
|
|
|
|
RefPtr<SQLResult> Select::execute(ExecutionContext& context) const
|
|
|
|
|
{
|
2021-11-02 16:49:54 -04:00
|
|
|
NonnullRefPtrVector<ResultColumn> columns;
|
|
|
|
|
for (auto& table_descriptor : table_or_subquery_list()) {
|
|
|
|
|
if (!table_descriptor.is_table())
|
2021-11-05 19:05:59 -04:00
|
|
|
return SQLResult::construct(SQLCommand::Select, SQLErrorCode::NotYetImplemented, "Sub-selects are not yet implemented");
|
|
|
|
|
auto table_def_or_error = context.database->get_table(table_descriptor.schema_name(), table_descriptor.table_name());
|
|
|
|
|
if (table_def_or_error.is_error())
|
|
|
|
|
return SQLResult::construct(SQLCommand::Select, SQLErrorCode::InternalError, table_def_or_error.error());
|
|
|
|
|
auto table = table_def_or_error.value();
|
2021-09-16 22:29:19 +02:00
|
|
|
if (!table) {
|
2021-11-05 19:05:59 -04:00
|
|
|
return SQLResult::construct(SQLCommand::Select, SQLErrorCode::TableDoesNotExist, table_descriptor.table_name());
|
2021-09-16 22:29:19 +02:00
|
|
|
}
|
2021-11-05 19:05:59 -04:00
|
|
|
|
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-07-19 19:48:46 -04:00
|
|
|
if (result_column_list().size() == 1 && result_column_list()[0].type() == ResultType::All) {
|
2021-09-16 22:29:19 +02:00
|
|
|
for (auto& col : table->columns()) {
|
|
|
|
|
columns.append(
|
|
|
|
|
create_ast_node<ResultColumn>(
|
|
|
|
|
create_ast_node<ColumnNameExpression>(table->parent()->name(), table->name(), col.name()),
|
|
|
|
|
""));
|
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-07-19 19:48:46 -04:00
|
|
|
}
|
2021-09-16 22:29:19 +02:00
|
|
|
}
|
2021-11-02 16:49:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VERIFY(!result_column_list().is_empty());
|
|
|
|
|
if (result_column_list().size() != 1 || result_column_list()[0].type() != ResultType::All) {
|
|
|
|
|
for (auto& col : result_column_list()) {
|
|
|
|
|
if (col.type() == ResultType::All)
|
|
|
|
|
// FIXME can have '*' for example in conjunction with computed columns
|
2021-11-05 19:05:59 -04:00
|
|
|
return SQLResult::construct(SQL::SQLCommand::Select, SQLErrorCode::SyntaxError, "*");
|
2021-11-02 16:49:54 -04:00
|
|
|
columns.append(col);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.result = SQLResult::construct();
|
|
|
|
|
AK::NonnullRefPtr<TupleDescriptor> descriptor = AK::adopt_ref(*new TupleDescriptor);
|
|
|
|
|
Tuple tuple(descriptor);
|
|
|
|
|
Vector<Tuple> rows;
|
|
|
|
|
descriptor->empend("__unity__");
|
|
|
|
|
tuple.append(Value(SQLType::Boolean, true));
|
|
|
|
|
rows.append(tuple);
|
|
|
|
|
|
|
|
|
|
for (auto& table_descriptor : table_or_subquery_list()) {
|
|
|
|
|
if (!table_descriptor.is_table())
|
2021-11-05 19:05:59 -04:00
|
|
|
return SQLResult::construct(SQLCommand::Select, SQLErrorCode::NotYetImplemented, "Sub-selects are not yet implemented");
|
|
|
|
|
auto table_def_or_error = context.database->get_table(table_descriptor.schema_name(), table_descriptor.table_name());
|
|
|
|
|
if (table_def_or_error.is_error())
|
|
|
|
|
return SQLResult::construct(SQLCommand::Select, SQLErrorCode::InternalError, table_def_or_error.error());
|
|
|
|
|
auto table = table_def_or_error.value();
|
2021-11-02 16:49:54 -04:00
|
|
|
if (table->num_columns() == 0)
|
|
|
|
|
continue;
|
|
|
|
|
auto old_descriptor_size = descriptor->size();
|
|
|
|
|
descriptor->extend(table->to_tuple_descriptor());
|
|
|
|
|
for (auto cartesian_row = rows.first(); cartesian_row.size() == old_descriptor_size; cartesian_row = rows.first()) {
|
|
|
|
|
rows.remove(0);
|
2021-11-05 19:05:59 -04:00
|
|
|
auto table_rows_or_error = context.database->select_all(*table);
|
|
|
|
|
if (table_rows_or_error.is_error())
|
|
|
|
|
return SQLResult::construct(SQLCommand::Create, SQLErrorCode::InternalError, table_rows_or_error.error());
|
|
|
|
|
for (auto& table_row : table_rows_or_error.value()) {
|
2021-11-02 16:49:54 -04:00
|
|
|
auto new_row = cartesian_row;
|
|
|
|
|
new_row.extend(table_row);
|
|
|
|
|
rows.append(new_row);
|
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-07-19 19:48:46 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-02 16:49:54 -04:00
|
|
|
|
|
|
|
|
for (auto& row : rows) {
|
|
|
|
|
context.current_row = &row;
|
|
|
|
|
if (where_clause()) {
|
|
|
|
|
auto where_result = where_clause()->evaluate(context);
|
|
|
|
|
if (context.result->has_error())
|
|
|
|
|
return context.result;
|
|
|
|
|
if (!where_result)
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
tuple.clear();
|
|
|
|
|
for (auto& col : columns) {
|
|
|
|
|
auto value = col.expression()->evaluate(context);
|
|
|
|
|
if (context.result->has_error())
|
|
|
|
|
return context.result;
|
|
|
|
|
tuple.append(value);
|
|
|
|
|
}
|
|
|
|
|
context.result->append(tuple);
|
|
|
|
|
}
|
|
|
|
|
return context.result;
|
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-07-19 19:48:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|