QxOrm
1.5.0
C++ Object Relational Mapping library
|
00001 /**************************************************************************** 00002 ** 00003 ** https://www.qxorm.com/ 00004 ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com) 00005 ** 00006 ** This file is part of the QxOrm library 00007 ** 00008 ** This software is provided 'as-is', without any express or implied 00009 ** warranty. In no event will the authors be held liable for any 00010 ** damages arising from the use of this software 00011 ** 00012 ** Commercial Usage 00013 ** Licensees holding valid commercial QxOrm licenses may use this file in 00014 ** accordance with the commercial license agreement provided with the 00015 ** Software or, alternatively, in accordance with the terms contained in 00016 ** a written agreement between you and Lionel Marty 00017 ** 00018 ** GNU General Public License Usage 00019 ** Alternatively, this file may be used under the terms of the GNU 00020 ** General Public License version 3.0 as published by the Free Software 00021 ** Foundation and appearing in the file 'license.gpl3.txt' included in the 00022 ** packaging of this file. Please review the following information to 00023 ** ensure the GNU General Public License version 3.0 requirements will be 00024 ** met : http://www.gnu.org/copyleft/gpl.html 00025 ** 00026 ** If you are unsure which license is appropriate for your use, or 00027 ** if you have questions regarding the use of this file, please contact : 00028 ** contact@qxorm.com 00029 ** 00030 ****************************************************************************/ 00031 00032 #ifndef _IX_DAO_HELPER_H_ 00033 #define _IX_DAO_HELPER_H_ 00034 00035 #ifdef _MSC_VER 00036 #pragma once 00037 #endif 00038 00046 #include <QtSql/qsqldatabase.h> 00047 #include <QtSql/qsqlquery.h> 00048 #include <QtSql/qsqlerror.h> 00049 #include <QtSql/qsqldriver.h> 00050 #include <QtSql/qsqlrecord.h> 00051 00052 #include <QxTraits/get_primary_key.h> 00053 #include <QxTraits/is_valid_primary_key.h> 00054 00055 #include <QxDao/QxSqlDatabase.h> 00056 #include <QxDao/IxSqlQueryBuilder.h> 00057 #include <QxDao/QxSqlQuery.h> 00058 #include <QxDao/IxSqlRelation.h> 00059 #include <QxDao/QxSqlRelationLinked.h> 00060 00061 #include <QxDao/QxSqlGenerator/IxSqlGenerator.h> 00062 00063 #include <QxCollection/QxCollection.h> 00064 00065 #include <QxDataMember/IxDataMemberX.h> 00066 00067 #include <QxValidator/QxInvalidValueX.h> 00068 #include <QxValidator/QxValidatorError.h> 00069 00070 namespace qx { 00071 template <class T> 00072 QxInvalidValueX validate(T & t, const QString & group); 00073 class QxSession; 00074 } // namespace qx 00075 00076 namespace qx { 00077 namespace dao { 00078 namespace detail { 00079 00080 struct IxDao_Timer; 00081 00086 class QX_DLL_EXPORT IxDao_Helper 00087 { 00088 00089 friend struct IxDao_Timer; 00090 00091 public: 00092 00093 enum timer_type { timer_none, timer_total, timer_db_exec, timer_db_next, timer_db_prepare, timer_cpp_build_hierarchy, 00094 timer_cpp_build_instance, timer_cpp_read_instance, timer_build_sql, timer_db_open, timer_db_transaction }; 00095 00096 private: 00097 00098 struct IxDao_HelperImpl; 00099 std::unique_ptr<IxDao_HelperImpl> m_pImpl; 00100 00101 protected: 00102 00103 IxDao_Helper(qx::IxSqlQueryBuilder * pBuilder, const qx::QxSqlQuery * pQuery = NULL); 00104 virtual ~IxDao_Helper(); 00105 00106 public: 00107 00108 bool isValid() const; 00109 bool hasFeature(QSqlDriver::DriverFeature ft) const; 00110 00111 QSqlDatabase & database(); 00112 const QSqlDatabase & database() const; 00113 QSqlQuery & query(); 00114 const QSqlQuery & query() const; 00115 QSqlError & error(); 00116 const QSqlError & error() const; 00117 qx::QxSqlQuery & qxQuery(); 00118 const qx::QxSqlQuery & qxQuery() const; 00119 qx::IxSqlQueryBuilder & builder(); 00120 const qx::IxSqlQueryBuilder & builder() const; 00121 qx::IxDataMemberX * getDataMemberX() const; 00122 long getDataCount() const; 00123 qx::IxDataMember * getDataId() const; 00124 qx::IxDataMember * nextData(long & l) const; 00125 QString sql() const; 00126 qx::QxSqlRelationLinked * getSqlRelationLinked() const; 00127 qx::QxSession * getSession() const; 00128 QString getIgnoreSoftDeleteHash() const; 00129 bool getCartesianProduct() const; 00130 QStringList getSqlColumns() const; 00131 void setSqlColumns(const QStringList & lst); 00132 bool getUseExecBatch() const; 00133 void setUseExecBatch(bool b); 00134 qx::QxCollection<QString, QVariantList> & getListExecBatch(); 00135 IxSqlGenerator * getSqlGenerator() const; 00136 void addInvalidValues(const qx::QxInvalidValueX & lst); 00137 bool getAddAutoIncrementIdToUpdateQuery() const; 00138 QStringList & itemsAsJson(); 00139 bool isReadOnly() const; 00140 bool isMongoDB() const; 00141 bool isDistinct() const; 00142 QVariant getIdFromQuery(int iNameIndex = -1) const; 00143 00144 QSqlError errFailed(bool bPrepare = false); 00145 QSqlError errEmpty(); 00146 QSqlError errNoData(); 00147 QSqlError errInvalidId(); 00148 QSqlError errInvalidRelation(); 00149 QSqlError errReadOnly(); 00150 00151 bool transaction(); 00152 bool nextRecord(); 00153 void quiet(); 00154 bool exec(bool bForceEmptyExec = false); 00155 bool prepare(QString & sql); 00156 00157 QSqlError updateError(const QSqlError & error); 00158 bool updateSqlRelationX(const QStringList & relation); 00159 void addQuery(bool bResolve); 00160 void dumpRecord() const; 00161 void resolveQuery(); 00162 00163 template <class U> 00164 inline bool isValidPrimaryKey(const U & u) 00165 { return (getDataId() && qx::trait::is_valid_primary_key(getDataId()->toVariant((& u), -1, qx::cvt::context::e_database))); } 00166 00167 template <class U> 00168 inline void updateLastInsertId(U & u) 00169 { 00170 if (getDataId() && getDataId()->getAutoIncrement() && this->hasFeature(QSqlDriver::LastInsertId)) 00171 { getDataId()->fromVariant((& u), query().lastInsertId(), -1, qx::cvt::context::e_database); } 00172 } 00173 00174 template <class U> 00175 inline bool validateInstance(U & u) 00176 { 00177 qx::QxInvalidValueX invalidValues = qx::validate(u, ""); 00178 this->addInvalidValues(invalidValues); 00179 return (invalidValues.count() <= 0); 00180 } 00181 00182 protected: 00183 00184 void dumpBoundValues() const; 00185 QSqlError updateError(const QString & sError); 00186 void timerStart(IxDao_Helper::timer_type timer); 00187 qint64 timerElapsed(IxDao_Helper::timer_type timer); 00188 void init(QSqlDatabase * pDatabase, const QString & sContext); 00189 void terminate(); 00190 00191 }; 00192 00197 struct IxDao_Timer 00198 { 00199 00200 IxDao_Helper * m_pDaoHelper; 00201 IxDao_Helper::timer_type m_eTimerType; 00202 00203 IxDao_Timer(IxDao_Helper * pDaoHelper, IxDao_Helper::timer_type timer) : m_pDaoHelper(pDaoHelper), m_eTimerType(timer) { if (m_pDaoHelper) { m_pDaoHelper->timerStart(m_eTimerType); } } 00204 ~IxDao_Timer() { if (m_pDaoHelper) { m_pDaoHelper->timerElapsed(m_eTimerType); } } 00205 00206 }; 00207 00208 } // namespace detail 00209 } // namespace dao 00210 } // namespace qx 00211 00212 #endif // _IX_DAO_HELPER_H_