summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibSQL/AST/Select.cpp
blob: 81ef322a57632e26649a2a2d9df0c346e4834afb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * 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
{
    NonnullRefPtrVector<ResultColumn> columns;
    for (auto& table_descriptor : table_or_subquery_list()) {
        if (!table_descriptor.is_table())
            TODO();
        auto table = context.database->get_table(table_descriptor.schema_name(), table_descriptor.table_name());
        if (!table) {
            return SQLResult::construct(SQL::SQLCommand::Select, SQL::SQLErrorCode::TableDoesNotExist, table_descriptor.table_name());
        }
        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()),
                        ""));
            }
        }
    }

    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
                return SQLResult::construct(SQL::SQLCommand::Select, SQL::SQLErrorCode::SyntaxError, "*");
            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())
            TODO();
        auto table = context.database->get_table(table_descriptor.schema_name(), table_descriptor.table_name());
        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);
            for (auto& table_row : context.database->select_all(*table)) {
                auto new_row = cartesian_row;
                new_row.extend(table_row);
                rows.append(new_row);
            }
        }
    }

    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;
}

}