Skip to content

Commit aee4fda

Browse files
committed
Implemented subCondition
1 parent d7b4414 commit aee4fda

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

src/tests/test_select.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,29 @@ int main() {
3434
.colum("col1")
3535
.colum("col2", "c3")
3636
.colum("col3")
37+
.colum("col4")
3738
.where()
3839
.equal("col1", "1")
3940
.or_()
4041
.notEqual("col2", "2")
41-
.endWhere()
42-
// .groupBy()
42+
.or_()
43+
.subCondition()
44+
.equal("c3", "4")
45+
// .and_() // be default must be added and
46+
.equal("col2", "5")
47+
.finishSubCondition()
48+
.or_()
49+
.lessThen("col4", "...")
50+
.endWhere() // need only for groupBy havingBy and etc
4351
;
4452
if (builder.hasErrors()) {
4553
return -1;
4654
}
4755
std::string sqlQuery = builder.sql();
48-
std::string sqlQueryExpected = "SELECT col1, col2 AS c3, col3 FROM table1 WHERE col1 = \"1\" OR col2 <> \"2\"";
56+
std::string sqlQueryExpected =
57+
"SELECT col1, col2 AS c3, col3, col4 "
58+
"FROM table1 "
59+
"WHERE col1 = \"1\" OR col2 <> \"2\" OR (c3 = \"4\" AND col2 = \"5\") OR col4 < \"...\"";
4960
if (sqlQuery != sqlQueryExpected) {
5061
std::cerr
5162
<< "Expected:" << std::endl

src/wsjcpp_sql_builder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ WsjcppSqlSelect &WsjcppSqlSelect::colum(const std::string &col, const std::strin
387387

388388
WsjcppSqlWhere<WsjcppSqlSelect> &WsjcppSqlSelect::where() {
389389
if (!m_where) {
390-
m_where = std::make_shared<WsjcppSqlWhere<WsjcppSqlSelect>>(m_builder, this);
390+
m_where = std::make_shared<WsjcppSqlWhere<WsjcppSqlSelect>>(nullptr, m_builder, this);
391391
}
392392

393393
return *(m_where.get());

src/wsjcpp_sql_builder.h

Lines changed: 31 additions & 3 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 { LOGICAL_OPERATOR, CONDITION, SUB };
89+
enum class WsjcppSqlWhereType { LOGICAL_OPERATOR, CONDITION, SUB_CONDITION };
9090

9191
class WsjcppSqlWhereBase {
9292
public:
@@ -130,7 +130,8 @@ class WsjcppSqlSelect;
130130
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) { }
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> &notEqual(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

Comments
 (0)