Skip to content

Commit d7b4414

Browse files
committed
redesign where to template (because it's will be reused for DELETE and for UPDATE queries)
1 parent 56b738b commit d7b4414

File tree

3 files changed

+83
-107
lines changed

3 files changed

+83
-107
lines changed

src/tests/test_select.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ int main() {
3838
.equal("col1", "1")
3939
.or_()
4040
.notEqual("col2", "2")
41-
.endWhere()
41+
.endWhere()
4242
// .groupBy()
4343
;
4444
if (builder.hasErrors()) {

src/wsjcpp_sql_builder.cpp

Lines changed: 4 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,13 @@ WsjcppSqlWhereType WsjcppSqlWhereBase::type() {
302302
// ---------------------------------------------------------------------
303303
// WsjcppSqlWhereOr
304304

305-
WsjcppSqlWhereOr::WsjcppSqlWhereOr() : WsjcppSqlWhereBase(WsjcppSqlWhereType::OR) { }
305+
WsjcppSqlWhereOr::WsjcppSqlWhereOr() : WsjcppSqlWhereBase(WsjcppSqlWhereType::LOGICAL_OPERATOR) { }
306306
std::string WsjcppSqlWhereOr::sql() { return " OR "; };
307307

308308
// ---------------------------------------------------------------------
309309
// WsjcppSqlWhereAnd
310310

311-
WsjcppSqlWhereAnd::WsjcppSqlWhereAnd() : WsjcppSqlWhereBase(WsjcppSqlWhereType::AND) { }
311+
WsjcppSqlWhereAnd::WsjcppSqlWhereAnd() : WsjcppSqlWhereBase(WsjcppSqlWhereType::LOGICAL_OPERATOR) { }
312312
std::string WsjcppSqlWhereAnd::sql() { return " AND "; };
313313

314314
// ---------------------------------------------------------------------
@@ -362,87 +362,7 @@ std::string WsjcppSqlWhereCondition::sql() {
362362
return ret;
363363
}
364364

365-
// ---------------------------------------------------------------------
366-
// WsjcppSqlWhere
367-
368-
WsjcppSqlWhere &WsjcppSqlWhere::cond(const std::string &name, WsjcppSqlWhereConditionType comparator, const std::string &value) {
369-
if (
370-
m_conditions.size() > 0
371-
&& m_conditions[m_conditions.size()-1]->type() == WsjcppSqlWhereType::CONDITION
372-
) {
373-
and_(); // default add and_
374-
}
375-
376-
m_conditions.push_back(std::make_shared<WsjcppSqlWhereCondition>(name, comparator, value));
377-
return *this;
378-
}
379-
380-
WsjcppSqlWhere &WsjcppSqlWhere::notEqual(const std::string &name, const std::string &value) {
381-
cond(name, WsjcppSqlWhereConditionType::NOT_EQUAL, value);
382-
return *this;
383-
}
384-
385-
WsjcppSqlWhere &WsjcppSqlWhere::equal(const std::string &name, const std::string &value) {
386-
cond(name, WsjcppSqlWhereConditionType::EQUAL, value);
387-
return *this;
388-
}
389-
390-
WsjcppSqlWhere &WsjcppSqlWhere::moreThen(const std::string &name, const std::string &value) {
391-
cond(name, WsjcppSqlWhereConditionType::MORE_THEN, value);
392-
return *this;
393-
}
394-
395-
WsjcppSqlWhere &WsjcppSqlWhere::lessThen(const std::string &name, const std::string &value) {
396-
cond(name, WsjcppSqlWhereConditionType::LESS_THEN, value);
397-
return *this;
398-
}
399365

400-
WsjcppSqlWhere &WsjcppSqlWhere::like(const std::string &name, const std::string &value) {
401-
cond(name, WsjcppSqlWhereConditionType::LIKE, value);
402-
return *this;
403-
}
404-
405-
406-
WsjcppSqlWhere &WsjcppSqlWhere::or_() {
407-
if (
408-
m_conditions.size() > 0
409-
&& (
410-
m_conditions[m_conditions.size()-1]->type() == WsjcppSqlWhereType::OR
411-
|| m_conditions[m_conditions.size()-1]->type() == WsjcppSqlWhereType::AND
412-
)
413-
) {
414-
// TODO add to builder errors;
415-
// std::cerr << "[WARNING] WsjcppSqlWhere. Last item alredy defined 'or' or 'and'. current will be skipped." << std::endl;
416-
return *this;
417-
}
418-
419-
m_conditions.push_back(std::make_shared<WsjcppSqlWhereOr>());
420-
return *this;
421-
}
422-
423-
WsjcppSqlWhere &WsjcppSqlWhere::and_() {
424-
if (
425-
m_conditions.size() > 0
426-
&& (
427-
m_conditions[m_conditions.size()-1]->type() == WsjcppSqlWhereType::OR
428-
|| m_conditions[m_conditions.size()-1]->type() == WsjcppSqlWhereType::AND
429-
)
430-
) {
431-
// TODO add to builder errors;
432-
// std::cerr << "[WARNING] WsjcppSqlWhere. Last item alredy defined 'or' or 'and'. current will be skipped." << std::endl;
433-
return *this;
434-
}
435-
m_conditions.push_back(std::make_shared<WsjcppSqlWhereAnd>());
436-
return *this;
437-
}
438-
439-
std::string WsjcppSqlWhere::sql() {
440-
std::string ret = "";
441-
for (auto item : m_conditions) {
442-
ret += item->sql();
443-
}
444-
return ret;
445-
};
446366

447367
// ---------------------------------------------------------------------
448368
// WsjcppSqlBuilderUpdate
@@ -465,11 +385,9 @@ WsjcppSqlSelect &WsjcppSqlSelect::colum(const std::string &col, const std::strin
465385
return *this;
466386
}
467387

468-
// WsjcppSqlWhere<WsjcppSqlSelect> &WsjcppSqlSelect::where() {
469-
WsjcppSqlWhere &WsjcppSqlSelect::where() {
388+
WsjcppSqlWhere<WsjcppSqlSelect> &WsjcppSqlSelect::where() {
470389
if (!m_where) {
471-
// m_where = std::make_shared<WsjcppSqlWhere<WsjcppSqlSelect>>();
472-
m_where = std::make_shared<WsjcppSqlWhere>(m_builder, this);
390+
m_where = std::make_shared<WsjcppSqlWhere<WsjcppSqlSelect>>(m_builder, this);
473391
}
474392

475393
return *(m_where.get());

src/wsjcpp_sql_builder.h

Lines changed: 78 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class WsjcppSqlBuilderUpdate : public WsjcppSqlQuery {
8686
class WsjcppSqlBuilder2;
8787

8888

89-
enum class WsjcppSqlWhereType { OR, AND, CONDITION, SUB };
89+
enum class WsjcppSqlWhereType { LOGICAL_OPERATOR, CONDITION, SUB };
9090

9191
class WsjcppSqlWhereBase {
9292
public:
@@ -127,32 +127,89 @@ class WsjcppSqlWhereCondition : public WsjcppSqlWhereBase {
127127

128128
class WsjcppSqlSelect;
129129

130-
// template<class T>
130+
template<class T>
131131
class WsjcppSqlWhere : public WsjcppSqlWhereBase {
132132
public:
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 &notEqual(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> &notEqual(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

150199
private:
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 {
170227
private:
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

191248
protected:
192249
friend WsjcppSqlSelect;
250+
friend WsjcppSqlWhere<WsjcppSqlSelect>;
193251
void addError(const std::string &err);
194252

195253
private:

0 commit comments

Comments
 (0)