/src/duckdb/src/main/relation/table_relation.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include "duckdb/main/relation/table_relation.hpp" |
2 | | #include "duckdb/parser/tableref/basetableref.hpp" |
3 | | #include "duckdb/parser/query_node/select_node.hpp" |
4 | | #include "duckdb/parser/expression/star_expression.hpp" |
5 | | #include "duckdb/main/relation/delete_relation.hpp" |
6 | | #include "duckdb/main/relation/update_relation.hpp" |
7 | | #include "duckdb/parser/parser.hpp" |
8 | | #include "duckdb/main/client_context.hpp" |
9 | | |
10 | | namespace duckdb { |
11 | | |
12 | | TableRelation::TableRelation(const shared_ptr<ClientContext> &context, unique_ptr<TableDescription> description) |
13 | 0 | : Relation(context, RelationType::TABLE_RELATION), description(std::move(description)) { |
14 | 0 | } |
15 | | |
16 | 0 | unique_ptr<QueryNode> TableRelation::GetQueryNode() { |
17 | 0 | auto result = make_uniq<SelectNode>(); |
18 | 0 | result->select_list.push_back(make_uniq<StarExpression>()); |
19 | 0 | result->from_table = GetTableRef(); |
20 | 0 | return std::move(result); |
21 | 0 | } |
22 | | |
23 | 0 | unique_ptr<TableRef> TableRelation::GetTableRef() { |
24 | 0 | auto table_ref = make_uniq<BaseTableRef>(); |
25 | 0 | table_ref->schema_name = description->schema; |
26 | 0 | table_ref->table_name = description->table; |
27 | 0 | return std::move(table_ref); |
28 | 0 | } |
29 | | |
30 | 0 | string TableRelation::GetAlias() { |
31 | 0 | return description->table; |
32 | 0 | } |
33 | | |
34 | 0 | const vector<ColumnDefinition> &TableRelation::Columns() { |
35 | 0 | return description->columns; |
36 | 0 | } |
37 | | |
38 | 0 | string TableRelation::ToString(idx_t depth) { |
39 | 0 | return RenderWhitespace(depth) + "Scan Table [" + description->table + "]"; |
40 | 0 | } |
41 | | |
42 | 0 | static unique_ptr<ParsedExpression> ParseCondition(ClientContext &context, const string &condition) { |
43 | 0 | if (!condition.empty()) { |
44 | 0 | auto expression_list = Parser::ParseExpressionList(condition, context.GetParserOptions()); |
45 | 0 | if (expression_list.size() != 1) { |
46 | 0 | throw ParserException("Expected a single expression as filter condition"); |
47 | 0 | } |
48 | 0 | return std::move(expression_list[0]); |
49 | 0 | } else { |
50 | 0 | return nullptr; |
51 | 0 | } |
52 | 0 | } |
53 | | |
54 | 0 | void TableRelation::Update(const string &update_list, const string &condition) { |
55 | 0 | vector<string> update_columns; |
56 | 0 | vector<unique_ptr<ParsedExpression>> expressions; |
57 | 0 | auto cond = ParseCondition(*context.GetContext(), condition); |
58 | 0 | Parser::ParseUpdateList(update_list, update_columns, expressions, context.GetContext()->GetParserOptions()); |
59 | 0 | auto update = make_shared_ptr<UpdateRelation>(context, std::move(cond), description->schema, description->table, |
60 | 0 | std::move(update_columns), std::move(expressions)); |
61 | 0 | update->Execute(); |
62 | 0 | } |
63 | | |
64 | 0 | void TableRelation::Delete(const string &condition) { |
65 | 0 | auto cond = ParseCondition(*context.GetContext(), condition); |
66 | 0 | auto del = make_shared_ptr<DeleteRelation>(context, std::move(cond), description->schema, description->table); |
67 | 0 | del->Execute(); |
68 | 0 | } |
69 | | |
70 | | } // namespace duckdb |