1#include "duckdb/parser/statement/update_statement.hpp"
2#include "duckdb/parser/query_node/select_node.hpp"
3
4namespace duckdb {
5
6UpdateSetInfo::UpdateSetInfo() {
7}
8
9UpdateSetInfo::UpdateSetInfo(const UpdateSetInfo &other) : columns(other.columns) {
10 if (other.condition) {
11 condition = other.condition->Copy();
12 }
13 for (auto &expr : other.expressions) {
14 expressions.emplace_back(args: expr->Copy());
15 }
16}
17
18unique_ptr<UpdateSetInfo> UpdateSetInfo::Copy() const {
19 return unique_ptr<UpdateSetInfo>(new UpdateSetInfo(*this));
20}
21
22UpdateStatement::UpdateStatement() : SQLStatement(StatementType::UPDATE_STATEMENT) {
23}
24
25UpdateStatement::UpdateStatement(const UpdateStatement &other)
26 : SQLStatement(other), table(other.table->Copy()), set_info(other.set_info->Copy()) {
27 if (other.from_table) {
28 from_table = other.from_table->Copy();
29 }
30 for (auto &expr : other.returning_list) {
31 returning_list.emplace_back(args: expr->Copy());
32 }
33 cte_map = other.cte_map.Copy();
34}
35
36string UpdateStatement::ToString() const {
37 D_ASSERT(set_info);
38 auto &condition = set_info->condition;
39 auto &columns = set_info->columns;
40 auto &expressions = set_info->expressions;
41
42 string result;
43 result = cte_map.ToString();
44 result += "UPDATE ";
45 result += table->ToString();
46 result += " SET ";
47 D_ASSERT(columns.size() == expressions.size());
48 for (idx_t i = 0; i < columns.size(); i++) {
49 if (i > 0) {
50 result += ", ";
51 }
52 result += KeywordHelper::WriteOptionallyQuoted(text: columns[i]);
53 result += " = ";
54 result += expressions[i]->ToString();
55 }
56 if (from_table) {
57 result += " FROM " + from_table->ToString();
58 }
59 if (condition) {
60 result += " WHERE " + condition->ToString();
61 }
62 if (!returning_list.empty()) {
63 result += " RETURNING ";
64 for (idx_t i = 0; i < returning_list.size(); i++) {
65 if (i > 0) {
66 result += ", ";
67 }
68 result += returning_list[i]->ToString();
69 }
70 }
71 return result;
72}
73
74unique_ptr<SQLStatement> UpdateStatement::Copy() const {
75 return unique_ptr<UpdateStatement>(new UpdateStatement(*this));
76}
77
78} // namespace duckdb
79