![]() |
QxOrm
1.4.4
C++ Object Relational Mapping library
|
00001 /**************************************************************************** 00002 ** 00003 ** http://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 _QX_SQL_DATABASE_H_ 00033 #define _QX_SQL_DATABASE_H_ 00034 00035 #ifdef _MSC_VER 00036 #pragma once 00037 #endif 00038 00046 #include <QtCore/qhash.h> 00047 #include <QtCore/qmutex.h> 00048 #include <QtCore/qthread.h> 00049 #include <QtCore/quuid.h> 00050 00051 #include <QtSql/qsqldatabase.h> 00052 #include <QtSql/qsqlquery.h> 00053 #include <QtSql/qsqlerror.h> 00054 00055 #include <QxSingleton/QxSingleton.h> 00056 00057 #include <QxDao/QxSqlGenerator/IxSqlGenerator.h> 00058 00059 #define QX_CONSTRUCT_QX_SQL_DATABASE() \ 00060 m_iPort(-1), m_bTraceSqlQuery(true), m_bTraceSqlRecord(false), \ 00061 m_bTraceSqlBoundValues(false), m_bTraceSqlBoundValuesOnError(true), \ 00062 m_ePlaceHolderStyle(ph_style_2_point_name), m_bSessionThrowable(false), \ 00063 m_bSessionAutoTransaction(true), m_bValidatorThrowable(false), \ 00064 m_bAutoReplaceSqlAliasIntoQuery(true), m_bVerifyOffsetRelation(false), \ 00065 m_bAddAutoIncrementIdToUpdateQuery(true), m_bForceParentIdToAllChildren(false) 00066 00067 namespace qx { 00068 00073 class QX_DLL_EXPORT QxSqlDatabase : public QxSingleton<QxSqlDatabase> 00074 { 00075 00076 friend class QxSingleton<QxSqlDatabase>; 00077 00078 public: 00079 00080 enum ph_style { ph_style_question_mark, ph_style_2_point_name, ph_style_at_name }; 00081 00082 typedef std::function<void (QSqlDatabase &)> type_fct_db_open; 00083 00084 private: 00085 00086 QHash<Qt::HANDLE, QString> m_lstDbByThread; 00087 QMutex m_oDbMutex; 00088 QString m_sDriverName; 00089 QString m_sConnectOptions; 00090 QString m_sDatabaseName; 00091 QString m_sUserName; 00092 QString m_sPassword; 00093 QString m_sHostName; 00094 int m_iPort; 00095 bool m_bTraceSqlQuery; 00096 bool m_bTraceSqlRecord; 00097 bool m_bTraceSqlBoundValues; 00098 bool m_bTraceSqlBoundValuesOnError; 00099 ph_style m_ePlaceHolderStyle; 00100 bool m_bSessionThrowable; 00101 bool m_bSessionAutoTransaction; 00102 bool m_bValidatorThrowable; 00103 qx::dao::detail::IxSqlGenerator_ptr m_pSqlGenerator; 00104 bool m_bAutoReplaceSqlAliasIntoQuery; 00105 bool m_bVerifyOffsetRelation; 00106 bool m_bAddAutoIncrementIdToUpdateQuery; 00107 bool m_bForceParentIdToAllChildren; 00108 type_fct_db_open m_fctDatabaseOpen; 00109 00110 private: 00111 00112 QxSqlDatabase() : QxSingleton<QxSqlDatabase>("qx::QxSqlDatabase"), QX_CONSTRUCT_QX_SQL_DATABASE() { ; } 00113 virtual ~QxSqlDatabase() { ; } 00114 00115 public: 00116 00117 QString getDriverName() const { return m_sDriverName; } 00118 QString getConnectOptions() const { return m_sConnectOptions; } 00119 QString getDatabaseName() const { return m_sDatabaseName; } 00120 QString getUserName() const { return m_sUserName; } 00121 QString getPassword() const { return m_sPassword; } 00122 QString getHostName() const { return m_sHostName; } 00123 int getPort() const { return m_iPort; } 00124 bool getTraceSqlQuery() const { return m_bTraceSqlQuery; } 00125 bool getTraceSqlRecord() const { return m_bTraceSqlRecord; } 00126 bool getTraceSqlBoundValues() const { return m_bTraceSqlBoundValues; } 00127 bool getTraceSqlBoundValuesOnError() const { return m_bTraceSqlBoundValuesOnError; } 00128 ph_style getSqlPlaceHolderStyle() const { return m_ePlaceHolderStyle; } 00129 bool getSessionThrowable() const { return m_bSessionThrowable; } 00130 bool getSessionAutoTransaction() const { return m_bSessionAutoTransaction; } 00131 bool getValidatorThrowable() const { return m_bValidatorThrowable; } 00132 bool getAutoReplaceSqlAliasIntoQuery() const { return m_bAutoReplaceSqlAliasIntoQuery; } 00133 bool getVerifyOffsetRelation() const { return m_bVerifyOffsetRelation; } 00134 bool getAddAutoIncrementIdToUpdateQuery() const { return m_bAddAutoIncrementIdToUpdateQuery; } 00135 bool getForceParentIdToAllChildren() const { return m_bForceParentIdToAllChildren; } 00136 type_fct_db_open getFctDatabaseOpen() const { return m_fctDatabaseOpen; } 00137 00138 void setDriverName(const QString & s) { m_sDriverName = s; getSqlGenerator(); } 00139 void setConnectOptions(const QString & s) { m_sConnectOptions = s; } 00140 void setDatabaseName(const QString & s) { m_sDatabaseName = s; } 00141 void setUserName(const QString & s) { m_sUserName = s; } 00142 void setPassword(const QString & s) { m_sPassword = s; } 00143 void setHostName(const QString & s) { m_sHostName = s; } 00144 void setPort(int i) { m_iPort = i; } 00145 void setTraceSqlQuery(bool b) { m_bTraceSqlQuery = b; } 00146 void setTraceSqlRecord(bool b) { m_bTraceSqlRecord = b; } 00147 void setTraceSqlBoundValues(bool b) { m_bTraceSqlBoundValues = b; } 00148 void setTraceSqlBoundValuesOnError(bool b) { m_bTraceSqlBoundValuesOnError = b; } 00149 void setSqlPlaceHolderStyle(ph_style e) { m_ePlaceHolderStyle = e; } 00150 void setSessionThrowable(bool b) { m_bSessionThrowable = b; } 00151 void setSessionAutoTransaction(bool b) { m_bSessionAutoTransaction = b; } 00152 void setValidatorThrowable(bool b) { m_bValidatorThrowable = b; } 00153 void setSqlGenerator(qx::dao::detail::IxSqlGenerator_ptr p) { m_pSqlGenerator = p; if (p) { p->init(); } } 00154 void setAutoReplaceSqlAliasIntoQuery(bool b) { m_bAutoReplaceSqlAliasIntoQuery = b; } 00155 void setVerifyOffsetRelation(bool b) { m_bVerifyOffsetRelation = b; } 00156 void setAddAutoIncrementIdToUpdateQuery(bool b) { m_bAddAutoIncrementIdToUpdateQuery = b; } 00157 void setForceParentIdToAllChildren(bool b) { m_bForceParentIdToAllChildren = b; } 00158 void setFctDatabaseOpen(type_fct_db_open fct) { m_fctDatabaseOpen = fct; } 00159 00160 static QSqlDatabase getDatabase(); 00161 static QSqlDatabase getDatabase(QSqlError & dbError); 00162 static QSqlDatabase getDatabaseCloned(); 00163 static void closeAllDatabases(); 00164 static void clearAllDatabases(); 00165 00166 qx::dao::detail::IxSqlGenerator * getSqlGenerator(); 00167 00168 private: 00169 00170 QSqlDatabase getDatabaseByCurrThreadId(QSqlError & dbError); 00171 QSqlDatabase createDatabase(QSqlError & dbError); 00172 00173 void displayLastError(const QSqlDatabase & db, const QString & sDesc) const; 00174 QString formatLastError(const QSqlDatabase & db) const; 00175 00176 bool isValid() const { return (! m_sDriverName.isEmpty() && ! m_sDatabaseName.isEmpty()); } 00177 00178 }; 00179 00180 } // namespace qx 00181 00182 QX_DLL_EXPORT_QX_SINGLETON_HPP(qx::QxSqlDatabase) 00183 00184 #endif // _QX_SQL_DATABASE_H_