@@ -205,7 +205,6 @@ WsjcppSqlWhere<WsjcppSqlSelect> &WsjcppSqlSelect::where() {
205205 if (!m_where) {
206206 m_where = std::make_shared<WsjcppSqlWhere<WsjcppSqlSelect>>(nullptr , builderRawPtr (), this );
207207 }
208-
209208 return *(m_where.get ());
210209}
211210
@@ -314,6 +313,70 @@ std::string WsjcppSqlInsert::sql() {
314313 return ret;
315314};
316315
316+ // ---------------------------------------------------------------------
317+ // WsjcppSqlUpdate
318+
319+ WsjcppSqlUpdate::WsjcppSqlUpdate (const std::string &tableName, WsjcppSqlBuilder *builder)
320+ : WsjcppSqlQuery(WsjcppSqlQueryType::INSERT, builder, tableName) {
321+
322+ }
323+
324+ WsjcppSqlUpdate &WsjcppSqlUpdate::set (const std::string &name, const std::string &val) {
325+ return setValue (name, WsjcppSqlBuilderHelpers::escapingStringValue (val));
326+ }
327+
328+ WsjcppSqlUpdate &WsjcppSqlUpdate::set (const std::string &name, int val) {
329+ return setValue (name, std::to_string (val));
330+ }
331+
332+ WsjcppSqlUpdate &WsjcppSqlUpdate::set (const std::string &name, float val) {
333+ return setValue (name, std::to_string (val));
334+ }
335+
336+ WsjcppSqlUpdate &WsjcppSqlUpdate::set (const std::string &name, double val) {
337+ return setValue (name, std::to_string (val));
338+ }
339+
340+ WsjcppSqlUpdate &WsjcppSqlUpdate::setValue (const std::string &name, const std::string &val) {
341+ auto it = std::find (m_columns.begin (), m_columns.end (), name);
342+ if (it != m_columns.end ()) {
343+ m_values[name] = val;
344+ // builder().addError("Column '" + name + "' already added to select");
345+ } else {
346+ m_columns.push_back (name);
347+ m_values[name] = val;
348+ }
349+ return *this ;
350+ }
351+
352+ WsjcppSqlWhere<WsjcppSqlUpdate> &WsjcppSqlUpdate::where () {
353+ if (!m_where) {
354+ m_where = std::make_shared<WsjcppSqlWhere<WsjcppSqlUpdate>>(nullptr , builderRawPtr (), this );
355+ }
356+ return *(m_where.get ());
357+ }
358+
359+ std::string WsjcppSqlUpdate::sql () {
360+ std::string ret = " UPDATE " + tableName () + " SET " ;
361+
362+ // TODO if columns is empty
363+ bool first = true ;
364+ for (auto col : m_columns) {
365+ if (!first) {
366+ ret += " , " ;
367+ }
368+ ret += col + " = " + m_values[col];
369+ first = false ;
370+ }
371+
372+ if (m_where) {
373+ ret += " WHERE " + m_where->sql ();
374+ }
375+
376+ return ret;
377+ };
378+
379+
317380// ---------------------------------------------------------------------
318381// WsjcppSqlBuilder
319382
@@ -337,11 +400,19 @@ WsjcppSqlInsert &WsjcppSqlBuilder::findInsertOrCreate(const std::string &tableNa
337400 return insertInto (tableName);
338401}
339402
340- // WsjcppSqlBuilder &WsjcppSqlBuilder::makeUpdate(const std::string &tableName) {
341- // m_tableName = tableName;
342- // m_nSqlType = WsjcppSqlQueryType::UPDATE;
343- // return *this;
344- // }
403+ WsjcppSqlUpdate &WsjcppSqlBuilder::update (const std::string &tableName) {
404+ m_queries.push_back (std::make_shared<WsjcppSqlUpdate>(tableName, this ));
405+ return *(WsjcppSqlUpdate *)(m_queries[m_queries.size () -1 ].get ());
406+ }
407+
408+ WsjcppSqlUpdate &WsjcppSqlBuilder::findUpdateOrCreate (const std::string &tableName) {
409+ for (auto query : m_queries) {
410+ if (query->sqlType () == WsjcppSqlQueryType::UPDATE && query->tableName () == tableName) {
411+ return *(WsjcppSqlUpdate *)(query.get ());
412+ }
413+ }
414+ return update (tableName);
415+ }
345416
346417// WsjcppSqlBuilder &WsjcppSqlBuilder::makeDelete(const std::string &tableName) {
347418// m_tableName = tableName;
0 commit comments