summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibSQL/AST/Select.cpp
blob: 6214e67015e3622de9198d7b688227dab9a186dc (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
/*
 * 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);
        Tuple tuple(descriptor);
        for (auto& row : context.database->select_all(*table)) {
            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;
    }
    return SQLResult::construct();
}

}