EasyQtSql
Easy SQL data access helper for QtSql
EasyQtSql_PreparedQuery.h
Go to the documentation of this file.
1 #ifndef EASYQTSQL_PREPAREDQUERY_H
2 #define EASYQTSQL_PREPAREDQUERY_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>
32 #include "EasyQtSql_QueryResult.h"
34 
35 #endif
36 
64 {
65 public:
66  PreparedQuery(const QString &stmt, const QSqlDatabase &db, bool forwardOnly = true)
67  :m_query(db)
68  {
69  m_query.setForwardOnly(forwardOnly);
70  m_query.prepare(stmt);
71  }
72 
74  {
75  m_index = 0;
76 
77  const bool res = m_query.exec();
78 
79 #ifdef DB_EXCEPTIONS_ENABLED
80 
81  if (!res)
82  throw DBException(m_query);
83 
84 #endif
85 
86  m_result = QueryResult(m_query, m_aliases);
87 
88  m_aliases.clear();
89 
90  return m_result;
91  }
92 
93  QueryResult &exec(const QVariant &bindValue)
94  {
95  m_query.bindValue(m_index, bindValue);
96 
97  return exec();
98  }
99 
100  QueryResult &exec(const In &paramWrapper)
101  {
102  m_query.bindValue(m_index, paramWrapper.value, QSql::In);
103 
104  addAliasIfSet(paramWrapper, m_index);
105 
106  return exec();
107  }
108 
109  QueryResult &exec(const Out &paramWrapper)
110  {
111  m_query.bindValue(m_index, paramWrapper.value, QSql::Out);
112 
113  addAliasIfSet(paramWrapper, m_index);
114 
115  return exec();
116  }
117 
118  QueryResult &exec(const InOut &paramWrapper)
119  {
120  m_query.bindValue(m_index, paramWrapper.value, QSql::InOut);
121 
122  addAliasIfSet(paramWrapper, m_index);
123 
124  return exec();
125  }
126 
127  template <typename... Rest> QueryResult &exec(const QVariant &bindValue, const Rest&... rest)
128  {
129  m_query.bindValue(m_index, bindValue);
130 
131  m_index++;
132 
133  return exec(rest...);
134  }
135 
136  template <typename... Rest> QueryResult &exec(const In &paramWrapper, const Rest&... rest)
137  {
138  m_query.bindValue(m_index, paramWrapper.value, QSql::In);
139 
140  addAliasIfSet(paramWrapper, m_index);
141 
142  m_index++;
143 
144  return exec(rest...);
145  }
146 
147  template <typename... Rest> QueryResult &exec(const Out &paramWrapper, const Rest&... rest)
148  {
149  m_query.bindValue(m_index, paramWrapper.value, QSql::Out);
150 
151  addAliasIfSet(paramWrapper, m_index);
152 
153  m_index++;
154 
155  return exec(rest...);
156  }
157 
158  template <typename... Rest> QueryResult &exec(const InOut &paramWrapper, const Rest&... rest)
159  {
160  m_query.bindValue(m_index, paramWrapper.value, QSql::InOut);
161 
162  addAliasIfSet(paramWrapper, m_index);
163 
164  m_index++;
165 
166  return exec(rest...);
167  }
168 
169 private:
170  QSqlQuery m_query;
171 
172  int m_index = 0;
173 
174  QueryResult m_result;
175 
176  QMap<QString, int> m_aliases;
177 
178  void addAliasIfSet(const ParamDirectionWrapper &paramWrapper, int index)
179  {
180  if (!paramWrapper.alias.isEmpty())
181  {
182  m_aliases[paramWrapper.alias] = index;
183  }
184  }
185 };
186 
187 #endif // EASYQTSQL_PREPAREDQUERY_H
Input SQL parameters wrapper.
Definition: EasyQtSql_ParamDirectionWrapper.h:61
Class for query preparation and execution.
Definition: EasyQtSql_PreparedQuery.h:63
const QString alias
Definition: EasyQtSql_ParamDirectionWrapper.h:50
PreparedQuery(const QString &stmt, const QSqlDatabase &db, bool forwardOnly=true)
Definition: EasyQtSql_PreparedQuery.h:66
QueryResult & exec(const Out &paramWrapper)
Definition: EasyQtSql_PreparedQuery.h:109
QueryResult & exec(const Out &paramWrapper, const Rest &... rest)
Definition: EasyQtSql_PreparedQuery.h:147
QueryResult & exec(const In &paramWrapper)
Definition: EasyQtSql_PreparedQuery.h:100
QueryResult & exec(const InOut &paramWrapper)
Definition: EasyQtSql_PreparedQuery.h:118
QueryResult & exec(const InOut &paramWrapper, const Rest &... rest)
Definition: EasyQtSql_PreparedQuery.h:158
QueryResult & exec()
Definition: EasyQtSql_PreparedQuery.h:73
QSqlQuery wrapper with handy data fetch methods.
Definition: EasyQtSql_QueryResult.h:39
QueryResult & exec(const QVariant &bindValue, const Rest &... rest)
Definition: EasyQtSql_PreparedQuery.h:127
Base parameters wrapper struct.
Definition: EasyQtSql_ParamDirectionWrapper.h:40
Output SQL parameters wrapper.
Definition: EasyQtSql_ParamDirectionWrapper.h:76
Bidirectional SQL parameters wrapper.
Definition: EasyQtSql_ParamDirectionWrapper.h:91
QueryResult & exec(const In &paramWrapper, const Rest &... rest)
Definition: EasyQtSql_PreparedQuery.h:136
QueryResult & exec(const QVariant &bindValue)
Definition: EasyQtSql_PreparedQuery.h:93
Exception class for SQL errors handling.
Definition: EasyQtSql_DBException.h:38
const QVariant value
Definition: EasyQtSql_ParamDirectionWrapper.h:49