@@ -86,7 +86,7 @@ class WsjcppSqlBuilderUpdate : public WsjcppSqlQuery {
8686class WsjcppSqlBuilder2 ;
8787
8888
89- enum class WsjcppSqlWhereType { LOGICAL_OPERATOR, CONDITION, SUB };
89+ enum class WsjcppSqlWhereType { LOGICAL_OPERATOR, CONDITION, SUB_CONDITION };
9090
9191class WsjcppSqlWhereBase {
9292public:
@@ -130,7 +130,8 @@ class WsjcppSqlSelect;
130130template <class T >
131131class WsjcppSqlWhere : public WsjcppSqlWhereBase {
132132public:
133- WsjcppSqlWhere (WsjcppSqlBuilder2 *builder, T *query) : WsjcppSqlWhereBase(WsjcppSqlWhereType::SUB), m_builder(builder), m_query(query) { }
133+ WsjcppSqlWhere (WsjcppSqlWhere<T> *parent, WsjcppSqlBuilder2 *builder, T *query)
134+ : WsjcppSqlWhereBase(WsjcppSqlWhereType::SUB_CONDITION), m_parent(parent), m_builder(builder), m_query(query) { }
134135
135136 WsjcppSqlWhere<T> ¬Equal (const std::string &name, const std::string &value) {
136137 cond (name, WsjcppSqlWhereConditionType::NOT_EQUAL, value);
@@ -184,14 +185,40 @@ class WsjcppSqlWhere : public WsjcppSqlWhereBase {
184185 return *this ;
185186 }
186187
188+ WsjcppSqlWhere<T> &subCondition () {
189+ if (
190+ m_conditions.size () > 0
191+ && m_conditions[m_conditions.size ()-1 ]->type () == WsjcppSqlWhereType::CONDITION
192+ ) {
193+ and_ (); // default add and_
194+ }
195+ auto sub_cond = std::make_shared<WsjcppSqlWhere<T>>(this , m_builder, m_query);
196+ m_conditions.push_back (sub_cond);
197+ return *(sub_cond.get ());
198+ }
199+
200+ WsjcppSqlWhere<T> &finishSubCondition () {
201+ // TODO return parent
202+ if (m_parent != nullptr ) {
203+ return *m_parent;
204+ }
205+ // default return current where
206+ // TODO warning to builder
207+ return *this ;
208+ }
209+
187210 T &endWhere () {
188211 return *m_query;
189212 }
190213
191214 virtual std::string sql () override {
192215 std::string ret = " " ;
193216 for (auto item : m_conditions) {
194- ret += item->sql ();
217+ if (item->type () == WsjcppSqlWhereType::SUB_CONDITION) {
218+ ret += " (" + item->sql () + " )" ;
219+ } else {
220+ ret += item->sql ();
221+ }
195222 }
196223 return ret;
197224 }
@@ -210,6 +237,7 @@ class WsjcppSqlWhere : public WsjcppSqlWhereBase {
210237
211238 WsjcppSqlBuilder2 *m_builder;
212239 T *m_query;
240+ WsjcppSqlWhere<T> *m_parent;
213241 std::vector<std::shared_ptr<WsjcppSqlWhereBase>> m_conditions;
214242};
215243
0 commit comments