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 std::shared_ptr<ClientContext> &context, unique_ptr<TableDescription> description) |
13 | : Relation(context, RelationType::TABLE_RELATION), description(std::move(description)) { |
14 | } |
15 | |
16 | unique_ptr<QueryNode> TableRelation::GetQueryNode() { |
17 | auto result = make_uniq<SelectNode>(); |
18 | result->select_list.push_back(x: make_uniq<StarExpression>()); |
19 | result->from_table = GetTableRef(); |
20 | return std::move(result); |
21 | } |
22 | |
23 | unique_ptr<TableRef> TableRelation::GetTableRef() { |
24 | auto table_ref = make_uniq<BaseTableRef>(); |
25 | table_ref->schema_name = description->schema; |
26 | table_ref->table_name = description->table; |
27 | return std::move(table_ref); |
28 | } |
29 | |
30 | string TableRelation::GetAlias() { |
31 | return description->table; |
32 | } |
33 | |
34 | const vector<ColumnDefinition> &TableRelation::Columns() { |
35 | return description->columns; |
36 | } |
37 | |
38 | string TableRelation::ToString(idx_t depth) { |
39 | return RenderWhitespace(depth) + "Scan Table [" + description->table + "]" ; |
40 | } |
41 | |
42 | static unique_ptr<ParsedExpression> ParseCondition(ClientContext &context, const string &condition) { |
43 | if (!condition.empty()) { |
44 | auto expression_list = Parser::ParseExpressionList(select_list: condition, options: context.GetParserOptions()); |
45 | if (expression_list.size() != 1) { |
46 | throw ParserException("Expected a single expression as filter condition" ); |
47 | } |
48 | return std::move(expression_list[0]); |
49 | } else { |
50 | return nullptr; |
51 | } |
52 | } |
53 | |
54 | void TableRelation::Update(const string &update_list, const string &condition) { |
55 | vector<string> update_columns; |
56 | vector<unique_ptr<ParsedExpression>> expressions; |
57 | auto cond = ParseCondition(context&: *context.GetContext(), condition); |
58 | Parser::ParseUpdateList(update_list, update_columns, expressions, options: context.GetContext()->GetParserOptions()); |
59 | auto update = make_shared<UpdateRelation>(args&: context, args: std::move(cond), args&: description->schema, args&: description->table, |
60 | args: std::move(update_columns), args: std::move(expressions)); |
61 | update->Execute(); |
62 | } |
63 | |
64 | void TableRelation::Delete(const string &condition) { |
65 | auto cond = ParseCondition(context&: *context.GetContext(), condition); |
66 | auto del = make_shared<DeleteRelation>(args&: context, args: std::move(cond), args&: description->schema, args&: description->table); |
67 | del->Execute(); |
68 | } |
69 | |
70 | } // namespace duckdb |
71 | |