Coverage Report

Created: 2025-06-12 07:25

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