EasyQtSql
Easy SQL data access helper for QtSql
EasyQtSql_InsertQuery.h
Go to the documentation of this file.
1 #ifndef EASYQTSQL_INSERTQUERY_H
2 #define EASYQTSQL_INSERTQUERY_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  InsertQuery(const QString &table, const QSqlDatabase &db)
43  : m_table(table)
44  , q(db)
45  { }
46 
60  InsertQuery &values(const QVariant& t)
61  {
62  m_args.append(t);
63 
64  if (m_insertArray.isEmpty())
65  {
66  m_insertArray.resize(m_args.count());
67  }
68  else
69  {
70  Q_ASSERT(m_insertArray.size() == m_args.size());
71  }
72 
73  for (int i = 0; i < m_args.count(); ++i)
74  {
75  m_insertArray[i].append(m_args.at(i));
76  }
77 
78  m_args.clear();
79 
80  return *this;
81  }
82 
83  template <typename... Rest> InsertQuery &values(const QVariant &first, const Rest&... rest)
84  {
85  m_args.append(first);
86 
87  return values(rest...);
88  }
89 
94  {
95  QString sql = QLatin1String("INSERT INTO ");
96  sql.append(m_table);
97  sql.append(QLatin1String(" VALUES ("));
98 
99  QStringList lst;
100  for (int i = 0; i < m_insertArray.count(); ++i)
101  {
102  lst.append(QLatin1String("?"));
103  }
104 
105  sql.append(lst.join(QLatin1String(",")));
106  sql.append(QLatin1String(")"));
107 
108  q.prepare(sql);
109 
110  bool res = false;
111 
112  if (m_insertArray.count() > 0 && m_insertArray[0].count() > 1)
113  {
114  for (int i = 0; i < m_insertArray.count(); ++i)
115  {
116  q.addBindValue(m_insertArray.at(i));
117  }
118 
119  res = q.execBatch();
120  }
121  else
122  {
123  for (int i = 0; i < m_insertArray.count(); ++i)
124  {
125  q.addBindValue(m_insertArray.at(i).first());
126  }
127 
128  res = q.exec();
129  }
130 
131  m_args.clear();
132  m_insertArray.clear();
133 
134 #ifdef DB_EXCEPTIONS_ENABLED
135 
136  if (!res)
137  throw DBException(q);
138 
139 #endif
140 
141  return NonQueryResult(q);
142  }
143 
144 private:
145  QString m_table;
146  QSqlQuery q;
147  QVariantList m_args;
148  QVector<QVariantList> m_insertArray;
149 };
150 
151 #endif // EASYQTSQL_INSERTQUERY_H
InsertQuery & values(const QVariant &first, const Rest &... rest)
Definition: EasyQtSql_InsertQuery.h:83
InsertQuery(const QString &table, const QSqlDatabase &db)
Definition: EasyQtSql_InsertQuery.h:42
InsertQuery & values(const QVariant &t)
Adds list of insert-values to INSERT INTO table(...) VALUES ... query.
Definition: EasyQtSql_InsertQuery.h:60
QSqlQuery wrapper for non-select query results reading.
Definition: EasyQtSql_NonQueryResult.h:38
NonQueryResult exec()
Executes prepared InsertQuery with insert values list.
Definition: EasyQtSql_InsertQuery.h:93
QSqlQuery wrapper for INSERT INTO table query execution.
Definition: EasyQtSql_InsertQuery.h:39
Exception class for SQL errors handling.
Definition: EasyQtSql_DBException.h:38