Skip to content

Commit d1b8c55

Browse files
committed
Implemented first insert version
1 parent dfd5d03 commit d1b8c55

File tree

3 files changed

+85
-24
lines changed

3 files changed

+85
-24
lines changed

src/tests/test_insert.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,28 @@
3030

3131
int main() {
3232
WsjcppSqlBuilder builder;
33-
builder.insertInto("table1");
33+
builder.insertInto("table2")
34+
.colum("col1")
35+
.addColums({"col2", "col3"})
36+
.val("val1")
37+
.val(1)
38+
.val(2.0)
39+
;
3440

3541
if (builder.hasErrors()) {
3642
std::cerr << "Select builder has some errors" << std::endl;
3743
return -1;
3844
}
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-
// }
45+
std::string sqlQuery = builder.sql();
46+
std::string sqlQueryExpected = "INSERT INTO table2(col1, col2, col3) VALUES('val1', 1, 2.000000)";
47+
if (sqlQuery != sqlQueryExpected) {
48+
std::cerr
49+
<< "Expected:" << std::endl
50+
<< " {" << sqlQueryExpected << "}" << std::endl
51+
<< ", but got:" << std::endl
52+
<< " {" << sqlQuery << "}" << std::endl
53+
;
54+
return -1;
55+
}
5056
return 0;
5157
}

src/wsjcpp_sql_builder.cpp

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,28 +248,79 @@ WsjcppSqlInsert::WsjcppSqlInsert(const std::string &tableName, WsjcppSqlBuilder
248248

249249
}
250250

251+
WsjcppSqlInsert &WsjcppSqlInsert::colum(const std::string &col) {
252+
m_columns.push_back(col);
253+
return *this;
254+
}
255+
256+
WsjcppSqlInsert &WsjcppSqlInsert::addColums(const std::vector<std::string> &cols) {
257+
for (auto col : cols) {
258+
m_columns.push_back(col);
259+
}
260+
return *this;
261+
}
262+
263+
WsjcppSqlInsert &WsjcppSqlInsert::val(const std::string &val) {
264+
m_values.push_back(WsjcppSqlBuilderHelpers::escapingStringValue(val));
265+
return *this;
266+
}
267+
268+
WsjcppSqlInsert &WsjcppSqlInsert::val(int val) {
269+
m_values.push_back(std::to_string(val));
270+
return *this;
271+
}
272+
273+
WsjcppSqlInsert &WsjcppSqlInsert::val(float val) {
274+
m_values.push_back(std::to_string(val));
275+
return *this;
276+
}
277+
278+
WsjcppSqlInsert &WsjcppSqlInsert::val(double val) {
279+
m_values.push_back(std::to_string(val));
280+
return *this;
281+
}
251282

252283
std::string WsjcppSqlInsert::sql() {
253284
std::string ret = "INSERT INTO " + tableName();
254285

286+
// TODO if columns is empty
287+
ret += "(";
288+
bool first = true;
289+
for (auto col : m_columns) {
290+
if (!first) {
291+
ret += ", ";
292+
}
293+
ret += col;
294+
first = false;
295+
}
296+
ret += ")";
297+
298+
ret += " VALUES(";
299+
first = true;
300+
for (auto val : m_values) {
301+
if (!first) {
302+
ret += ", ";
303+
}
304+
ret += val;
305+
first = false;
306+
}
307+
ret += ")";
308+
255309
return ret;
256310
};
257311

258312
// ---------------------------------------------------------------------
259313
// WsjcppSqlBuilder
260314

261315
WsjcppSqlSelect &WsjcppSqlBuilder::selectFrom(const std::string &tableName) {
262-
m_tableName = tableName;
263-
m_nSqlType = WsjcppSqlQueryType::SELECT;
264-
m_queries.push_back(std::make_shared<WsjcppSqlSelect>(m_tableName, this));
316+
m_queries.push_back(std::make_shared<WsjcppSqlSelect>(tableName, this));
265317
// TODO check must be select last one;
266318
return *(WsjcppSqlSelect *)(m_queries[m_queries.size() -1].get());
267319
}
268320

269-
WsjcppSqlBuilder &WsjcppSqlBuilder::insertInto(const std::string &tableName) {
270-
m_tableName = tableName;
271-
m_nSqlType = WsjcppSqlQueryType::INSERT;
272-
return *this;
321+
WsjcppSqlInsert &WsjcppSqlBuilder::insertInto(const std::string &tableName) {
322+
m_queries.push_back(std::make_shared<WsjcppSqlInsert>(tableName, this));
323+
return *(WsjcppSqlInsert *)(m_queries[m_queries.size() -1].get());;
273324
}
274325

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

src/wsjcpp_sql_builder.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,22 +239,28 @@ class WsjcppSqlSelect : public WsjcppSqlQuery {
239239
class WsjcppSqlInsert : public WsjcppSqlQuery {
240240
public:
241241
WsjcppSqlInsert(const std::string &tableName, WsjcppSqlBuilder *builder);
242-
// WsjcppSqlInsert &colum(const std::string &col);
242+
WsjcppSqlInsert &colum(const std::string &col);
243+
WsjcppSqlInsert &addColums(const std::vector<std::string> &cols);
244+
245+
WsjcppSqlInsert &val(const std::string &col);
246+
WsjcppSqlInsert &val(int col);
247+
WsjcppSqlInsert &val(float col);
248+
WsjcppSqlInsert &val(double col);
243249

244250
WsjcppSqlBuilder &builder();
245251
virtual std::string sql() override;
246252

247253
private:
248254
std::vector<std::string> m_columns;
249-
std::map<std::string, std::string> m_columns_as;
255+
std::vector<std::string> m_values;
250256
};
251257

252258
class WsjcppSqlBuilder {
253259
public:
254260
// TODO begin / end transaction can be added here
255261

256262
WsjcppSqlSelect &selectFrom(const std::string &tableName);
257-
WsjcppSqlBuilder &insertInto(const std::string &tableName);
263+
WsjcppSqlInsert &insertInto(const std::string &tableName);
258264
// WsjcppSqlBuilder &update(const std::string &sSqlTable);
259265
// WsjcppSqlBuilder &deleteFrom(const std::string &sSqlTable);
260266

@@ -269,7 +275,5 @@ class WsjcppSqlBuilder {
269275

270276
private:
271277
std::vector<std::string> m_errors;
272-
std::string m_tableName;
273-
WsjcppSqlQueryType m_nSqlType;
274278
std::vector<std::shared_ptr<WsjcppSqlQuery>> m_queries;
275279
};

0 commit comments

Comments
 (0)