EasyQtSql
Easy SQL data access helper for QtSql
EasyQtSql_UpdateQuery.h
Go to the documentation of this file.
1 #ifndef EASYQTSQL_UPDATEQUERY_H
2 #define EASYQTSQL_UPDATEQUERY_H
3 
4 /*
5  * The MIT License (MIT)
6  * Copyright 2018 Alexey Kramin
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27 */
28 
29 #ifndef EASY_QT_SQL_MAIN
30 
31 #include <QtSql>
33 
34 #endif
35 
40 {
41 public:
42  UpdateQuery(const QString &table, const QSqlDatabase &db)
43  : q(db)
44  , m_table(table)
45  { }
46 
63  UpdateQuery &set(const QString &field, const QVariant &value)
64  {
65  m_updateMap[field] = value;
66 
67  return *this;
68  }
69 
83  UpdateQuery &set(const QVariantMap &map)
84  {
85  for (auto it = map.begin(); it != map.end(); ++it)
86  {
87  m_updateMap[it.key()] = it.value();
88  }
89 
90  return *this;
91  }
92 
104  NonQueryResult where(const QString &expr)
105  {
106  m_whereExpr = expr;
107  return exec();
108  }
109 
124  NonQueryResult where(const QString &expr, const QVariant &last)
125  {
126  m_params.append(last);
127  m_whereExpr = expr;
128  return exec();
129  }
130 
131  template <typename... Rest> NonQueryResult where(const QString &expr, const QVariant &first, const Rest&... rest)
132  {
133  m_params.append(first);
134  return where(expr, rest...);
135  }
136 
141  {
142  QString sql = "UPDATE " + m_table + " SET ";
143  QStringList fieldValuePairs;
144 
145  for (auto it = m_updateMap.begin(); it != m_updateMap.end(); ++it)
146  {
147  fieldValuePairs.append(it.key() + "=?");
148  }
149 
150  sql += fieldValuePairs.join(",");
151 
152  if (!m_whereExpr.isEmpty())
153  {
154  sql += " WHERE " + m_whereExpr;
155  }
156 
157  q.prepare(sql);
158 
159  for (auto it = m_updateMap.begin(); it != m_updateMap.end(); ++it)
160  {
161  q.addBindValue(it.value());
162  }
163 
164  for (auto it = m_params.begin(); it != m_params.end(); ++it)
165  {
166  q.addBindValue(*it);
167  }
168 
169  bool res = q.exec();
170 
171 #ifdef DB_EXCEPTIONS_ENABLED
172 
173  if (!res)
174  throw DBException(q);
175 
176 #endif
177 
178  return NonQueryResult(q);
179  }
180 
181 private:
182  QSqlQuery q;
183  QString m_table;
184  QVariantMap m_updateMap;
185  QVariantList m_params;
186  QString m_whereExpr;
187 };
188 
189 #endif // EASYQTSQL_UPDATEQUERY_H
NonQueryResult where(const QString &expr)
Executes update query with expr condition.
Definition: EasyQtSql_UpdateQuery.h:104
UpdateQuery(const QString &table, const QSqlDatabase &db)
Definition: EasyQtSql_UpdateQuery.h:42
NonQueryResult exec()
Executes UPDATE query without conditions.
Definition: EasyQtSql_UpdateQuery.h:140
NonQueryResult where(const QString &expr, const QVariant &first, const Rest &... rest)
Definition: EasyQtSql_UpdateQuery.h:131
QSqlQuery wrapper for non-select query results reading.
Definition: EasyQtSql_NonQueryResult.h:38
NonQueryResult where(const QString &expr, const QVariant &last)
Executes update query with expr condition and last parameter binding.
Definition: EasyQtSql_UpdateQuery.h:124
QSqlQuery wrapper for UPDATE ... SET ... WHERE ... query execution.
Definition: EasyQtSql_UpdateQuery.h:39
Exception class for SQL errors handling.
Definition: EasyQtSql_DBException.h:38