ladybird/Userland/Libraries/LibSQL/AST/Select.cpp
Jan de Visser fe50598a03 LibSQL: Allow expressions and column names in SELECT ... FROM
Up to now the only ``SELECT`` statement that worked was ``SELECT *
FROM <table>``. This commit allows a column list consisting of
column names and expressions in addition to ``*``. ``WHERE``
still doesn't work though.
2021-10-05 02:22:19 +02:00

51 lines
1.8 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>
#include <LibSQL/Meta.h>
#include <LibSQL/Row.h>
namespace SQL::AST {
RefPtr<SQLResult> Select::execute(ExecutionContext& context) const
{
if (table_or_subquery_list().size() == 1 && table_or_subquery_list()[0].is_table()) {
auto table = context.database->get_table(table_or_subquery_list()[0].schema_name(), table_or_subquery_list()[0].table_name());
if (!table) {
return SQLResult::construct(SQL::SQLCommand::Select, SQL::SQLErrorCode::TableDoesNotExist, table_or_subquery_list()[0].table_name());
}
NonnullRefPtrVector<ResultColumn> columns;
if (result_column_list().size() == 1 && result_column_list()[0].type() == ResultType::All) {
for (auto& col : table->columns()) {
columns.append(
create_ast_node<ResultColumn>(
create_ast_node<ColumnNameExpression>(table->parent()->name(), table->name(), col.name()),
""));
}
} else {
for (auto& col : result_column_list()) {
columns.append(col);
}
}
context.result = SQLResult::construct();
AK::NonnullRefPtr<TupleDescriptor> descriptor = AK::adopt_ref(*new TupleDescriptor);
for (auto& row : context.database->select_all(*table)) {
context.current_row = &row;
Tuple tuple(descriptor);
for (auto& col : columns) {
auto value = col.expression()->evaluate(context);
tuple.append(value);
}
context.result->append(tuple);
}
return context.result;
}
return SQLResult::construct();
}
}