cutelyst  4.5.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
sql.h
1 /*
2  * SPDX-FileCopyrightText: (C) 2015-2022 Daniel Nicoletti <dantti12@gmail.com>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 #ifndef CSQL_H
6 #define CSQL_H
7 
8 #include <Cutelyst/Plugins/Utils/sql_export.h>
9 #include <Cutelyst/paramsmultimap.h>
10 
11 #include <QtCore/QVariant>
12 #include <QtSql/QSqlDatabase>
13 
14 namespace Cutelyst {
15 
16 namespace Sql {
17 
31 class CUTELYST_PLUGIN_UTILS_SQL_EXPORT Transaction
32 {
33 public:
38  explicit Transaction(const QString &databaseName = QString());
39 
44  Transaction(const QSqlDatabase &database);
45 
50  ~Transaction();
51 
55  bool transaction() const;
56 
60  bool commit();
61 
65  bool rollback();
66 
67 private:
68  QSqlDatabase m_db;
69  bool m_transactionRunning;
70 };
71 
78 CUTELYST_PLUGIN_UTILS_SQL_EXPORT QVariantHash queryToHashObject(QSqlQuery &query);
79 
86 CUTELYST_PLUGIN_UTILS_SQL_EXPORT QVariantList queryToHashList(QSqlQuery &query);
87 
94 CUTELYST_PLUGIN_UTILS_SQL_EXPORT QVariantMap queryToMapObject(QSqlQuery &query);
95 
101 CUTELYST_PLUGIN_UTILS_SQL_EXPORT QJsonObject queryToJsonObject(QSqlQuery &query);
102 
110 CUTELYST_PLUGIN_UTILS_SQL_EXPORT QVariantList queryToMapList(QSqlQuery &query);
111 
116 CUTELYST_PLUGIN_UTILS_SQL_EXPORT QJsonArray queryToJsonObjectArray(QSqlQuery &query);
117 
123 CUTELYST_PLUGIN_UTILS_SQL_EXPORT QVariantList queryToList(QSqlQuery &query);
124 
130 CUTELYST_PLUGIN_UTILS_SQL_EXPORT QJsonArray queryToJsonArray(QSqlQuery &query);
131 
138 CUTELYST_PLUGIN_UTILS_SQL_EXPORT QVariantHash queryToIndexedHash(QSqlQuery &query,
139  const QString &key);
140 
147 CUTELYST_PLUGIN_UTILS_SQL_EXPORT QJsonObject queryToIndexedJsonObject(QSqlQuery &query,
148  const QString &key);
149 
156 CUTELYST_PLUGIN_UTILS_SQL_EXPORT void bindParamsToQuery(QSqlQuery &query,
157  const Cutelyst::ParamsMultiMap &params,
158  bool htmlEscaped = true);
159 
177 CUTELYST_PLUGIN_UTILS_SQL_EXPORT QSqlQuery preparedQuery(const QString &query,
178  QSqlDatabase db = QSqlDatabase(),
179  bool forwardOnly = false);
180 
185 CUTELYST_PLUGIN_UTILS_SQL_EXPORT QString databaseNameThread(const QString &dbName = QString());
186 
191 CUTELYST_PLUGIN_UTILS_SQL_EXPORT QSqlDatabase databaseThread(const QString &dbName = QString());
192 
211 CUTELYST_PLUGIN_UTILS_SQL_EXPORT QSqlQuery preparedQueryThread(const QString &query,
212  const QString &dbName = QString(),
213  bool forwardOnly = false);
214 } // namespace Sql
215 
216 } // namespace Cutelyst
217 
223 #define CPreparedSqlQueryForDatabase(str, db) \
224  ([]() -> QSqlQuery { \
225  static thread_local QSqlQuery query_temp = Cutelyst::Sql::preparedQuery(str, db); \
226  return query_temp; \
227  }())
228 
234 #define CPreparedSqlQuery(str) \
235  ([]() -> QSqlQuery { \
236  static QSqlQuery query_temp = Cutelyst::Sql::preparedQuery(str); \
237  return query_temp; \
238  }())
239 
245 #define CPreparedSqlQueryThread(str) \
246  ([]() -> QSqlQuery { \
247  static thread_local QSqlQuery query_temp = Cutelyst::Sql::preparedQueryThread(str); \
248  return query_temp; \
249  }())
250 
256 #define CPreparedSqlQueryThreadForDB(str, db) \
257  ([]() -> QSqlQuery { \
258  static thread_local QSqlQuery query_temp = Cutelyst::Sql::preparedQueryThread(str, db); \
259  return query_temp; \
260  }())
261 
268 #define CPreparedSqlQueryForDatabaseFO(str, db) \
269  ([]() -> QSqlQuery { \
270  static thread_local QSqlQuery query_temp = Cutelyst::Sql::preparedQuery(str, db, true); \
271  return query_temp; \
272  }())
273 
280 #define CPreparedSqlQueryFO(str) \
281  ([]() -> QSqlQuery { \
282  static QSqlQuery query_temp = Cutelyst::Sql::preparedQuery(str, QSqlDatabase(), true); \
283  return query_temp; \
284  }())
285 
292 #define CPreparedSqlQueryThreadFO(str) \
293  ([]() -> QSqlQuery { \
294  static thread_local QSqlQuery query_temp = \
295  Cutelyst::Sql::preparedQueryThread(str, QString(), true); \
296  return query_temp; \
297  }())
298 
305 #define CPreparedSqlQueryThreadForDBFO(str, db) \
306  ([]() -> QSqlQuery { \
307  static thread_local QSqlQuery query_temp = \
308  Cutelyst::Sql::preparedQueryThread(str, db, true); \
309  return query_temp; \
310  }())
311 
312 #endif // CSQL_H
This is a helper class to create scoped transactions.
Definition: sql.h:32
CUTELYST_PLUGIN_UTILS_SQL_EXPORT QVariantList queryToHashList(QSqlQuery &query)
Definition: sql.cpp:33
CUTELYST_PLUGIN_UTILS_SQL_EXPORT QVariantMap queryToMapObject(QSqlQuery &query)
Definition: sql.cpp:53
CUTELYST_PLUGIN_UTILS_SQL_EXPORT QVariantHash queryToHashObject(QSqlQuery &query)
Definition: sql.cpp:20
CUTELYST_PLUGIN_UTILS_SQL_EXPORT void bindParamsToQuery(QSqlQuery &query, const Cutelyst::ParamsMultiMap &params, bool htmlEscaped=true)
Definition: sql.cpp:215
CUTELYST_PLUGIN_UTILS_SQL_EXPORT QJsonArray queryToJsonObjectArray(QSqlQuery &query)
Definition: sql.cpp:99
CUTELYST_PLUGIN_UTILS_SQL_EXPORT QString databaseNameThread(const QString &dbName=QString())
Definition: sql.cpp:263
CUTELYST_PLUGIN_UTILS_SQL_EXPORT QSqlQuery preparedQuery(const QString &query, QSqlDatabase db=QSqlDatabase(), bool forwardOnly=false)
Definition: sql.cpp:241
CUTELYST_PLUGIN_UTILS_SQL_EXPORT QVariantList queryToList(QSqlQuery &query)
Definition: sql.cpp:119
CUTELYST_PLUGIN_UTILS_SQL_EXPORT QJsonObject queryToIndexedJsonObject(QSqlQuery &query, const QString &key)
Definition: sql.cpp:183
CUTELYST_PLUGIN_UTILS_SQL_EXPORT QVariantList queryToMapList(QSqlQuery &query)
Definition: sql.cpp:79
CUTELYST_PLUGIN_UTILS_SQL_EXPORT QSqlDatabase databaseThread(const QString &dbName=QString())
Definition: sql.cpp:268
CUTELYST_PLUGIN_UTILS_SQL_EXPORT QVariantHash queryToIndexedHash(QSqlQuery &query, const QString &key)
Definition: sql.cpp:151
CUTELYST_PLUGIN_UTILS_SQL_EXPORT QJsonArray queryToJsonArray(QSqlQuery &query)
Definition: sql.cpp:135
CUTELYST_PLUGIN_UTILS_SQL_EXPORT QSqlQuery preparedQueryThread(const QString &query, const QString &dbName=QString(), bool forwardOnly=false)
Definition: sql.cpp:252
CUTELYST_PLUGIN_UTILS_SQL_EXPORT QJsonObject queryToJsonObject(QSqlQuery &query)
Definition: sql.cpp:66
The Cutelyst namespace holds all public Cutelyst API.