IxDao_Helper.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #ifndef _IX_DAO_HELPER_H_
  32. #define _IX_DAO_HELPER_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file IxDao_Helper.h
  38. * \author Lionel Marty
  39. * \ingroup QxDao
  40. * \brief Helper class to communicate with database
  41. */
  42. #include <QtSql/qsqldatabase.h>
  43. #include <QtSql/qsqlquery.h>
  44. #include <QtSql/qsqlerror.h>
  45. #include <QtSql/qsqldriver.h>
  46. #include <QtSql/qsqlrecord.h>
  47. #include <QxTraits/get_primary_key.h>
  48. #include <QxTraits/is_valid_primary_key.h>
  49. #include <QxDao/QxSqlDatabase.h>
  50. #include <QxDao/IxSqlQueryBuilder.h>
  51. #include <QxDao/QxSqlQuery.h>
  52. #include <QxDao/IxSqlRelation.h>
  53. #include <QxDao/QxSqlRelationLinked.h>
  54. #include <QxDao/QxSqlGenerator/IxSqlGenerator.h>
  55. #include <QxCollection/QxCollection.h>
  56. #include <QxDataMember/IxDataMemberX.h>
  57. #include <QxValidator/QxInvalidValueX.h>
  58. #include <QxValidator/QxValidatorError.h>
  59. namespace qx {
  60. template <class T>
  61. QxInvalidValueX validate(T & t, const QString & group);
  62. class QxSession;
  63. } // namespace qx
  64. namespace qx {
  65. namespace dao {
  66. namespace detail {
  67. struct IxDao_Timer;
  68. /*!
  69. * \ingroup QxDao
  70. * \brief qx::dao::detail::IxDao_Helper : helper class to communicate with database
  71. */
  72. class QX_DLL_EXPORT IxDao_Helper
  73. {
  74. friend struct IxDao_Timer;
  75. public:
  76. enum timer_type { timer_none, timer_total, timer_db_exec, timer_db_next, timer_db_prepare, timer_cpp_build_hierarchy,
  77. timer_cpp_build_instance, timer_cpp_read_instance, timer_build_sql, timer_db_open, timer_db_transaction };
  78. private:
  79. struct IxDao_HelperImpl;
  80. std::unique_ptr<IxDao_HelperImpl> m_pImpl; //!< Private implementation idiom
  81. protected:
  82. IxDao_Helper(qx::IxSqlQueryBuilder * pBuilder, const qx::QxSqlQuery * pQuery = NULL);
  83. virtual ~IxDao_Helper();
  84. public:
  85. bool isValid() const;
  86. bool hasFeature(QSqlDriver::DriverFeature ft) const;
  87. QSqlDatabase & database();
  88. const QSqlDatabase & database() const;
  89. QSqlQuery & query();
  90. const QSqlQuery & query() const;
  91. QSqlError & error();
  92. const QSqlError & error() const;
  93. qx::QxSqlQuery & qxQuery();
  94. const qx::QxSqlQuery & qxQuery() const;
  95. qx::IxSqlQueryBuilder & builder();
  96. const qx::IxSqlQueryBuilder & builder() const;
  97. qx::IxDataMemberX * getDataMemberX() const;
  98. long getDataCount() const;
  99. qx::IxDataMember * getDataId() const;
  100. qx::IxDataMember * nextData(long & l) const;
  101. QString sql() const;
  102. qx::QxSqlRelationLinked * getSqlRelationLinked() const;
  103. qx::QxSession * getSession() const;
  104. QString getIgnoreSoftDeleteHash() const;
  105. bool getCartesianProduct() const;
  106. QStringList getSqlColumns() const;
  107. void setSqlColumns(const QStringList & lst);
  108. IxSqlGenerator * getSqlGenerator() const;
  109. void addInvalidValues(const qx::QxInvalidValueX & lst);
  110. bool getAddAutoIncrementIdToUpdateQuery() const;
  111. QStringList & itemsAsJson();
  112. bool isReadOnly() const;
  113. bool isMongoDB() const;
  114. QSqlError errFailed(bool bPrepare = false);
  115. QSqlError errEmpty();
  116. QSqlError errNoData();
  117. QSqlError errInvalidId();
  118. QSqlError errInvalidRelation();
  119. QSqlError errReadOnly();
  120. bool transaction();
  121. bool nextRecord();
  122. void quiet();
  123. bool exec(bool bForceEmptyExec = false);
  124. bool prepare(QString & sql);
  125. QSqlError updateError(const QSqlError & error);
  126. bool updateSqlRelationX(const QStringList & relation);
  127. void addQuery(bool bResolve);
  128. void dumpRecord() const;
  129. template <class U>
  130. inline bool isValidPrimaryKey(const U & u)
  131. { return (getDataId() && qx::trait::is_valid_primary_key(getDataId()->toVariant((& u), -1, qx::cvt::context::e_database))); }
  132. template <class U>
  133. inline void updateLastInsertId(U & u)
  134. {
  135. if (getDataId() && getDataId()->getAutoIncrement() && this->hasFeature(QSqlDriver::LastInsertId))
  136. { getDataId()->fromVariant((& u), query().lastInsertId(), -1, qx::cvt::context::e_database); }
  137. }
  138. template <class U>
  139. inline bool validateInstance(U & u)
  140. {
  141. qx::QxInvalidValueX invalidValues = qx::validate(u, "");
  142. this->addInvalidValues(invalidValues);
  143. return (invalidValues.count() <= 0);
  144. }
  145. protected:
  146. void dumpBoundValues() const;
  147. QSqlError updateError(const QString & sError);
  148. void timerStart(IxDao_Helper::timer_type timer);
  149. qint64 timerElapsed(IxDao_Helper::timer_type timer);
  150. void init(QSqlDatabase * pDatabase, const QString & sContext);
  151. void terminate();
  152. };
  153. /*!
  154. * \ingroup QxDao
  155. * \brief qx::dao::detail::IxDao_Timer : scoped timer to measure database elapsed times (using C++ RAII)
  156. */
  157. struct IxDao_Timer
  158. {
  159. IxDao_Helper * m_pDaoHelper; //!< Pointer to dao helper class
  160. IxDao_Helper::timer_type m_eTimerType; //!< Timer type (database or C++ action)
  161. IxDao_Timer(IxDao_Helper * pDaoHelper, IxDao_Helper::timer_type timer) : m_pDaoHelper(pDaoHelper), m_eTimerType(timer) { if (m_pDaoHelper) { m_pDaoHelper->timerStart(m_eTimerType); } }
  162. ~IxDao_Timer() { if (m_pDaoHelper) { m_pDaoHelper->timerElapsed(m_eTimerType); } }
  163. };
  164. } // namespace detail
  165. } // namespace dao
  166. } // namespace qx
  167. #endif // _IX_DAO_HELPER_H_