QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches
QxSession.h
Go to the documentation of this file.
1/****************************************************************************
2**
3** https://www.qxorm.com/
4** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
5**
6** This file is part of the QxOrm library
7**
8** This software is provided 'as-is', without any express or implied
9** warranty. In no event will the authors be held liable for any
10** damages arising from the use of this software
11**
12** Commercial Usage
13** Licensees holding valid commercial QxOrm licenses may use this file in
14** accordance with the commercial license agreement provided with the
15** Software or, alternatively, in accordance with the terms contained in
16** a written agreement between you and Lionel Marty
17**
18** GNU General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU
20** General Public License version 3.0 as published by the Free Software
21** Foundation and appearing in the file 'license.gpl3.txt' included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU General Public License version 3.0 requirements will be
24** met : http://www.gnu.org/copyleft/gpl.html
25**
26** If you are unsure which license is appropriate for your use, or
27** if you have questions regarding the use of this file, please contact :
28** contact@qxorm.com
29**
30****************************************************************************/
31
32#ifndef _QX_DAO_SESSION_H_
33#define _QX_DAO_SESSION_H_
34
35#ifdef _MSC_VER
36#pragma once
37#endif
38
46#include <QtSql/qsqldatabase.h>
47#include <QtSql/qsqlquery.h>
48#include <QtSql/qsqlerror.h>
49#include <QtSql/qsqldriver.h>
50
51#include <QtCore/qlist.h>
52
53#include <QxCommon/QxBool.h>
54
55#include <QxDao/QxDao.h>
56#include <QxDao/QxSqlQuery.h>
57#include <QxDao/QxSqlError.h>
58
59#include <QxRegister/QxClass.h>
60
61namespace qx {
62
119{
120
121private:
122
123 struct QxSessionImpl;
124 std::shared_ptr<QxSessionImpl> m_pImpl;
125
126public:
127
129 QxSession(const QSqlDatabase & database);
130 QxSession(const QSqlDatabase & database, bool bOpenTransaction);
131 QxSession(const QSqlDatabase & database, bool bOpenTransaction, bool bThrowable, bool bAutoRollbackWhenDestroyed = false);
132 virtual ~QxSession();
133
134 bool isThrowable() const;
135 bool isOpened() const;
136 bool isValid() const;
139 QSqlError firstError() const;
140 QSqlError lastError() const;
141 QList<QSqlError> allErrors() const;
142 const QSqlDatabase * database() const;
143 QSqlDatabase * database();
144
145 bool open();
146 bool close();
147 bool commit();
148 bool rollback();
149
150 QxSession & operator+= (const QSqlError & err);
151
152 static QxSession * getActiveSession(QSqlDatabase * db);
153
154 void ignoreSoftDelete(bool bIgnoreSoftDelete = true, const QStringList & classesToIgnore = QStringList());
155 bool checkIgnoreSoftDelete(const QString & classKey) const;
156 QString getIgnoreSoftDeleteHash() const;
157
158 template <class T>
159 long count(const qx::QxSqlQuery & query = qx::QxSqlQuery())
160 { return qx::dao::count<T>(query, this->database()); }
161
162 template <class T>
163 QSqlError count(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery())
164 { return qx::dao::count<T>(lCount, query, this->database()); }
165
166 template <class T>
167 T * fetchById(const QVariant & id, const QStringList & columns = QStringList(), const QStringList & relation = QStringList())
168 {
170 IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL);
171 if (! pDataMemberId) { qAssert(false); return NULL; }
172 T * t = new T(); QSqlError err;
173 pDataMemberId->fromVariant(t, id, -1, qx::cvt::context::e_database);
174 if (relation.count() == 0) { err = qx::dao::fetch_by_id((* t), this->database(), columns); }
175 else { err = qx::dao::fetch_by_id_with_relation(relation, (* t), this->database()); }
176 if (err.isValid()) { delete t; t = NULL; (* this) += err; }
177 return t;
178 }
179
180 template <class T>
181 QSqlError fetchById(T & t, const QStringList & columns = QStringList(), const QStringList & relation = QStringList())
182 {
183 QSqlError err;
184 if (relation.count() == 0) { err = qx::dao::fetch_by_id(t, this->database(), columns); }
185 else { err = qx::dao::fetch_by_id_with_relation(relation, t, this->database()); }
186 if (err.isValid()) { (* this) += err; }
187 return err;
188 }
189
190 template <class T>
191 QSqlError fetchAll(T & t, const QStringList & columns = QStringList(), const QStringList & relation = QStringList())
192 {
193 QSqlError err;
194 if (relation.count() == 0) { err = qx::dao::fetch_all(t, this->database(), columns); }
195 else { err = qx::dao::fetch_all_with_relation(relation, t, this->database()); }
196 if (err.isValid()) { (* this) += err; }
197 return err;
198 }
199
200 template <class T>
201 QSqlError fetchByQuery(const qx::QxSqlQuery & query, T & t, const QStringList & columns = QStringList(), const QStringList & relation = QStringList())
202 {
203 QSqlError err;
204 if (relation.count() == 0) { err = qx::dao::fetch_by_query(query, t, this->database(), columns); }
205 else { err = qx::dao::fetch_by_query_with_relation(relation, query, t, this->database()); }
206 if (err.isValid()) { (* this) += err; }
207 return err;
208 }
209
210 template <class T>
211 QSqlError insert(T & t, const QStringList & relation = QStringList(), bool bUseExecBatch = false)
212 {
213 QSqlError err;
214 if (relation.count() == 0) { err = qx::dao::insert(t, this->database(), bUseExecBatch); }
215 else { err = qx::dao::insert_with_relation(relation, t, this->database()); }
216 if (err.isValid()) { (* this) += err; }
217 return err;
218 }
219
220 template <class T>
221 QSqlError update(T & t, const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & columns = QStringList(), const QStringList & relation = QStringList(), bool bUseExecBatch = false)
222 {
223 QSqlError err;
224 if (relation.count() == 0) { err = qx::dao::update_by_query(query, t, this->database(), columns, bUseExecBatch); }
225 else { err = qx::dao::update_by_query_with_relation(relation, query, t, this->database()); }
226 if (err.isValid()) { (* this) += err; }
227 return err;
228 }
229
230 template <class T>
231 QSqlError save(T & t, const QStringList & relation = QStringList())
232 {
233 QSqlError err;
234 if (relation.count() == 0) { err = qx::dao::save(t, this->database()); }
235 else { err = qx::dao::save_with_relation(relation, t, this->database()); }
236 if (err.isValid()) { (* this) += err; }
237 return err;
238 }
239
240 template <class T>
241 QSqlError deleteById(const QVariant & id)
242 {
244 IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL);
245 if (! pDataMemberId) { qAssert(false); return QSqlError(); }
246 std::shared_ptr<T> t = std::make_shared<T>();
247 pDataMemberId->fromVariant(t.get(), id, -1, qx::cvt::context::e_database);
248 QSqlError err = qx::dao::delete_by_id((* t), this->database());
249 if (err.isValid()) { (* this) += err; }
250 return err;
251 }
252
253 template <class T>
254 QSqlError deleteById(T & t, bool bUseExecBatch = false)
255 {
256 QSqlError err = qx::dao::delete_by_id(t, this->database(), bUseExecBatch);
257 if (err.isValid()) { (* this) += err; }
258 return err;
259 }
260
261 template <class T>
262 QSqlError deleteAll()
263 {
264 QSqlError err = qx::dao::delete_all<T>(this->database());
265 if (err.isValid()) { (* this) += err; }
266 return err;
267 }
268
269 template <class T>
270 QSqlError deleteByQuery(const qx::QxSqlQuery & query)
271 {
272 QSqlError err = qx::dao::delete_by_query<T>(query, this->database());
273 if (err.isValid()) { (* this) += err; }
274 return err;
275 }
276
277 template <class T>
278 QSqlError destroyById(const QVariant & id)
279 {
281 IxDataMember * pDataMemberId = (pDataMemberX ? pDataMemberX->getId_WithDaoStrategy() : NULL);
282 if (! pDataMemberId) { qAssert(false); return QSqlError(); }
283 std::shared_ptr<T> t = std::make_shared<T>();
284 pDataMemberId->fromVariant(t.get(), id, -1, qx::cvt::context::e_database);
285 QSqlError err = qx::dao::destroy_by_id((* t), this->database());
286 if (err.isValid()) { (* this) += err; }
287 return err;
288 }
289
290 template <class T>
291 QSqlError destroyById(T & t, bool bUseExecBatch = false)
292 {
293 QSqlError err = qx::dao::destroy_by_id(t, this->database(), bUseExecBatch);
294 if (err.isValid()) { (* this) += err; }
295 return err;
296 }
297
298 template <class T>
299 QSqlError destroyAll()
300 {
301 QSqlError err = qx::dao::destroy_all<T>(this->database());
302 if (err.isValid()) { (* this) += err; }
303 return err;
304 }
305
306 template <class T>
307 QSqlError destroyByQuery(const qx::QxSqlQuery & query)
308 {
309 QSqlError err = qx::dao::destroy_by_query<T>(query, this->database());
310 if (err.isValid()) { (* this) += err; }
311 return err;
312 }
313
314 template <class T>
315 QSqlError executeQuery(qx::QxSqlQuery & query, T & t)
316 {
317 QSqlError err = qx::dao::execute_query<T>(query, t, this->database());
318 if (err.isValid()) { (* this) += err; }
319 return err;
320 }
321
322 QSqlError callQuery(qx::QxSqlQuery & query)
323 {
324 QSqlError err = qx::dao::call_query(query, this->database());
325 if (err.isValid()) { (* this) += err; }
326 return err;
327 }
328
329 template <class T>
331 { return qx::dao::exist(t, this->database()); }
332
333private:
334
335 QxSession(const QxSession & other) { Q_UNUSED(other); }
336 QxSession & operator=(const QxSession & other) { Q_UNUSED(other); return (* this); }
337
338};
339
340} // namespace qx
341
342#endif // _QX_DAO_SESSION_H_
qx_bool : QxOrm library boolean type with code and description message when an error occured
Concrete class registered into QxOrm context.
Provide template functions to map C++ class registered into QxOrm context with table database (ORM - ...
#define qAssert(x)
Definition QxMacro.h:52
#define QX_DLL_EXPORT
Definition QxMacro.h:182
Define a SQL error exception and retrieve QSqlError type of Qt library.
Define a user SQL query added to default SQL query builded by QxOrm library, and used by qx::dao::xxx...
IxDataMemberX * getDataMemberX() const
qx::IxDataMember : common interface for all class properties registered into QxOrm context
virtual qx_bool fromVariant(void *pOwner, const QVariant &v, const QString &sFormat, int iIndexName=-1, qx::cvt::context::ctx_type ctx=qx::cvt::context::e_no_context)=0
qx::IxDataMemberX : common interface for a list of IxDataMember class properties registered into QxOr...
virtual IxDataMember * getId_WithDaoStrategy() const =0
qx_bool : boolean type with code and description message when an error occured
Definition QxBool.h:71
qx::QxClass<T> : concrete class of type T registered into QxOrm context (this class is a singleton an...
Definition QxClass.h:79
qx::QxSession : define a session to manage automatically database transactions (using C++ RAII)
Definition QxSession.h:119
QSqlError destroyAll()
Definition QxSession.h:299
const QSqlDatabase * database() const
QSqlError save(T &t, const QStringList &relation=QStringList())
Definition QxSession.h:231
QxSession(const QSqlDatabase &database, bool bOpenTransaction, bool bThrowable, bool bAutoRollbackWhenDestroyed=false)
qx_bool exist(T &t)
Definition QxSession.h:330
static QxSession * getActiveSession(QSqlDatabase *db)
QSqlError deleteAll()
Definition QxSession.h:262
QSqlError count(long &lCount, const qx::QxSqlQuery &query=qx::QxSqlQuery())
Definition QxSession.h:163
QxSession(const QxSession &other)
Definition QxSession.h:335
QList< QSqlError > allErrors() const
bool isAutoRollbackWhenDestroyed() const
bool isValid() const
QxSession & operator=(const QxSession &other)
Definition QxSession.h:336
QSqlError fetchAll(T &t, const QStringList &columns=QStringList(), const QStringList &relation=QStringList())
Definition QxSession.h:191
QSqlError fetchByQuery(const qx::QxSqlQuery &query, T &t, const QStringList &columns=QStringList(), const QStringList &relation=QStringList())
Definition QxSession.h:201
void setAutoRollbackWhenDestroyed(bool b)
QSqlError deleteById(const QVariant &id)
Definition QxSession.h:241
QxSession(const QSqlDatabase &database, bool bOpenTransaction)
QSqlError fetchById(T &t, const QStringList &columns=QStringList(), const QStringList &relation=QStringList())
Definition QxSession.h:181
bool isThrowable() const
QxSession(const QSqlDatabase &database)
long count(const qx::QxSqlQuery &query=qx::QxSqlQuery())
Definition QxSession.h:159
bool isOpened() const
QSqlError executeQuery(qx::QxSqlQuery &query, T &t)
Definition QxSession.h:315
T * fetchById(const QVariant &id, const QStringList &columns=QStringList(), const QStringList &relation=QStringList())
Definition QxSession.h:167
QSqlError destroyByQuery(const qx::QxSqlQuery &query)
Definition QxSession.h:307
std::shared_ptr< QxSessionImpl > m_pImpl
Private implementation idiom (use std::shared_ptr instead of std::unique_ptr because of incomplete ty...
Definition QxSession.h:124
QSqlError deleteByQuery(const qx::QxSqlQuery &query)
Definition QxSession.h:270
QSqlError lastError() const
QSqlError callQuery(qx::QxSqlQuery &query)
Definition QxSession.h:322
QSqlError deleteById(T &t, bool bUseExecBatch=false)
Definition QxSession.h:254
QSqlError destroyById(const QVariant &id)
Definition QxSession.h:278
QSqlDatabase * database()
QSqlError update(T &t, const qx::QxSqlQuery &query=qx::QxSqlQuery(), const QStringList &columns=QStringList(), const QStringList &relation=QStringList(), bool bUseExecBatch=false)
Definition QxSession.h:221
QSqlError firstError() const
virtual ~QxSession()
bool checkIgnoreSoftDelete(const QString &classKey) const
QSqlError destroyById(T &t, bool bUseExecBatch=false)
Definition QxSession.h:291
QSqlError insert(T &t, const QStringList &relation=QStringList(), bool bUseExecBatch=false)
Definition QxSession.h:211
QString getIgnoreSoftDeleteHash() const
void ignoreSoftDelete(bool bIgnoreSoftDelete=true, const QStringList &classesToIgnore=QStringList())
qx::QxSqlQuery : define a user SQL query added to default SQL query builded by QxOrm library,...
Definition QxSqlQuery.h:245
QSqlError destroy_all(QSqlDatabase *pDatabase=NULL)
Destroy all lines of a table (database) mapped to a C++ class T (registered into QxOrm context),...
Definition QxDao.h:220
QSqlError execute_query(qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase=NULL)
Execute a custom SQL query or a stored procedure, all columns that can be mapped to the instance of t...
Definition QxDao.h:743
QSqlError save(T &t, QSqlDatabase *pDatabase=NULL)
Insert (if no exist) or update (if already exist) an element or a list of elements into database.
Definition QxDao.h:158
QSqlError fetch_by_id(T &t, QSqlDatabase *pDatabase=NULL, const QStringList &columns=QStringList())
Fetch an object t (retrieve all its properties) of type T (registered into QxOrm context) mapped to a...
Definition QxDao.h:636
QSqlError fetch_by_query_with_relation(const QString &relation, const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase=NULL)
Fetch a list of objects (retrieve all elements and properties associated) of type T (container regist...
Definition QxDao.h:382
QX_DLL_EXPORT QSqlError call_query(qx::QxSqlQuery &query, QSqlDatabase *pDatabase=NULL)
qx::dao::call_query function can be used to call a custom SQL query or a stored procedure
QSqlError fetch_by_id_with_relation(const QString &relation, T &t, QSqlDatabase *pDatabase=NULL)
Fetch an object t (retrieve all its properties) of type T (registered into QxOrm context) mapped to a...
Definition QxDao.h:293
QSqlError delete_by_id(T &t, QSqlDatabase *pDatabase=NULL, bool bUseExecBatch=false)
Delete a line (or list of lines) of a table (database) mapped to a C++ object of type T (registered i...
Definition QxDao.h:176
qx_bool exist(T &t, QSqlDatabase *pDatabase=NULL)
Search if an element (or list of elements) already exists into database.
Definition QxDao.h:278
QSqlError save_with_relation(const QString &relation, T &t, QSqlDatabase *pDatabase=NULL)
Insert (if no exist) or update (if already exist) an element and its relationships (or a list of elem...
Definition QxDao.h:565
QSqlError insert_with_relation(const QString &relation, T &t, QSqlDatabase *pDatabase=NULL)
Insert an element and its relationships (or a list of elements + relationships) into database.
Definition QxDao.h:428
QSqlError insert(T &t, QSqlDatabase *pDatabase=NULL, bool bUseExecBatch=false)
Insert an element or a list of elements into database.
Definition QxDao.h:142
QSqlError destroy_by_query(const qx::QxSqlQuery &query, QSqlDatabase *pDatabase=NULL)
Destroy all lines of a table (database) mapped to a C++ class T (registered into QxOrm context) and f...
Definition QxDao.h:251
QSqlError delete_all(QSqlDatabase *pDatabase=NULL)
Delete all lines of a table (database) mapped to a C++ class T (registered into QxOrm context)
Definition QxDao.h:207
QSqlError update_by_query(const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase=NULL, const QStringList &columns=QStringList(), bool bUseExecBatch=false)
Update an element or a list of elements into database (adding a user SQL query to the default SQL que...
Definition QxDao.h:700
QSqlError fetch_by_query(const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase=NULL, const QStringList &columns=QStringList())
Fetch a list of objects (retrieve all elements and properties associated) of type T (container regist...
Definition QxDao.h:667
QSqlError update_by_query_with_relation(const QString &relation, const qx::QxSqlQuery &query, T &t, QSqlDatabase *pDatabase=NULL)
Update an element and its relationships (or a list of elements + relationships) into database (adding...
Definition QxDao.h:488
QSqlError fetch_all(T &t, QSqlDatabase *pDatabase=NULL, const QStringList &columns=QStringList())
Fetch a list of objects (retrieve all elements and properties associated) of type T (container regist...
Definition QxDao.h:651
QSqlError delete_by_query(const qx::QxSqlQuery &query, QSqlDatabase *pDatabase=NULL)
Delete all lines of a table (database) mapped to a C++ class T (registered into QxOrm context) and fi...
Definition QxDao.h:237
QSqlError fetch_all_with_relation(const QString &relation, T &t, QSqlDatabase *pDatabase=NULL)
Fetch a list of objects (retrieve all elements and properties associated) of type T (container regist...
Definition QxDao.h:337
long count(const qx::QxSqlQuery &query=qx::QxSqlQuery(), QSqlDatabase *pDatabase=NULL)
Return the number of lines in the table (database) mapped to the C++ class T (registered into QxOrm c...
Definition QxDao.h:98
QSqlError destroy_by_id(T &t, QSqlDatabase *pDatabase=NULL, bool bUseExecBatch=false)
Destroy a line (or list of lines) of a table (database) mapped to a C++ object of type T (registered ...
Definition QxDao.h:191
Root namespace for all QxOrm library features.