Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
database::query_condition Class Reference

Represents a WHERE condition in a query. More...

#include <query_builder.h>

Collaboration diagram for database::query_condition:
Collaboration graph

Public Member Functions

 query_condition (const std::string &field, const std::string &op, const core::database_value &value)
 
std::string to_sql () const
 
std::string to_mongodb () const
 
std::string to_redis () const
 
query_condition operator&& (const query_condition &other) const
 
query_condition operator|| (const query_condition &other) const
 

Private Member Functions

 query_condition ()
 

Private Attributes

std::string field_
 
std::string operator_
 
core::database_value value_
 
std::string raw_condition_
 
std::vector< query_conditionsub_conditions_
 
std::string logical_operator_
 

Detailed Description

Represents a WHERE condition in a query.

Definition at line 46 of file query_builder.h.

Constructor & Destructor Documentation

◆ query_condition() [1/2]

database::query_condition::query_condition ( const std::string & field,
const std::string & op,
const core::database_value & value )

Definition at line 13 of file query_builder.cpp.

14 : field_(field), operator_(op), value_(value)
15 {
16 }
core::database_value value_

◆ query_condition() [2/2]

database::query_condition::query_condition ( )
private

Definition at line 18 of file query_builder.cpp.

19 {
20 }

Member Function Documentation

◆ operator&&()

query_condition database::query_condition::operator&& ( const query_condition & other) const

Definition at line 144 of file query_builder.cpp.

145 {
146 query_condition result;
147 result.sub_conditions_.push_back(*this);
148 result.sub_conditions_.push_back(other);
149 result.logical_operator_ = "AND";
150 return result;
151 }

References logical_operator_, and sub_conditions_.

◆ operator||()

query_condition database::query_condition::operator|| ( const query_condition & other) const

Definition at line 153 of file query_builder.cpp.

154 {
155 query_condition result;
156 result.sub_conditions_.push_back(*this);
157 result.sub_conditions_.push_back(other);
158 result.logical_operator_ = "OR";
159 return result;
160 }

References logical_operator_, and sub_conditions_.

◆ to_mongodb()

std::string database::query_condition::to_mongodb ( ) const

Definition at line 72 of file query_builder.cpp.

73 {
74 if (!raw_condition_.empty()) {
75 return raw_condition_;
76 }
77
78 if (!sub_conditions_.empty()) {
79 std::ostringstream oss;
80 std::string mongo_op = (logical_operator_ == "AND") ? "$and" : "$or";
81 oss << "{ \"" << mongo_op << "\": [";
82 for (size_t i = 0; i < sub_conditions_.size(); ++i) {
83 if (i > 0) oss << ", ";
84 oss << sub_conditions_[i].to_mongodb();
85 }
86 oss << "] }";
87 return oss.str();
88 }
89
90 std::ostringstream oss;
91 oss << "{ \"" << field_ << "\": ";
92
93 if (operator_ == "=") {
94 std::visit([&oss](const auto& val) {
95 using T = std::decay_t<decltype(val)>;
96 if constexpr (std::is_same_v<T, std::string>) {
97 oss << "\"" << val << "\"";
98 } else if constexpr (std::is_same_v<T, int64_t>) {
99 oss << val;
100 } else if constexpr (std::is_same_v<T, double>) {
101 oss << val;
102 } else if constexpr (std::is_same_v<T, bool>) {
103 oss << (val ? "true" : "false");
104 } else if constexpr (std::is_same_v<T, std::monostate>) {
105 oss << "null";
106 }
107 }, value_);
108 } else {
109 std::string mongo_op;
110 if (operator_ == ">") mongo_op = "$gt";
111 else if (operator_ == ">=") mongo_op = "$gte";
112 else if (operator_ == "<") mongo_op = "$lt";
113 else if (operator_ == "<=") mongo_op = "$lte";
114 else if (operator_ == "!=") mongo_op = "$ne";
115 else mongo_op = "$eq";
116
117 oss << "{ \"" << mongo_op << "\": ";
118 std::visit([&oss](const auto& val) {
119 using T = std::decay_t<decltype(val)>;
120 if constexpr (std::is_same_v<T, std::string>) {
121 oss << "\"" << val << "\"";
122 } else if constexpr (std::is_same_v<T, int64_t>) {
123 oss << val;
124 } else if constexpr (std::is_same_v<T, double>) {
125 oss << val;
126 } else if constexpr (std::is_same_v<T, bool>) {
127 oss << (val ? "true" : "false");
128 } else if constexpr (std::is_same_v<T, std::monostate>) {
129 oss << "null";
130 }
131 }, value_);
132 oss << " }";
133 }
134
135 oss << " }";
136 return oss.str();
137 }
std::vector< query_condition > sub_conditions_

References field_, logical_operator_, operator_, raw_condition_, sub_conditions_, and value_.

◆ to_redis()

std::string database::query_condition::to_redis ( ) const

Definition at line 139 of file query_builder.cpp.

140 {
141 return raw_condition_;
142 }

References raw_condition_.

◆ to_sql()

std::string database::query_condition::to_sql ( ) const

Definition at line 22 of file query_builder.cpp.

23 {
24 if (!raw_condition_.empty()) {
25 return raw_condition_;
26 }
27
28 if (!sub_conditions_.empty()) {
29 std::ostringstream oss;
30 oss << "(";
31 for (size_t i = 0; i < sub_conditions_.size(); ++i) {
32 if (i > 0) {
33 oss << " " << logical_operator_ << " ";
34 }
35 oss << sub_conditions_[i].to_sql();
36 }
37 oss << ")";
38 return oss.str();
39 }
40
41 std::ostringstream oss;
42 oss << field_ << " " << operator_ << " ";
43
44 std::visit([&oss](const auto& val) {
45 using T = std::decay_t<decltype(val)>;
46 if constexpr (std::is_same_v<T, std::string>) {
47 // Escape single quotes by doubling them (SQL standard)
48 std::string escaped;
49 escaped.reserve(val.size() + 10);
50 for (char c : val) {
51 if (c == '\'') {
52 escaped += "''";
53 } else {
54 escaped += c;
55 }
56 }
57 oss << "'" << escaped << "'";
58 } else if constexpr (std::is_same_v<T, int64_t>) {
59 oss << val;
60 } else if constexpr (std::is_same_v<T, double>) {
61 oss << val;
62 } else if constexpr (std::is_same_v<T, bool>) {
63 oss << (val ? "TRUE" : "FALSE");
64 } else if constexpr (std::is_same_v<T, std::monostate>) {
65 oss << "NULL";
66 }
67 }, value_);
68
69 return oss.str();
70 }

References field_, logical_operator_, operator_, raw_condition_, sub_conditions_, and value_.

Member Data Documentation

◆ field_

std::string database::query_condition::field_
private

Definition at line 63 of file query_builder.h.

Referenced by to_mongodb(), and to_sql().

◆ logical_operator_

std::string database::query_condition::logical_operator_
private

Definition at line 68 of file query_builder.h.

Referenced by operator&&(), operator||(), to_mongodb(), and to_sql().

◆ operator_

std::string database::query_condition::operator_
private

Definition at line 64 of file query_builder.h.

Referenced by to_mongodb(), and to_sql().

◆ raw_condition_

std::string database::query_condition::raw_condition_
private

Definition at line 66 of file query_builder.h.

Referenced by to_mongodb(), to_redis(), and to_sql().

◆ sub_conditions_

std::vector<query_condition> database::query_condition::sub_conditions_
private

Definition at line 67 of file query_builder.h.

Referenced by operator&&(), operator||(), to_mongodb(), and to_sql().

◆ value_

core::database_value database::query_condition::value_
private

Definition at line 65 of file query_builder.h.

Referenced by to_mongodb(), and to_sql().


The documentation for this class was generated from the following files: