EasyQtSql
Easy SQL data access helper for QtSql
EasyQtSql_DeleteQuery.h
Go to the documentation of this file.
1 #ifndef EASYQTSQL_DELETEQUERY_H
2 #define EASYQTSQL_DELETEQUERY_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  DeleteQuery(const QString &table, const QSqlDatabase &db)
43  : m_query(db)
44  , m_table(table)
45  { }
46 
55  NonQueryResult where(const QString &expr)
56  {
57  const QString &sql = createSql(m_table, expr);
58 
59  const bool res = m_query.exec(sql);
60 
61 #ifdef DB_EXCEPTIONS_ENABLED
62 
63  if (!res)
64  throw DBException(m_query);
65 
66 #endif
67 
68  return NonQueryResult(m_query);
69  }
70 
81  NonQueryResult where(const QString &expr, const QVariant &last)
82  {
83  m_params.append(last);
84 
85  const QString &sql = createSql(m_table, expr);
86 
87  m_query.prepare(sql);
88 
89  for (int i = 0; i < m_params.count(); ++i)
90  {
91  m_query.addBindValue(m_params.at(i));
92  }
93 
94  const bool res = m_query.exec();
95 
96 #ifdef DB_EXCEPTIONS_ENABLED
97 
98  if (!res)
99  throw DBException(m_query);
100 
101 #endif
102 
103  return NonQueryResult(m_query);
104  }
105 
106  template <typename... Rest> NonQueryResult where(const QString &expr, const QVariant &first, const Rest&... rest)
107  {
108  m_params.append(first);
109  return where(expr, rest...);
110  }
111 
121  {
122  const QString &sql = createSql(m_table);
123 
124  const bool res = m_query.exec(sql);
125 
126 #ifdef DB_EXCEPTIONS_ENABLED
127 
128  if (!res)
129  throw DBException(m_query);
130 
131 #endif
132 
133  return NonQueryResult(m_query);
134  }
135 
136 
137 private:
138  QSqlQuery m_query;
139  QString m_table;
140  QVariantList m_params;
141 
142  static QString createSql(const QString &table, const QString &expr = "1=1")
143  {
144  return QString("DELETE FROM %0 WHERE %1").arg(table).arg(expr);
145  }
146 
147 };
148 
149 #endif // EASYQTSQL_DELETEQUERY_H
NonQueryResult where(const QString &expr)
Executes conditional DELETE FROM table WHERE expr query.
Definition: EasyQtSql_DeleteQuery.h:55
NonQueryResult exec()
Executes unconditional DELETE FROM table query.
Definition: EasyQtSql_DeleteQuery.h:120
NonQueryResult where(const QString &expr, const QVariant &first, const Rest &... rest)
Definition: EasyQtSql_DeleteQuery.h:106
DeleteQuery(const QString &table, const QSqlDatabase &db)
Definition: EasyQtSql_DeleteQuery.h:42
QSqlQuery wrapper for non-select query results reading.
Definition: EasyQtSql_NonQueryResult.h:38
NonQueryResult where(const QString &expr, const QVariant &last)
Executes conditional DELETE FROM table WHERE expr query with parameter binding.
Definition: EasyQtSql_DeleteQuery.h:81
QSqlQuery wrapper for DELETE FROM ... WHERE .. query execution.
Definition: EasyQtSql_DeleteQuery.h:39
Exception class for SQL errors handling.
Definition: EasyQtSql_DBException.h:38