Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
condition_builder.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#include "condition_builder.h"
6#include <sstream>
7
8namespace database::query {
9
11 : current_group_level_(0) {}
12
14 condition_node node;
15 node.cond = cond;
16 node.op = op;
18 node.is_group_start = false;
19 node.is_group_end = false;
20 nodes_.push_back(node);
21 return *this;
22}
23
24condition_builder& condition_builder::add(const std::string& field, const std::string& operator_,
25 const core::database_value& value, logical_op op) {
26 condition cond;
27 cond.field = field;
28 cond.op = operator_;
29 cond.value = value;
30 return add(cond, op);
31}
32
34 condition cond;
35 cond.raw = raw;
36 return add(cond, op);
37}
38
40 condition_node node;
42 node.is_group_start = true;
43 node.is_group_end = false;
44 node.op = logical_op::AND; // Default, will be overridden
45 nodes_.push_back(node);
46
48 return *this;
49}
50
52 if (current_group_level_ > 0) {
54
55 condition_node node;
57 node.is_group_start = false;
58 node.is_group_end = true;
59 node.op = logical_op::AND;
60 nodes_.push_back(node);
61 }
62 return *this;
63}
64
65std::string condition_builder::build(const value_formatter& formatter) const {
66 if (nodes_.empty()) {
67 return "";
68 }
69
70 std::ostringstream oss;
71 bool first = true;
72
73 for (const auto& node : nodes_) {
74 if (node.is_group_start) {
75 if (!first) {
76 oss << " " << logical_op_to_string(node.op) << " ";
77 }
78 oss << "(";
79 first = true;
80 continue;
81 }
82
83 if (node.is_group_end) {
84 oss << ")";
85 first = false;
86 continue;
87 }
88
89 if (!first) {
90 oss << " " << logical_op_to_string(node.op) << " ";
91 }
92
93 oss << format_condition(node.cond, formatter);
94 first = false;
95 }
96
97 return oss.str();
98}
99
101 return nodes_.empty();
102}
103
105 nodes_.clear();
107}
108
110 const value_formatter& formatter) const {
111 if (cond.is_raw()) {
112 return cond.raw;
113 }
114
115 std::ostringstream oss;
116 oss << formatter.escape_identifier(cond.field) << " " << cond.op << " ";
117
118 // Handle special operators
119 if (cond.op == "IS NULL" || cond.op == "IS NOT NULL") {
120 // No value needed
121 return formatter.escape_identifier(cond.field) + " " + cond.op;
122 }
123
124 oss << formatter.format(cond.value);
125 return oss.str();
126}
127
129 switch (op) {
130 case logical_op::AND:
131 return "AND";
132 case logical_op::OR:
133 return "OR";
134 default:
135 return "AND";
136 }
137}
138
139} // namespace database::query
Builds WHERE clause conditions with proper precedence.
condition_builder & end_group()
End a condition group (adds closing parenthesis)
void clear()
Clear all conditions.
condition_builder & add_raw(const std::string &raw, logical_op op=logical_op::AND)
Add a raw SQL condition.
std::string logical_op_to_string(logical_op op) const
std::vector< condition_node > nodes_
condition_builder & begin_group()
Begin a condition group (adds opening parenthesis)
std::string build(const value_formatter &formatter) const
Build the WHERE clause.
std::string format_condition(const condition &cond, const value_formatter &formatter) const
bool empty() const
Check if builder is empty.
condition_builder & add(const condition &cond, logical_op op=logical_op::AND)
Add a condition.
Formats database values for different backends.
std::string escape_identifier(const std::string &identifier) const
Quote and escape an identifier (table/column name)
std::string format(const core::database_value &value) const
Format a database value.
std::variant< std::string, int64_t, double, bool, std::nullptr_t > database_value
logical_op
Logical operators for combining conditions.
Represents a single WHERE condition.
core::database_value value
Value to compare.
std::string raw
Raw SQL condition (if any)
std::string field
Field name.
std::string op
Operator (=, !=, <, >, <=, >=, LIKE, IN, etc.)