QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches
IxDao_Helper.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 _IX_DAO_HELPER_H_
33#define _IX_DAO_HELPER_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#include <QtSql/qsqlrecord.h>
51
54
55#include <QxDao/QxSqlDatabase.h>
57#include <QxDao/QxSqlQuery.h>
58#include <QxDao/IxSqlRelation.h>
60
62
64
66
69
70namespace qx {
71template <class T>
72QxInvalidValueX validate(T & t, const QString & group);
73class QxSession;
74} // namespace qx
75
76namespace qx {
77namespace dao {
78namespace detail {
79
80struct IxDao_Timer;
81
87{
88
89 friend struct IxDao_Timer;
90
91public:
92
93 enum timer_type { timer_none, timer_total, timer_db_exec, timer_db_next, timer_db_prepare, timer_cpp_build_hierarchy,
94 timer_cpp_build_instance, timer_cpp_read_instance, timer_build_sql, timer_db_open, timer_db_transaction };
95
96private:
97
98 struct IxDao_HelperImpl;
99 std::unique_ptr<IxDao_HelperImpl> m_pImpl;
100
101protected:
102
103 IxDao_Helper(qx::IxSqlQueryBuilder * pBuilder, const qx::QxSqlQuery * pQuery = NULL);
104 virtual ~IxDao_Helper();
105
106public:
107
108 bool isValid() const;
109 bool hasFeature(QSqlDriver::DriverFeature ft) const;
110
111 QSqlDatabase & database();
112 const QSqlDatabase & database() const;
113 QSqlQuery & query();
114 const QSqlQuery & query() const;
115 QSqlError & error();
116 const QSqlError & error() const;
118 const qx::QxSqlQuery & qxQuery() const;
122 long getDataCount() const;
124 qx::IxDataMember * nextData(long & l) const;
125 QString sql() const;
128 QString getIgnoreSoftDeleteHash() const;
130 QStringList getSqlColumns() const;
131 void setSqlColumns(const QStringList & lst);
132 bool getUseExecBatch() const;
133 void setUseExecBatch(bool b);
138 QStringList & itemsAsJson();
139 bool isReadOnly() const;
140 bool isMongoDB() const;
141 bool isDistinct() const;
142 QVariant getIdFromQuery(int iNameIndex = -1) const;
143
144 QSqlError errFailed(bool bPrepare = false);
145 QSqlError errEmpty();
146 QSqlError errNoData();
147 QSqlError errInvalidId();
149 QSqlError errReadOnly();
150
153 void quiet();
154 bool exec(bool bForceEmptyExec = false);
155 bool prepare(QString & sql);
156
157 QSqlError updateError(const QSqlError & error);
158 bool updateSqlRelationX(const QStringList & relation);
159 void addQuery(bool bResolve);
160 void dumpRecord() const;
162
163 template <class U>
164 inline bool isValidPrimaryKey(const U & u)
165 { return (getDataId() && qx::trait::is_valid_primary_key(getDataId()->toVariant((& u), -1, qx::cvt::context::e_database))); }
166
167 template <class U>
168 inline void updateLastInsertId(U & u)
169 {
170 if (getDataId() && getDataId()->getAutoIncrement() && this->hasFeature(QSqlDriver::LastInsertId))
171 { getDataId()->fromVariant((& u), query().lastInsertId(), -1, qx::cvt::context::e_database); }
172 }
173
174 template <class U>
175 inline bool validateInstance(U & u)
176 {
177 qx::QxInvalidValueX invalidValues = qx::validate(u, "");
178 this->addInvalidValues(invalidValues);
179 return (invalidValues.count() <= 0);
180 }
181
182protected:
183
184 void dumpBoundValues() const;
185 QSqlError updateError(const QString & sError);
188 void init(QSqlDatabase * pDatabase, const QString & sContext);
189 void terminate();
190
191};
192
207
208} // namespace detail
209} // namespace dao
210} // namespace qx
211
212#endif // _IX_DAO_HELPER_H_
Common interface for a list of IxDataMember class properties registered into QxOrm context (for examp...
Common interface for all SQL generators to build SQL query specific for each database.
Common interface to build SQL queries to communicate with database.
Common interface for all relationships defined between 2 classes (or between 2 tables in database)
QxOrm thread-safe container (keep insertion order + quick access by index + quick access by key)
List of invalid values.
#define QX_DLL_EXPORT
Definition QxMacro.h:182
Define all parameters to connect to database and retrieve a valid connection by thread.
Define a user SQL query added to default SQL query builded by QxOrm library, and used by qx::dao::xxx...
Hierarchy of relationships to build SQL query.
Define a validator error exception (for example, inserting or updating an element into database) and ...
qx::IxDataMember : common interface for all class properties registered into QxOrm context
qx::IxDataMemberX : common interface for a list of IxDataMember class properties registered into QxOr...
qx::IxSqlQueryBuilder : common interface to build SQL queries to communicate with database
qx::QxCollection<Key, Value> : QxOrm thread-safe container (keep insertion order + quick access by in...
qx::QxInvalidValueX : list of invalid values
long count() const
qx::QxSession : define a session to manage automatically database transactions (using C++ RAII)
Definition QxSession.h:119
qx::QxSqlQuery : define a user SQL query added to default SQL query builded by QxOrm library,...
Definition QxSqlQuery.h:245
qx::QxSqlRelationLinked : hierarchy of relationships to build SQL query
qx::dao::detail::IxDao_Helper : helper class to communicate with database
QVariant getIdFromQuery(int iNameIndex=-1) const
qx::QxSession * getSession() const
const QSqlQuery & query() const
bool prepare(QString &sql)
bool isValidPrimaryKey(const U &u)
const QSqlError & error() const
qx::QxSqlQuery & qxQuery()
bool getAddAutoIncrementIdToUpdateQuery() const
QSqlError errFailed(bool bPrepare=false)
const QSqlDatabase & database() const
QSqlError updateError(const QString &sError)
void init(QSqlDatabase *pDatabase, const QString &sContext)
QStringList getSqlColumns() const
void addQuery(bool bResolve)
qx::IxDataMember * nextData(long &l) const
std::unique_ptr< IxDao_HelperImpl > m_pImpl
Private implementation idiom.
qx::QxCollection< QString, QVariantList > & getListExecBatch()
bool exec(bool bForceEmptyExec=false)
const qx::IxSqlQueryBuilder & builder() const
qx::QxSqlRelationLinked * getSqlRelationLinked() const
bool updateSqlRelationX(const QStringList &relation)
const qx::QxSqlQuery & qxQuery() const
IxDao_Helper(qx::IxSqlQueryBuilder *pBuilder, const qx::QxSqlQuery *pQuery=NULL)
qx::IxDataMember * getDataId() const
void timerStart(IxDao_Helper::timer_type timer)
qint64 timerElapsed(IxDao_Helper::timer_type timer)
void addInvalidValues(const qx::QxInvalidValueX &lst)
QSqlError updateError(const QSqlError &error)
QString getIgnoreSoftDeleteHash() const
qx::IxSqlQueryBuilder & builder()
qx::IxDataMemberX * getDataMemberX() const
IxSqlGenerator * getSqlGenerator() const
bool hasFeature(QSqlDriver::DriverFeature ft) const
void setSqlColumns(const QStringList &lst)
qx::dao::detail::IxSqlGenerator : common interface for all SQL generators to build SQL query specific...
qx::trait::get_primary_key<T>::type : return primary key type of T, by default primary key is long ty...
qx::trait::is_valid_primary_key<T>(const T & t) : return true if t can be a valid primary key to be i...
bool is_valid_primary_key(const T &t)
Root namespace for all QxOrm library features.
QxInvalidValueX validate(T &t, const QString &group)
qx::dao::detail::IxDao_Timer : scoped timer to measure database elapsed times (using C++ RAII)
IxDao_Helper::timer_type m_eTimerType
Timer type (database or C++ action)
IxDao_Timer(IxDao_Helper *pDaoHelper, IxDao_Helper::timer_type timer)
IxDao_Helper * m_pDaoHelper
Pointer to dao helper class.