mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 03:08:13 +00:00
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.
This commit is contained in:
parent
f33a288ca4
commit
fe50598a03
8 changed files with 90 additions and 20 deletions
|
@ -298,7 +298,7 @@ private:
|
|||
struct ExecutionContext {
|
||||
NonnullRefPtr<Database> database;
|
||||
RefPtr<SQLResult> result { nullptr };
|
||||
Tuple current_row {};
|
||||
Tuple* current_row { nullptr };
|
||||
};
|
||||
|
||||
class Expression : public ASTNode {
|
||||
|
@ -429,6 +429,7 @@ public:
|
|||
const String& schema_name() const { return m_schema_name; }
|
||||
const String& table_name() const { return m_table_name; }
|
||||
const String& column_name() const { return m_column_name; }
|
||||
virtual Value evaluate(ExecutionContext&) const override;
|
||||
|
||||
private:
|
||||
String m_schema_name;
|
||||
|
|
|
@ -87,4 +87,17 @@ Value UnaryOperatorExpression::evaluate(ExecutionContext& context) const
|
|||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
Value ColumnNameExpression::evaluate(ExecutionContext& context) const
|
||||
{
|
||||
auto& descriptor = *context.current_row->descriptor();
|
||||
VERIFY(context.current_row->size() == descriptor.size());
|
||||
for (auto ix = 0u; ix < context.current_row->size(); ix++) {
|
||||
auto& column_descriptor = descriptor[ix];
|
||||
if (column_descriptor.name == column_name())
|
||||
return { (*context.current_row)[ix] };
|
||||
}
|
||||
// TODO: Error handling.
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -14,22 +14,36 @@ namespace SQL::AST {
|
|||
RefPtr<SQLResult> Select::execute(ExecutionContext& context) const
|
||||
{
|
||||
if (table_or_subquery_list().size() == 1 && table_or_subquery_list()[0].is_table()) {
|
||||
if (result_column_list().size() == 1 && result_column_list()[0].type() == ResultType::All) {
|
||||
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());
|
||||
}
|
||||
NonnullRefPtr<TupleDescriptor> descriptor = table->to_tuple_descriptor();
|
||||
context.result = SQLResult::construct();
|
||||
for (auto& row : context.database->select_all(*table)) {
|
||||
Tuple tuple(descriptor);
|
||||
for (auto ix = 0u; ix < descriptor->size(); ix++) {
|
||||
tuple[ix] = row[ix];
|
||||
}
|
||||
context.result->append(tuple);
|
||||
}
|
||||
return context.result;
|
||||
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();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue