Skip to content

Commit afebee9

Browse files
committed
Added reuse Insert with clearValues()
1 parent d1b8c55 commit afebee9

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/tests/test_insert.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,25 @@ int main() {
5353
;
5454
return -1;
5555
}
56+
57+
builder.findInsertOrCreate("table2")
58+
.clearValues()
59+
.val("val2")
60+
.val(2)
61+
.val(10.0)
62+
;
63+
64+
sqlQuery = builder.sql();
65+
sqlQueryExpected = "INSERT INTO table2(col1, col2, col3) VALUES('val2', 2, 10.000000)";
66+
if (sqlQuery != sqlQueryExpected) {
67+
std::cerr
68+
<< "Expected:" << std::endl
69+
<< " {" << sqlQueryExpected << "}" << std::endl
70+
<< ", but got:" << std::endl
71+
<< " {" << sqlQuery << "}" << std::endl
72+
;
73+
return -1;
74+
}
75+
5676
return 0;
5777
}

src/wsjcpp_sql_builder.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,11 @@ WsjcppSqlInsert &WsjcppSqlInsert::addColums(const std::vector<std::string> &cols
260260
return *this;
261261
}
262262

263+
WsjcppSqlInsert &WsjcppSqlInsert::clearValues() {
264+
m_values.clear();
265+
return *this;
266+
}
267+
263268
WsjcppSqlInsert &WsjcppSqlInsert::val(const std::string &val) {
264269
m_values.push_back(WsjcppSqlBuilderHelpers::escapingStringValue(val));
265270
return *this;
@@ -320,7 +325,16 @@ WsjcppSqlSelect &WsjcppSqlBuilder::selectFrom(const std::string &tableName) {
320325

321326
WsjcppSqlInsert &WsjcppSqlBuilder::insertInto(const std::string &tableName) {
322327
m_queries.push_back(std::make_shared<WsjcppSqlInsert>(tableName, this));
323-
return *(WsjcppSqlInsert *)(m_queries[m_queries.size() -1].get());;
328+
return *(WsjcppSqlInsert *)(m_queries[m_queries.size() -1].get());
329+
}
330+
331+
WsjcppSqlInsert &WsjcppSqlBuilder::findInsertOrCreate(const std::string &tableName) {
332+
for (auto query : m_queries) {
333+
if (query->sqlType() == WsjcppSqlQueryType::INSERT && query->tableName() == tableName) {
334+
return *(WsjcppSqlInsert *)(query.get());
335+
}
336+
}
337+
return insertInto(tableName);
324338
}
325339

326340
// WsjcppSqlBuilder &WsjcppSqlBuilder::makeUpdate(const std::string &tableName) {

src/wsjcpp_sql_builder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ class WsjcppSqlInsert : public WsjcppSqlQuery {
241241
WsjcppSqlInsert(const std::string &tableName, WsjcppSqlBuilder *builder);
242242
WsjcppSqlInsert &colum(const std::string &col);
243243
WsjcppSqlInsert &addColums(const std::vector<std::string> &cols);
244+
WsjcppSqlInsert &clearValues();
244245

245246
WsjcppSqlInsert &val(const std::string &col);
246247
WsjcppSqlInsert &val(int col);
@@ -261,6 +262,7 @@ class WsjcppSqlBuilder {
261262

262263
WsjcppSqlSelect &selectFrom(const std::string &tableName);
263264
WsjcppSqlInsert &insertInto(const std::string &tableName);
265+
WsjcppSqlInsert &findInsertOrCreate(const std::string &tableName);
264266
// WsjcppSqlBuilder &update(const std::string &sSqlTable);
265267
// WsjcppSqlBuilder &deleteFrom(const std::string &sSqlTable);
266268

0 commit comments

Comments
 (0)