@@ -86,7 +86,7 @@ class WsjcppSqlBuilderUpdate : public WsjcppSqlQuery {
8686class WsjcppSqlBuilder2 ;
8787
8888
89- enum class WsjcppSqlWhereType { OR, AND , CONDITION, SUB };
89+ enum class WsjcppSqlWhereType { LOGICAL_OPERATOR , CONDITION, SUB };
9090
9191class WsjcppSqlWhereBase {
9292public:
@@ -127,32 +127,89 @@ class WsjcppSqlWhereCondition : public WsjcppSqlWhereBase {
127127
128128class WsjcppSqlSelect ;
129129
130- // template<class T>
130+ template <class T >
131131class WsjcppSqlWhere : public WsjcppSqlWhereBase {
132132public:
133- // WsjcppSqlWhere(WsjcppSqlBuilder2 *builder, T *query) : WsjcppSqlWhereBase(WsjcppSqlWhereType::SUB), m_builder(builder), m_query(query) { }
134- WsjcppSqlWhere (WsjcppSqlBuilder2 *builder, WsjcppSqlSelect *query) : WsjcppSqlWhereBase(WsjcppSqlWhereType::SUB), m_builder(builder), m_query(query) { }
135- WsjcppSqlWhere ¬Equal (const std::string &name, const std::string &value);
136- WsjcppSqlWhere &equal (const std::string &name, const std::string &value);
137- WsjcppSqlWhere &moreThen (const std::string &name, const std::string &value);
138- WsjcppSqlWhere &lessThen (const std::string &name, const std::string &value);
139- WsjcppSqlWhere &like (const std::string &name, const std::string &value);
140-
141- WsjcppSqlWhere &or_ ();
142- WsjcppSqlWhere &and_ ();
143- // T &endWhere() {
144- WsjcppSqlSelect &endWhere () {
133+ WsjcppSqlWhere (WsjcppSqlBuilder2 *builder, T *query) : WsjcppSqlWhereBase(WsjcppSqlWhereType::SUB), m_builder(builder), m_query(query) { }
134+
135+ WsjcppSqlWhere<T> ¬Equal (const std::string &name, const std::string &value) {
136+ cond (name, WsjcppSqlWhereConditionType::NOT_EQUAL, value);
137+ return *this ;
138+ }
139+
140+ WsjcppSqlWhere<T> &equal (const std::string &name, const std::string &value) {
141+ cond (name, WsjcppSqlWhereConditionType::EQUAL, value);
142+ return *this ;
143+ }
144+
145+ WsjcppSqlWhere<T> &moreThen (const std::string &name, const std::string &value) {
146+ cond (name, WsjcppSqlWhereConditionType::MORE_THEN, value);
147+ return *this ;
148+ }
149+
150+ WsjcppSqlWhere<T> &lessThen (const std::string &name, const std::string &value) {
151+ cond (name, WsjcppSqlWhereConditionType::LESS_THEN, value);
152+ return *this ;
153+ }
154+
155+ WsjcppSqlWhere<T> &like (const std::string &name, const std::string &value) {
156+ cond (name, WsjcppSqlWhereConditionType::LIKE, value);
157+ return *this ;
158+ }
159+
160+ WsjcppSqlWhere<T> &or_ () {
161+ if (
162+ m_conditions.size () > 0
163+ && m_conditions[m_conditions.size ()-1 ]->type () == WsjcppSqlWhereType::LOGICAL_OPERATOR
164+ ) {
165+ // TODO
166+ // m_builder->addError("[WARNING] WsjcppSqlWhere. Last item alredy defined as logical_operator. current will be skipped.");
167+ return *this ;
168+ }
169+
170+ m_conditions.push_back (std::make_shared<WsjcppSqlWhereOr>());
171+ return *this ;
172+ }
173+
174+ WsjcppSqlWhere<T> &and_ () {
175+ if (
176+ m_conditions.size () > 0
177+ && m_conditions[m_conditions.size ()-1 ]->type () == WsjcppSqlWhereType::LOGICAL_OPERATOR
178+ ) {
179+ // TODO
180+ // m_builder->addError("[WARNING] WsjcppSqlWhere. Last item alredy defined as logical_operator. current will be skipped.");
181+ return *this ;
182+ }
183+ m_conditions.push_back (std::make_shared<WsjcppSqlWhereAnd>());
184+ return *this ;
185+ }
186+
187+ T &endWhere () {
145188 return *m_query;
146189 }
147190
148- virtual std::string sql () override ;
191+ virtual std::string sql () override {
192+ std::string ret = " " ;
193+ for (auto item : m_conditions) {
194+ ret += item->sql ();
195+ }
196+ return ret;
197+ }
149198
150199private:
151- WsjcppSqlWhere &cond (const std::string &name, WsjcppSqlWhereConditionType comparator, const std::string &value);
200+ WsjcppSqlWhere<T> &cond (const std::string &name, WsjcppSqlWhereConditionType comparator, const std::string &value) {
201+ if (
202+ m_conditions.size () > 0
203+ && m_conditions[m_conditions.size ()-1 ]->type () == WsjcppSqlWhereType::CONDITION
204+ ) {
205+ and_ (); // default add and_
206+ }
207+ m_conditions.push_back (std::make_shared<WsjcppSqlWhereCondition>(name, comparator, value));
208+ return *this ;
209+ }
152210
153211 WsjcppSqlBuilder2 *m_builder;
154- // T *m_query;
155- WsjcppSqlSelect *m_query;
212+ T *m_query;
156213 std::vector<std::shared_ptr<WsjcppSqlWhereBase>> m_conditions;
157214};
158215
@@ -161,7 +218,7 @@ class WsjcppSqlSelect : public WsjcppSqlQuery {
161218 WsjcppSqlSelect (const std::string &tableName, WsjcppSqlBuilder2 *builder);
162219 WsjcppSqlSelect &colum (const std::string &col, const std::string &col_as = " " );
163220
164- WsjcppSqlWhere &where ();
221+ WsjcppSqlWhere<WsjcppSqlSelect> &where ();
165222 // TODO group by
166223 // TODO order by
167224 WsjcppSqlBuilder2 &compile ();
@@ -170,7 +227,7 @@ class WsjcppSqlSelect : public WsjcppSqlQuery {
170227private:
171228 std::string m_tableName;
172229 WsjcppSqlBuilder2 *m_builder;
173- std::shared_ptr<WsjcppSqlWhere> m_where;
230+ std::shared_ptr<WsjcppSqlWhere<WsjcppSqlSelect> > m_where;
174231 std::vector<std::string> m_columns;
175232 std::map<std::string, std::string> m_columns_as;
176233};
@@ -190,6 +247,7 @@ class WsjcppSqlBuilder2 {
190247
191248protected:
192249 friend WsjcppSqlSelect;
250+ friend WsjcppSqlWhere<WsjcppSqlSelect>;
193251 void addError (const std::string &err);
194252
195253private:
0 commit comments