Skip to content

Commit dc7a0c9

Browse files
committed
Removed old builder
1 parent 303bf83 commit dc7a0c9

File tree

5 files changed

+58
-305
lines changed

5 files changed

+58
-305
lines changed

src/main.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
#include <wsjcpp_sql_builder.h>
33

44
int main(int argc, const char* argv[]) {
5-
WsjcppSqlBuilderInsert sql("TABLE_NAME");
6-
sql.add("COL1", "val1"); // will be escaped
7-
sql.add("COL2", 1);
8-
// sql.add("COL3", 1.1);
9-
if (!sql.isValid()) {
10-
std::cerr << "Something wrong with query: " << sql.getErrorMessage() << std::endl;
11-
return -1;
12-
}
5+
// WsjcppSqlBuilderInsert sql("TABLE_NAME");
6+
// sql.add("COL1", "val1"); // will be escaped
7+
// sql.add("COL2", 1);
8+
// // sql.add("COL3", 1.1);
9+
// if (!sql.isValid()) {
10+
// std::cerr << "Something wrong with query: " << sql.getErrorMessage() << std::endl;
11+
// return -1;
12+
// }
1313

14-
std::string expectedIns = "INSERT INTO TABLE_NAME(COL1, COL2) VALUES ('val1', 1);";
15-
if (expectedIns != sql.getTextQuery()) {
16-
std::cerr << "Expeceted sql: " << expectedIns << ", but got " << sql.getTextQuery() << std::endl;
17-
return -1;
18-
}
19-
std::cout << sql.getTextQuery() << std::endl;
14+
// std::string expectedIns = "INSERT INTO TABLE_NAME(COL1, COL2) VALUES ('val1', 1);";
15+
// if (expectedIns != sql.getTextQuery()) {
16+
// std::cerr << "Expeceted sql: " << expectedIns << ", but got " << sql.getTextQuery() << std::endl;
17+
// return -1;
18+
// }
19+
// std::cout << sql.getTextQuery() << std::endl;
2020

2121
return 0;
2222
}

src/tests/test_insert.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,23 @@
2929
#include <wsjcpp_sql_builder.h>
3030

3131
int main() {
32-
WsjcppSqlBuilderInsert sql("TABLE_NAME");
33-
sql.add("COL1", "val1"); // will be escaped
34-
sql.add("COL2", 1);
35-
// sql.add("COL3", 1.1);
36-
if (!sql.isValid()) {
37-
std::cerr << "Something wrong with query: " << sql.getErrorMessage() << std::endl;
38-
return -1;
39-
}
40-
if (sql.getTextQuery() != "INSERT INTO TABLE_NAME(COL1, COL2) VALUES ('val1', 1);") {
32+
WsjcppSqlBuilder2 builder;
33+
builder.insertInto("table1");
34+
35+
if (builder.hasErrors()) {
36+
std::cerr << "Select builder has some errors" << std::endl;
4137
return -1;
4238
}
39+
// std::string sqlQuery = builder.sql();
40+
// std::string sqlQueryExpected = "INSERT INTO TABLE_NAME(COL1, COL2) VALUES ('val1', 1);";
41+
// if (sqlQuery != sqlQueryExpected) {
42+
// std::cerr
43+
// << "Expected:" << std::endl
44+
// << " " << sqlQueryExpected << std::endl
45+
// << ", but got:" << std::endl
46+
// << " " << sqlQuery << std::endl
47+
// ;
48+
// return -1;
49+
// }
4350
return 0;
4451
}

src/tests/test_select.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ int main() {
5050
.endWhere() // need only for groupBy havingBy and etc
5151
;
5252
if (builder.hasErrors()) {
53+
std::cerr << "Select builder has some errors" << std::endl;
5354
return -1;
5455
}
5556
std::string sqlQuery = builder.sql();

src/wsjcpp_sql_builder.cpp

Lines changed: 17 additions & 235 deletions
Original file line numberDiff line numberDiff line change
@@ -62,236 +62,20 @@ std::string WsjcppSqlBuilderHelpers::escapingStringValue(const std::string &sVal
6262
return sResult;
6363
}
6464

65-
// ---------------------------------------------------------------------
66-
// WsjcppSqlQuery
67-
68-
WsjcppSqlQuery::WsjcppSqlQuery(WsjcppSqlBuilderType nSqlType,
69-
const std::string &sSqlTable) {
70-
m_nSqlType = nSqlType;
71-
m_sSqlTable = sSqlTable;
72-
m_bValid = true;
73-
if (m_nSqlType == WsjcppSqlBuilderType::SELECT) {
74-
m_sSqlQuery0 = "SELECT ";
75-
m_sSqlQuery1 = " FROM " + sSqlTable;
76-
m_sSqlQuery2 = "";
77-
} else if (m_nSqlType == WsjcppSqlBuilderType::INSERT) {
78-
m_sSqlQuery0 = "INSERT INTO " + sSqlTable + "(";
79-
m_sSqlQuery1 = ") VALUES (";
80-
m_sSqlQuery2 = ");";
81-
} else if (m_nSqlType == WsjcppSqlBuilderType::UPDATE) {
82-
m_sSqlQuery0 = "UPDATE " + sSqlTable + " SET ";
83-
m_sSqlQuery1 = " WHERE ";
84-
} else {
85-
m_sErrorMessage = "Unknown sql type";
86-
m_bValid = false;
87-
}
88-
}
8965

90-
bool WsjcppSqlQuery::sel(const std::string &sColumnName) {
91-
if (!checkName(sColumnName)) {
92-
return false;
93-
}
94-
m_sSqlQuery0 += sColumnName + ", ";
95-
return true;
96-
}
66+
WsjcppSqlQuery::WsjcppSqlQuery(WsjcppSqlBuilderType sqlType, const std::string &tableName)
67+
: m_sqlType(sqlType), m_tableName(tableName) {
9768

98-
bool WsjcppSqlQuery::add(const std::string &sColumnName,
99-
const std::string &sValue) {
100-
if (!checkName(sColumnName)) {
101-
return false;
102-
}
103-
if (m_nSqlType == WsjcppSqlBuilderType::SELECT) {
104-
m_sErrorMessage = "For select you could not use 'add'";
105-
m_bValid = false;
106-
} else if (m_nSqlType == WsjcppSqlBuilderType::INSERT) {
107-
m_sSqlQuery0 += sColumnName + ", ";
108-
m_sSqlQuery1 += WsjcppSqlBuilderHelpers::escapingStringValue(sValue) + ", ";
109-
} else if (m_nSqlType == WsjcppSqlBuilderType::UPDATE) {
110-
m_sSqlQuery0 += sColumnName + " = " + WsjcppSqlBuilderHelpers::escapingStringValue(sValue);
111-
} else {
112-
m_sErrorMessage = "Unknown sql type";
113-
m_bValid = false;
114-
}
115-
116-
// m_sSqlQuery = sBefore + sEscapedValue + sAfter;
117-
118-
// while (true) {
119-
// /* Locate the substring to replace. */
120-
// index = str.find(sName, index);
121-
// if (index == std::string::npos)
122-
// return false;
123-
124-
// /* Make the replacement. */
125-
// str.replace(index, 3, "def");
126-
127-
// /* Advance index forward so the next iteration doesn't pick it up as
128-
// well. */ index += 3;
129-
// }
130-
return true;
13169
}
13270

133-
bool WsjcppSqlQuery::add(const std::string &sColumnName, int nValue) {
134-
if (!checkName(sColumnName)) {
135-
return false;
136-
}
137-
138-
if (m_nSqlType == WsjcppSqlBuilderType::SELECT) {
139-
m_sErrorMessage = "For select you could not use 'add'";
140-
m_bValid = false;
141-
} else if (m_nSqlType == WsjcppSqlBuilderType::INSERT) {
142-
m_sSqlQuery0 += sColumnName + ", ";
143-
m_sSqlQuery1 += std::to_string(nValue) + ", ";
144-
} else if (m_nSqlType == WsjcppSqlBuilderType::UPDATE) {
145-
m_sSqlQuery0 += sColumnName + " = " + std::to_string(nValue);
146-
}
147-
148-
return true;
71+
WsjcppSqlBuilderType WsjcppSqlQuery::sqlType() {
72+
return m_sqlType;
14973
}
15074

151-
bool WsjcppSqlQuery::add(const std::string &sColumnName, long nValue) {
152-
if (!checkName(sColumnName)) {
153-
return false;
154-
}
155-
156-
if (m_nSqlType == WsjcppSqlBuilderType::SELECT) {
157-
m_sErrorMessage = "For select you could not use 'add'";
158-
m_bValid = false;
159-
} else if (m_nSqlType == WsjcppSqlBuilderType::INSERT) {
160-
m_sSqlQuery0 += sColumnName + ", ";
161-
m_sSqlQuery1 += std::to_string(nValue) + ", ";
162-
} else if (m_nSqlType == WsjcppSqlBuilderType::UPDATE) {
163-
m_sSqlQuery0 += sColumnName + " = " + std::to_string(nValue);
164-
}
165-
166-
return true;
167-
}
168-
169-
bool WsjcppSqlQuery::where(const std::string &sColumnName,
170-
const std::string &sValue) {
171-
if (!checkName(sColumnName)) {
172-
return false;
173-
}
174-
if (m_nSqlType == WsjcppSqlBuilderType::SELECT) {
175-
m_sSqlQuery2 += sColumnName + " = " + WsjcppSqlBuilderHelpers::escapingStringValue(sValue);
176-
} else if (m_nSqlType == WsjcppSqlBuilderType::INSERT) {
177-
m_sErrorMessage = "where can be in insert";
178-
return false;
179-
} else if (m_nSqlType == WsjcppSqlBuilderType::UPDATE) {
180-
m_sSqlQuery1 += sColumnName + " = " + WsjcppSqlBuilderHelpers::escapingStringValue(sValue);
181-
}
182-
183-
return true;
184-
}
185-
186-
bool WsjcppSqlQuery::where(const std::string &sColumnName, int nValue) {
187-
if (!checkName(sColumnName)) {
188-
return false;
189-
}
190-
if (m_nSqlType == WsjcppSqlBuilderType::SELECT) {
191-
m_sSqlQuery2 += sColumnName + " = " + std::to_string(nValue);
192-
} else if (m_nSqlType == WsjcppSqlBuilderType::INSERT) {
193-
m_sErrorMessage = "where can be in insert";
194-
return false;
195-
} else if (m_nSqlType == WsjcppSqlBuilderType::UPDATE) {
196-
m_sSqlQuery1 += sColumnName + " = " + std::to_string(nValue);
197-
}
198-
return true;
75+
const std::string &WsjcppSqlQuery::tableName() {
76+
return m_tableName;
19977
}
20078

201-
bool WsjcppSqlQuery::where(const std::string &sColumnName, long nValue) {
202-
if (!checkName(sColumnName)) {
203-
return false;
204-
}
205-
if (m_nSqlType == WsjcppSqlBuilderType::SELECT) {
206-
m_sSqlQuery2 += sColumnName + " = " + std::to_string(nValue);
207-
} else if (m_nSqlType == WsjcppSqlBuilderType::INSERT) {
208-
m_sErrorMessage = "where can be in insert";
209-
return false;
210-
} else if (m_nSqlType == WsjcppSqlBuilderType::UPDATE) {
211-
m_sSqlQuery1 += sColumnName + " = " + std::to_string(nValue);
212-
}
213-
return true;
214-
}
215-
216-
std::string WsjcppSqlQuery::getTextQuery() {
217-
std::string sSqlQuery = "";
218-
size_t size0 = m_sSqlQuery0.size();
219-
size_t size1 = m_sSqlQuery1.size();
220-
size_t size2 = m_sSqlQuery2.size();
221-
if (m_nSqlType == WsjcppSqlBuilderType::SELECT) {
222-
// TODO refactor this to vector and join
223-
sSqlQuery = m_sSqlQuery0;
224-
if (size0 > 2 && m_sSqlQuery0[size0 - 1] == ' ' &&
225-
m_sSqlQuery0[size0 - 2] == ',') {
226-
sSqlQuery += m_sSqlQuery0.substr(0, size0 - 2);
227-
}
228-
sSqlQuery += m_sSqlQuery1;
229-
if (size2 > 2 && m_sSqlQuery2[size2 - 1] == ' ' &&
230-
m_sSqlQuery2[size2 - 2] == ',') {
231-
sSqlQuery += m_sSqlQuery2.substr(0, size2 - 2);
232-
}
233-
} else if (m_nSqlType == WsjcppSqlBuilderType::INSERT) {
234-
if (size0 > 2 && m_sSqlQuery0[size0 - 1] == ' ' &&
235-
m_sSqlQuery0[size0 - 2] == ',') {
236-
sSqlQuery += m_sSqlQuery0.substr(0, size0 - 2);
237-
}
238-
if (size1 > 2 && m_sSqlQuery1[size1 - 1] == ' ' &&
239-
m_sSqlQuery1[size1 - 2] == ',') {
240-
sSqlQuery += m_sSqlQuery1.substr(0, size1 - 2);
241-
}
242-
sSqlQuery += m_sSqlQuery2;
243-
} else if (m_nSqlType == WsjcppSqlBuilderType::UPDATE) {
244-
sSqlQuery = m_sSqlQuery0 + m_sSqlQuery1 + m_sSqlQuery2;
245-
}
246-
return sSqlQuery;
247-
}
248-
249-
bool WsjcppSqlQuery::isValid() { return m_bValid; }
250-
251-
std::string WsjcppSqlQuery::getErrorMessage() { return m_sErrorMessage; }
252-
253-
bool WsjcppSqlQuery::checkName(const std::string &sColumnName) {
254-
if (sColumnName.size() < 2) {
255-
m_sErrorMessage =
256-
"Parameter '" + sColumnName + "' must more than 2 characters";
257-
m_bValid = false;
258-
return false;
259-
}
260-
// TODO check alphabet
261-
262-
// if (sName[0] != ':') {
263-
// m_sErrorMessage = "Parameter '" + sName + "' must starts with ':'";
264-
// m_bValid = false;
265-
// return false;
266-
// }
267-
// nIndex = m_sSqlQuery.find(sName, 0);
268-
// if (nIndex == std::string::npos) {
269-
// m_sErrorMessage = "Not found '" + sName + "' in " + m_sSqlQuery;
270-
// m_bValid = false;
271-
// return false;
272-
// }
273-
return true;
274-
}
275-
276-
// ---------------------------------------------------------------------
277-
// WsjcppSqlBuilderSelect
278-
279-
WsjcppSqlBuilderSelect::WsjcppSqlBuilderSelect(const std::string &sSqlTable)
280-
: WsjcppSqlQuery(WsjcppSqlBuilderType::SELECT, sSqlTable) {}
281-
282-
// ---------------------------------------------------------------------
283-
// WsjcppSqlBuilderInsert
284-
285-
WsjcppSqlBuilderInsert::WsjcppSqlBuilderInsert(const std::string &sSqlTable)
286-
: WsjcppSqlQuery(WsjcppSqlBuilderType::INSERT, sSqlTable) {}
287-
288-
// ---------------------------------------------------------------------
289-
// WsjcppSqlBuilderUpdate
290-
291-
WsjcppSqlBuilderUpdate::WsjcppSqlBuilderUpdate(const std::string &sSqlTable)
292-
: WsjcppSqlQuery(WsjcppSqlBuilderType::UPDATE, sSqlTable) {}
293-
294-
29579
// ---------------------------------------------------------------------
29680
// WsjcppSqlWhereBase
29781

@@ -390,8 +174,6 @@ std::string WsjcppSqlWhereCondition::sql() {
390174
return ret;
391175
}
392176

393-
394-
395177
// ---------------------------------------------------------------------
396178
// WsjcppSqlBuilderUpdate
397179

@@ -468,23 +250,23 @@ WsjcppSqlSelect &WsjcppSqlBuilder2::selectFrom(const std::string &tableName) {
468250
return *(WsjcppSqlSelect *)(m_queries[m_queries.size() -1].get());
469251
}
470252

471-
WsjcppSqlBuilder2 &WsjcppSqlBuilder2::makeInsert(const std::string &tableName) {
253+
WsjcppSqlBuilder2 &WsjcppSqlBuilder2::insertInto(const std::string &tableName) {
472254
m_tableName = tableName;
473255
m_nSqlType = WsjcppSqlBuilderType::INSERT;
474256
return *this;
475257
}
476258

477-
WsjcppSqlBuilder2 &WsjcppSqlBuilder2::makeUpdate(const std::string &tableName) {
478-
m_tableName = tableName;
479-
m_nSqlType = WsjcppSqlBuilderType::UPDATE;
480-
return *this;
481-
}
259+
// WsjcppSqlBuilder2 &WsjcppSqlBuilder2::makeUpdate(const std::string &tableName) {
260+
// m_tableName = tableName;
261+
// m_nSqlType = WsjcppSqlBuilderType::UPDATE;
262+
// return *this;
263+
// }
482264

483-
WsjcppSqlBuilder2 &WsjcppSqlBuilder2::makeDelete(const std::string &tableName) {
484-
m_tableName = tableName;
485-
m_nSqlType = WsjcppSqlBuilderType::DELETE;
486-
return *this;
487-
}
265+
// WsjcppSqlBuilder2 &WsjcppSqlBuilder2::makeDelete(const std::string &tableName) {
266+
// m_tableName = tableName;
267+
// m_nSqlType = WsjcppSqlBuilderType::DELETE;
268+
// return *this;
269+
// }
488270

489271
bool WsjcppSqlBuilder2::hasErrors() {
490272
return m_errors.size() > 0;

0 commit comments

Comments
 (0)