QxSqlDatabase.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 _QX_SQL_DATABASE_H_
  32. #define _QX_SQL_DATABASE_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file QxSqlDatabase.h
  38. * \author Lionel Marty
  39. * \ingroup QxDao
  40. * \brief Define all parameters to connect to database and retrieve a valid connection by thread
  41. */
  42. #include <QtCore/qhash.h>
  43. #include <QtCore/qmutex.h>
  44. #include <QtCore/qthread.h>
  45. #include <QtCore/quuid.h>
  46. #include <QtSql/qsqldatabase.h>
  47. #include <QtSql/qsqlquery.h>
  48. #include <QtSql/qsqlerror.h>
  49. #include <QxSingleton/QxSingleton.h>
  50. #include <QxDao/QxSqlGenerator/IxSqlGenerator.h>
  51. namespace qx {
  52. namespace dao {
  53. namespace detail {
  54. class IxDao_Helper;
  55. } // namespace detail
  56. } // namespace dao
  57. } // namespace qx
  58. namespace qx {
  59. /*!
  60. * \ingroup QxDao
  61. * \brief qx::QxSqlDatabase : define all parameters to connect to database and retrieve a valid connection by thread (this class is a singleton and is thread-safe)
  62. */
  63. class QX_DLL_EXPORT QxSqlDatabase : public QxSingleton<QxSqlDatabase>
  64. {
  65. friend class QxSingleton<QxSqlDatabase>;
  66. friend class qx::dao::detail::IxDao_Helper;
  67. public:
  68. enum ph_style { ph_style_question_mark, ph_style_2_point_name, ph_style_at_name };
  69. typedef std::function<void (QSqlDatabase &)> type_fct_db_open;
  70. private:
  71. struct QxSqlDatabaseImpl;
  72. std::unique_ptr<QxSqlDatabaseImpl> m_pImpl; //!< Private implementation idiom
  73. QxSqlDatabase();
  74. virtual ~QxSqlDatabase();
  75. public:
  76. QString getDriverName() const;
  77. QString getConnectOptions() const;
  78. QString getDatabaseName() const;
  79. QString getUserName() const;
  80. QString getPassword() const;
  81. QString getHostName() const;
  82. int getPort() const;
  83. bool getTraceSqlQuery() const;
  84. bool getTraceSqlRecord() const;
  85. bool getTraceSqlBoundValues() const;
  86. bool getTraceSqlBoundValuesOnError() const;
  87. ph_style getSqlPlaceHolderStyle() const;
  88. bool getSessionThrowable() const;
  89. bool getSessionAutoTransaction() const;
  90. bool getValidatorThrowable() const;
  91. bool getAutoReplaceSqlAliasIntoQuery() const;
  92. bool getVerifyOffsetRelation() const;
  93. bool getAddAutoIncrementIdToUpdateQuery() const;
  94. bool getForceParentIdToAllChildren() const;
  95. type_fct_db_open getFctDatabaseOpen() const;
  96. bool getAddSqlSquareBracketsForTableName() const;
  97. bool getAddSqlSquareBracketsForColumnName() const;
  98. bool getFormatSqlQueryBeforeLogging() const;
  99. QStringList getSqlDelimiterForTableName() const;
  100. QStringList getSqlDelimiterForColumnName() const;
  101. int getTraceSqlOnlySlowQueriesDatabase() const;
  102. int getTraceSqlOnlySlowQueriesTotal() const;
  103. bool getDisplayTimerDetails() const;
  104. void setDriverName(const QString & s, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  105. void setConnectOptions(const QString & s, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  106. void setDatabaseName(const QString & s, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  107. void setUserName(const QString & s, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  108. void setPassword(const QString & s, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  109. void setHostName(const QString & s, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  110. void setPort(int i, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  111. void setTraceSqlQuery(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  112. void setTraceSqlRecord(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  113. void setTraceSqlBoundValues(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  114. void setTraceSqlBoundValuesOnError(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  115. void setSqlPlaceHolderStyle(ph_style e, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  116. void setSessionThrowable(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  117. void setSessionAutoTransaction(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  118. void setValidatorThrowable(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  119. void setSqlGenerator(qx::dao::detail::IxSqlGenerator_ptr p, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  120. void setAutoReplaceSqlAliasIntoQuery(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  121. void setVerifyOffsetRelation(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  122. void setAddAutoIncrementIdToUpdateQuery(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  123. void setForceParentIdToAllChildren(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  124. void setFctDatabaseOpen(type_fct_db_open fct, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  125. void setAddSqlSquareBracketsForTableName(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  126. void setAddSqlSquareBracketsForColumnName(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  127. void setFormatSqlQueryBeforeLogging(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  128. void setSqlDelimiterForTableName(const QStringList & lst, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  129. void setSqlDelimiterForColumnName(const QStringList & lst, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  130. void setTraceSqlOnlySlowQueriesDatabase(int i, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  131. void setTraceSqlOnlySlowQueriesTotal(int i, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  132. void setDisplayTimerDetails(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
  133. static QSqlDatabase getDatabase();
  134. static QSqlDatabase getDatabase(QSqlError & dbError);
  135. static QSqlDatabase getDatabaseCloned();
  136. static QSqlDatabase checkDatabaseByThread();
  137. static void closeAllDatabases();
  138. static void clearAllDatabases();
  139. static bool isEmpty();
  140. qx::dao::detail::IxSqlGenerator * getSqlGenerator();
  141. void clearAllSettingsForCurrentThread();
  142. void clearAllSettingsForDatabase(QSqlDatabase * p);
  143. protected:
  144. bool setCurrentDatabaseByThread(QSqlDatabase * p);
  145. void clearCurrentDatabaseByThread();
  146. };
  147. } // namespace qx
  148. QX_DLL_EXPORT_QX_SINGLETON_HPP(qx::QxSqlDatabase)
  149. #endif // _QX_SQL_DATABASE_H_