![]() |
QxOrm
1.2.7
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_ePlaceHolderStyle(ph_style_2_point_name), m_bSessionThrowable(false), \ 00062 m_bSessionAutoTransaction(true), m_bValidatorThrowable(false), \ 00063 m_bAutoReplaceSqlAliasIntoQuery(true), m_bVerifyOffsetRelation(false), \ 00064 m_bAddAutoIncrementIdToUpdateQuery(true), m_bForceParentIdToAllChildren(false) 00065 00066 namespace qx { 00067 00072 class QX_DLL_EXPORT QxSqlDatabase : public QxSingleton<QxSqlDatabase> 00073 { 00074 00075 friend class QxSingleton<QxSqlDatabase>; 00076 00077 public: 00078 00079 enum ph_style { ph_style_question_mark, ph_style_2_point_name, ph_style_at_name }; 00080 00081 private: 00082 00083 QHash<Qt::HANDLE, QString> m_lstDbByThread; 00084 QMutex m_oDbMutex; 00085 QString m_sDriverName; 00086 QString m_sConnectOptions; 00087 QString m_sDatabaseName; 00088 QString m_sUserName; 00089 QString m_sPassword; 00090 QString m_sHostName; 00091 int m_iPort; 00092 bool m_bTraceSqlQuery; 00093 bool m_bTraceSqlRecord; 00094 ph_style m_ePlaceHolderStyle; 00095 bool m_bSessionThrowable; 00096 bool m_bSessionAutoTransaction; 00097 bool m_bValidatorThrowable; 00098 qx::dao::detail::IxSqlGenerator_ptr m_pSqlGenerator; 00099 bool m_bAutoReplaceSqlAliasIntoQuery; 00100 bool m_bVerifyOffsetRelation; 00101 bool m_bAddAutoIncrementIdToUpdateQuery; 00102 bool m_bForceParentIdToAllChildren; 00103 00104 private: 00105 00106 QxSqlDatabase() : QxSingleton<QxSqlDatabase>("qx::QxSqlDatabase"), QX_CONSTRUCT_QX_SQL_DATABASE() { ; } 00107 virtual ~QxSqlDatabase() { ; } 00108 00109 public: 00110 00111 QString getDriverName() const { return m_sDriverName; } 00112 QString getConnectOptions() const { return m_sConnectOptions; } 00113 QString getDatabaseName() const { return m_sDatabaseName; } 00114 QString getUserName() const { return m_sUserName; } 00115 QString getPassword() const { return m_sPassword; } 00116 QString getHostName() const { return m_sHostName; } 00117 int getPort() const { return m_iPort; } 00118 bool getTraceSqlQuery() const { return m_bTraceSqlQuery; } 00119 bool getTraceSqlRecord() const { return m_bTraceSqlRecord; } 00120 ph_style getSqlPlaceHolderStyle() const { return m_ePlaceHolderStyle; } 00121 bool getSessionThrowable() const { return m_bSessionThrowable; } 00122 bool getSessionAutoTransaction() const { return m_bSessionAutoTransaction; } 00123 bool getValidatorThrowable() const { return m_bValidatorThrowable; } 00124 bool getAutoReplaceSqlAliasIntoQuery() const { return m_bAutoReplaceSqlAliasIntoQuery; } 00125 bool getVerifyOffsetRelation() const { return m_bVerifyOffsetRelation; } 00126 bool getAddAutoIncrementIdToUpdateQuery() const { return m_bAddAutoIncrementIdToUpdateQuery; } 00127 bool getForceParentIdToAllChildren() const { return m_bForceParentIdToAllChildren; } 00128 00129 void setDriverName(const QString & s) { m_sDriverName = s; getSqlGenerator(); } 00130 void setConnectOptions(const QString & s) { m_sConnectOptions = s; } 00131 void setDatabaseName(const QString & s) { m_sDatabaseName = s; } 00132 void setUserName(const QString & s) { m_sUserName = s; } 00133 void setPassword(const QString & s) { m_sPassword = s; } 00134 void setHostName(const QString & s) { m_sHostName = s; } 00135 void setPort(int i) { m_iPort = i; } 00136 void setTraceSqlQuery(bool b) { m_bTraceSqlQuery = b; } 00137 void setTraceSqlRecord(bool b) { m_bTraceSqlRecord = b; } 00138 void setSqlPlaceHolderStyle(ph_style e) { m_ePlaceHolderStyle = e; } 00139 void setSessionThrowable(bool b) { m_bSessionThrowable = b; } 00140 void setSessionAutoTransaction(bool b) { m_bSessionAutoTransaction = b; } 00141 void setValidatorThrowable(bool b) { m_bValidatorThrowable = b; } 00142 void setSqlGenerator(qx::dao::detail::IxSqlGenerator_ptr p) { m_pSqlGenerator = p; if (p) { p->init(); } } 00143 void setAutoReplaceSqlAliasIntoQuery(bool b) { m_bAutoReplaceSqlAliasIntoQuery = b; } 00144 void setVerifyOffsetRelation(bool b) { m_bVerifyOffsetRelation = b; } 00145 void setAddAutoIncrementIdToUpdateQuery(bool b) { m_bAddAutoIncrementIdToUpdateQuery = b; } 00146 void setForceParentIdToAllChildren(bool b) { m_bForceParentIdToAllChildren = b; } 00147 00148 static QSqlDatabase getDatabase(); 00149 static QSqlDatabase getDatabase(QSqlError & dbError); 00150 static QSqlDatabase getDatabaseCloned(); 00151 static void closeAllDatabases(); 00152 static void clearAllDatabases(); 00153 00154 qx::dao::detail::IxSqlGenerator * getSqlGenerator(); 00155 00156 private: 00157 00158 QSqlDatabase getDatabaseByCurrThreadId(QSqlError & dbError); 00159 QSqlDatabase createDatabase(QSqlError & dbError); 00160 00161 void displayLastError(const QSqlDatabase & db, const QString & sDesc) const; 00162 QString formatLastError(const QSqlDatabase & db) const; 00163 00164 bool isValid() const { return (! m_sDriverName.isEmpty() && ! m_sDatabaseName.isEmpty()); } 00165 00166 }; 00167 00168 } // namespace qx 00169 00170 QX_DLL_EXPORT_QX_SINGLETON_HPP(qx::QxSqlDatabase) 00171 00172 #endif // _QX_SQL_DATABASE_H_