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
|
|
|
|
|
*/
|
|
|
|
|
|
2022-01-12 11:41:58 -05:00
|
|
|
#include <AK/NumericLimits.h>
|
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
|
|
|
#include <LibSQL/AST/AST.h>
|
|
|
|
|
#include <LibSQL/Database.h>
|
|
|
|
|
#include <LibSQL/Meta.h>
|
|
|
|
|
#include <LibSQL/Row.h>
|
|
|
|
|
|
|
|
|
|
namespace SQL::AST {
|
|
|
|
|
|
2022-02-10 14:43:00 -05:00
|
|
|
ResultOr<ResultSet> Select::execute(ExecutionContext& context) const
|
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
|
|
|
NonnullRefPtrVector<ResultColumn> columns;
|
2022-02-09 15:57:57 -05:00
|
|
|
|
|
|
|
|
auto const& result_column_list = this->result_column_list();
|
|
|
|
|
VERIFY(!result_column_list.is_empty());
|
|
|
|
|
|
2021-11-02 16:49:54 -04:00
|
|
|
for (auto& table_descriptor : table_or_subquery_list()) {
|
|
|
|
|
if (!table_descriptor.is_table())
|
2022-02-10 14:43:00 -05:00
|
|
|
return Result { SQLCommand::Select, SQLErrorCode::NotYetImplemented, "Sub-selects are not yet implemented"sv };
|
2021-11-05 19:05:59 -04:00
|
|
|
|
2022-02-09 15:57:57 -05:00
|
|
|
auto table_def = TRY(context.database->get_table(table_descriptor.schema_name(), table_descriptor.table_name()));
|
|
|
|
|
|
|
|
|
|
if (result_column_list.size() == 1 && result_column_list[0].type() == ResultType::All) {
|
|
|
|
|
for (auto& col : table_def->columns()) {
|
2021-09-16 22:29:19 +02:00
|
|
|
columns.append(
|
|
|
|
|
create_ast_node<ResultColumn>(
|
2022-02-09 15:57:57 -05:00
|
|
|
create_ast_node<ColumnNameExpression>(table_def->parent()->name(), table_def->name(), col.name()),
|
2021-09-16 22:29:19 +02: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
|
|
|
}
|
2021-09-16 22:29:19 +02:00
|
|
|
}
|
2021-11-02 16:49:54 -04:00
|
|
|
}
|
|
|
|
|
|
2022-02-09 15:57:57 -05:00
|
|
|
if (result_column_list.size() != 1 || result_column_list[0].type() != ResultType::All) {
|
|
|
|
|
for (auto& col : result_column_list) {
|
|
|
|
|
if (col.type() == ResultType::All) {
|
2021-11-02 16:49:54 -04:00
|
|
|
// FIXME can have '*' for example in conjunction with computed columns
|
2022-02-10 14:43:00 -05:00
|
|
|
return Result { SQLCommand::Select, SQLErrorCode::SyntaxError, "*"sv };
|
2022-02-09 15:57:57 -05:00
|
|
|
}
|
|
|
|
|
|
2021-11-02 16:49:54 -04:00
|
|
|
columns.append(col);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 14:43:00 -05:00
|
|
|
ResultSet result { SQLCommand::Select };
|
2022-02-09 15:57:57 -05:00
|
|
|
|
|
|
|
|
auto descriptor = adopt_ref(*new TupleDescriptor);
|
2021-11-02 16:49:54 -04:00
|
|
|
Tuple tuple(descriptor);
|
|
|
|
|
Vector<Tuple> rows;
|
2022-02-09 15:57:57 -05:00
|
|
|
descriptor->empend("__unity__"sv);
|
LibSQL: Rewrite the SQL::Value type to be contained within one class
Currently, the Value class is essentially a "pImpl" wrapper around the
ValueImpl hierarchy of classes. This is a bit difficult to follow and
reason about, as methods jump between the Value class and its impl
classes.
This changes the Variant held by Value to instead store the specified
types (String, int, etc.) directly. In doing so, the ValueImpl classes
are removed, and all methods are now just concise Variant visitors.
As part of this rewrite, support for the "array" type is dropped (or
rather, just not re-implemented) as it was unused. If it's needed in the
future, support can be re-added.
This does retain the ability for non-NULL types to store NULL values
(i.e. an empty Optional). I tried dropping this support as well, but it
is depended upon by the on-disk storage classes in non-trivial ways.
2022-09-21 13:48:02 -04:00
|
|
|
tuple.append(Value { true });
|
2021-11-02 16:49:54 -04:00
|
|
|
rows.append(tuple);
|
|
|
|
|
|
|
|
|
|
for (auto& table_descriptor : table_or_subquery_list()) {
|
|
|
|
|
if (!table_descriptor.is_table())
|
2022-02-10 14:43:00 -05:00
|
|
|
return Result { SQLCommand::Select, SQLErrorCode::NotYetImplemented, "Sub-selects are not yet implemented"sv };
|
2022-02-09 15:57:57 -05:00
|
|
|
|
|
|
|
|
auto table_def = TRY(context.database->get_table(table_descriptor.schema_name(), table_descriptor.table_name()));
|
|
|
|
|
if (table_def->num_columns() == 0)
|
2021-11-02 16:49:54 -04:00
|
|
|
continue;
|
2022-02-09 15:57:57 -05:00
|
|
|
|
2021-11-02 16:49:54 -04:00
|
|
|
auto old_descriptor_size = descriptor->size();
|
2022-02-09 15:57:57 -05:00
|
|
|
descriptor->extend(table_def->to_tuple_descriptor());
|
|
|
|
|
|
2022-02-09 17:10:08 -05:00
|
|
|
while (!rows.is_empty() && (rows.first().size() == old_descriptor_size)) {
|
|
|
|
|
auto cartesian_row = rows.take_first();
|
2022-02-09 15:57:57 -05:00
|
|
|
auto table_rows = TRY(context.database->select_all(*table_def));
|
|
|
|
|
|
|
|
|
|
for (auto& table_row : table_rows) {
|
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
|
|
|
|
2022-01-12 09:49:43 -05:00
|
|
|
bool has_ordering { false };
|
2022-02-09 15:57:57 -05:00
|
|
|
auto sort_descriptor = adopt_ref(*new TupleDescriptor);
|
2022-01-12 09:49:43 -05:00
|
|
|
for (auto& term : m_ordering_term_list) {
|
|
|
|
|
sort_descriptor->append(TupleElementDescriptor { .order = term.order() });
|
|
|
|
|
has_ordering = true;
|
|
|
|
|
}
|
|
|
|
|
Tuple sort_key(sort_descriptor);
|
|
|
|
|
|
2021-11-02 16:49:54 -04:00
|
|
|
for (auto& row : rows) {
|
|
|
|
|
context.current_row = &row;
|
2022-02-09 15:57:57 -05:00
|
|
|
|
2021-11-02 16:49:54 -04:00
|
|
|
if (where_clause()) {
|
2022-09-22 08:35:47 -04:00
|
|
|
auto where_result = TRY(where_clause()->evaluate(context)).to_bool();
|
|
|
|
|
if (!where_result.has_value() || !where_result.value())
|
2021-11-02 16:49:54 -04:00
|
|
|
continue;
|
|
|
|
|
}
|
2022-02-09 15:57:57 -05:00
|
|
|
|
2021-11-02 16:49:54 -04:00
|
|
|
tuple.clear();
|
2022-02-09 15:57:57 -05:00
|
|
|
|
2021-11-02 16:49:54 -04:00
|
|
|
for (auto& col : columns) {
|
2022-02-10 07:46:36 -05:00
|
|
|
auto value = TRY(col.expression()->evaluate(context));
|
2021-11-02 16:49:54 -04:00
|
|
|
tuple.append(value);
|
|
|
|
|
}
|
2022-01-12 09:49:43 -05:00
|
|
|
|
|
|
|
|
if (has_ordering) {
|
|
|
|
|
sort_key.clear();
|
|
|
|
|
for (auto& term : m_ordering_term_list) {
|
2022-02-10 07:46:36 -05:00
|
|
|
auto value = TRY(term.expression()->evaluate(context));
|
2022-01-12 09:49:43 -05:00
|
|
|
sort_key.append(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-09 15:57:57 -05:00
|
|
|
|
2022-02-10 14:43:00 -05:00
|
|
|
result.insert_row(tuple, sort_key);
|
2021-11-02 16:49:54 -04:00
|
|
|
}
|
2022-01-12 11:41:58 -05:00
|
|
|
|
|
|
|
|
if (m_limit_clause != nullptr) {
|
|
|
|
|
size_t limit_value = NumericLimits<size_t>::max();
|
2022-02-09 15:57:57 -05:00
|
|
|
size_t offset_value = 0;
|
|
|
|
|
|
2022-02-10 07:46:36 -05:00
|
|
|
auto limit = TRY(m_limit_clause->limit_expression()->evaluate(context));
|
2022-01-12 11:41:58 -05:00
|
|
|
if (!limit.is_null()) {
|
|
|
|
|
auto limit_value_maybe = limit.to_u32();
|
2022-02-09 15:57:57 -05:00
|
|
|
if (!limit_value_maybe.has_value())
|
2022-02-10 14:43:00 -05:00
|
|
|
return Result { SQLCommand::Select, SQLErrorCode::SyntaxError, "LIMIT clause must evaluate to an integer value"sv };
|
2022-02-09 15:57:57 -05:00
|
|
|
|
2022-01-12 11:41:58 -05:00
|
|
|
limit_value = limit_value_maybe.value();
|
|
|
|
|
}
|
2022-02-09 15:57:57 -05:00
|
|
|
|
2022-01-12 11:41:58 -05:00
|
|
|
if (m_limit_clause->offset_expression() != nullptr) {
|
2022-02-10 07:46:36 -05:00
|
|
|
auto offset = TRY(m_limit_clause->offset_expression()->evaluate(context));
|
2022-01-12 11:41:58 -05:00
|
|
|
if (!offset.is_null()) {
|
|
|
|
|
auto offset_value_maybe = offset.to_u32();
|
2022-02-09 15:57:57 -05:00
|
|
|
if (!offset_value_maybe.has_value())
|
2022-02-10 14:43:00 -05:00
|
|
|
return Result { SQLCommand::Select, SQLErrorCode::SyntaxError, "OFFSET clause must evaluate to an integer value"sv };
|
2022-02-09 15:57:57 -05:00
|
|
|
|
2022-01-12 11:41:58 -05:00
|
|
|
offset_value = offset_value_maybe.value();
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-09 15:57:57 -05:00
|
|
|
|
2022-02-10 14:43:00 -05:00
|
|
|
result.limit(offset_value, limit_value);
|
2022-01-12 11:41:58 -05:00
|
|
|
}
|
|
|
|
|
|
2022-02-10 14:43:00 -05:00
|
|
|
return 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|