1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/parser/expression/case_expression.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/parser/parsed_expression.hpp"
12#include "duckdb/common/vector.hpp"
13
14namespace duckdb {
15
16struct CaseCheck {
17 unique_ptr<ParsedExpression> when_expr;
18 unique_ptr<ParsedExpression> then_expr;
19
20 void FormatSerialize(FormatSerializer &serializer) const;
21 static CaseCheck FormatDeserialize(FormatDeserializer &deserializer);
22};
23
24//! The CaseExpression represents a CASE expression in the query
25class CaseExpression : public ParsedExpression {
26public:
27 static constexpr const ExpressionClass TYPE = ExpressionClass::CASE;
28
29public:
30 DUCKDB_API CaseExpression();
31
32 vector<CaseCheck> case_checks;
33 unique_ptr<ParsedExpression> else_expr;
34
35public:
36 string ToString() const override;
37
38 static bool Equal(const CaseExpression &a, const CaseExpression &b);
39
40 unique_ptr<ParsedExpression> Copy() const override;
41
42 void Serialize(FieldWriter &writer) const override;
43 static unique_ptr<ParsedExpression> Deserialize(ExpressionType type, FieldReader &source);
44 void FormatSerialize(FormatSerializer &serializer) const override;
45 static unique_ptr<ParsedExpression> FormatDeserialize(ExpressionType type, FormatDeserializer &deserializer);
46
47public:
48 template <class T, class BASE>
49 static string ToString(const T &entry) {
50 string case_str = "CASE ";
51 for (auto &check : entry.case_checks) {
52 case_str += " WHEN (" + check.when_expr->ToString() + ")";
53 case_str += " THEN (" + check.then_expr->ToString() + ")";
54 }
55 case_str += " ELSE " + entry.else_expr->ToString();
56 case_str += " END";
57 return case_str;
58 }
59};
60} // namespace duckdb
61