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