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 {
|
|
|
|
|
|
2023-02-03 09:58:38 -05:00
|
|
|
static DeprecatedString result_column_name(ResultColumn const& column, size_t column_index)
|
|
|
|
|
{
|
|
|
|
|
auto fallback_column_name = [column_index]() {
|
|
|
|
|
return DeprecatedString::formatted("Column{}", column_index);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (auto const& alias = column.column_alias(); !alias.is_empty())
|
|
|
|
|
return alias;
|
|
|
|
|
|
|
|
|
|
if (column.select_from_expression()) {
|
|
|
|
|
if (is<ColumnNameExpression>(*column.expression())) {
|
|
|
|
|
auto const& column_name_expression = verify_cast<ColumnNameExpression>(*column.expression());
|
|
|
|
|
return column_name_expression.column_name();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME: Generate column names from other result column expressions.
|
|
|
|
|
return fallback_column_name();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VERIFY(column.select_from_table());
|
|
|
|
|
|
|
|
|
|
// FIXME: Generate column names from select-from-table result columns.
|
|
|
|
|
return fallback_column_name();
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
{
|
2023-02-19 23:22:46 +01:00
|
|
|
NonnullRefPtrVector<ResultColumn const> columns;
|
2023-02-03 09:58:38 -05:00
|
|
|
Vector<DeprecatedString> column_names;
|
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) {
|
2023-02-03 09:58:38 -05:00
|
|
|
TRY(columns.try_ensure_capacity(columns.size() + table_def->columns().size()));
|
|
|
|
|
TRY(column_names.try_ensure_capacity(column_names.size() + table_def->columns().size()));
|
|
|
|
|
|
2022-02-09 15:57:57 -05:00
|
|
|
for (auto& col : table_def->columns()) {
|
2023-02-03 09:58:38 -05:00
|
|
|
columns.unchecked_append(
|
2021-09-16 22:29:19 +02:00
|
|
|
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
|
|
|
""));
|
2023-02-03 09:58:38 -05:00
|
|
|
|
|
|
|
|
column_names.unchecked_append(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
|
|
|
}
|
|
|
|
|
|
2022-02-09 15:57:57 -05:00
|
|
|
if (result_column_list.size() != 1 || result_column_list[0].type() != ResultType::All) {
|
2023-02-03 09:58:38 -05:00
|
|
|
TRY(columns.try_ensure_capacity(result_column_list.size()));
|
|
|
|
|
TRY(column_names.try_ensure_capacity(result_column_list.size()));
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < result_column_list.size(); ++i) {
|
|
|
|
|
auto const& col = result_column_list[i];
|
|
|
|
|
|
2022-02-09 15:57:57 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2023-02-03 09:58:38 -05:00
|
|
|
columns.unchecked_append(col);
|
|
|
|
|
column_names.unchecked_append(result_column_name(col, i));
|
2021-11-02 16:49:54 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 09:58:38 -05:00
|
|
|
ResultSet result { SQLCommand::Select, move(column_names) };
|
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()) {
|
LibSQL: Support 64-bit integer values and handle overflow errors
Currently, integers are stored in LibSQL as 32-bit signed integers, even
if the provided type is unsigned. This resulted in a series of unchecked
unsigned-to-signed conversions, and prevented storing 64-bit values.
Further, mathematical operations were performed without similar checks,
and without checking for overflow.
This changes SQL::Value to behave like SQLite for INTEGER types. In
SQLite, the INTEGER type does not imply a size or signedness of the
underlying type. Instead, SQLite determines on-the-fly what type is
needed as values are created and updated.
To do so, the SQL::Value variant can now hold an i64 or u64 integer. If
a specific type is requested, invalid conversions are now explictly an
error (e.g. converting a stored -1 to a u64 will fail). When binary
mathematical operations are performed, we now try to coerce the RHS
value to a type that works with the LHS value, failing the operation if
that isn't possible. Any overflow or invalid operation (e.g. bitshifting
a 64-bit value by more than 64 bytes) is an error.
2022-12-11 11:44:11 -05:00
|
|
|
auto limit_value_maybe = limit.to_int<size_t>();
|
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()) {
|
LibSQL: Support 64-bit integer values and handle overflow errors
Currently, integers are stored in LibSQL as 32-bit signed integers, even
if the provided type is unsigned. This resulted in a series of unchecked
unsigned-to-signed conversions, and prevented storing 64-bit values.
Further, mathematical operations were performed without similar checks,
and without checking for overflow.
This changes SQL::Value to behave like SQLite for INTEGER types. In
SQLite, the INTEGER type does not imply a size or signedness of the
underlying type. Instead, SQLite determines on-the-fly what type is
needed as values are created and updated.
To do so, the SQL::Value variant can now hold an i64 or u64 integer. If
a specific type is requested, invalid conversions are now explictly an
error (e.g. converting a stored -1 to a u64 will fail). When binary
mathematical operations are performed, we now try to coerce the RHS
value to a type that works with the LHS value, failing the operation if
that isn't possible. Any overflow or invalid operation (e.g. bitshifting
a 64-bit value by more than 64 bytes) is an error.
2022-12-11 11:44:11 -05:00
|
|
|
auto offset_value_maybe = offset.to_int<size_t>();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|