![]() |
QxOrm
1.2.4
C++ Object Relational Mapping library
|
00001 /**************************************************************************** 00002 ** 00003 ** http://www.qxorm.com/ 00004 ** http://sourceforge.net/projects/qxorm/ 00005 ** Original file by Lionel Marty 00006 ** 00007 ** This file is part of the QxOrm library 00008 ** 00009 ** This software is provided 'as-is', without any express or implied 00010 ** warranty. In no event will the authors be held liable for any 00011 ** damages arising from the use of this software. 00012 ** 00013 ** GNU Lesser General Public License Usage 00014 ** This file must be used under the terms of the GNU Lesser 00015 ** General Public License version 2.1 as published by the Free Software 00016 ** Foundation and appearing in the file 'license.lgpl.txt' included in the 00017 ** packaging of this file. Please review the following information to 00018 ** ensure the GNU Lesser General Public License version 2.1 requirements 00019 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. 00020 ** 00021 ** If you have questions regarding the use of this file, please contact : 00022 ** contact@qxorm.com 00023 ** 00024 ****************************************************************************/ 00025 00026 #ifndef _QX_SQL_DATABASE_H_ 00027 #define _QX_SQL_DATABASE_H_ 00028 00029 #ifdef _MSC_VER 00030 #pragma once 00031 #endif 00032 00040 #include <QtCore/qhash.h> 00041 #include <QtCore/qmutex.h> 00042 #include <QtCore/qthread.h> 00043 #include <QtCore/quuid.h> 00044 00045 #include <QtSql/qsqldatabase.h> 00046 #include <QtSql/qsqlquery.h> 00047 #include <QtSql/qsqlerror.h> 00048 00049 #include <QxSingleton/QxSingleton.h> 00050 00051 #include <QxDao/QxSqlGenerator/IxSqlGenerator.h> 00052 00053 #define QX_CONSTRUCT_QX_SQL_DATABASE() \ 00054 m_iPort(-1), m_bTraceSqlQuery(true), m_bTraceSqlRecord(false), \ 00055 m_ePlaceHolderStyle(ph_style_2_point_name), m_bSessionThrowable(false), \ 00056 m_bSessionAutoTransaction(true), m_bValidatorThrowable(false), \ 00057 m_bAutoReplaceSqlAliasIntoQuery(true), m_bVerifyOffsetRelation(false) 00058 00059 namespace qx { 00060 00065 class QX_DLL_EXPORT QxSqlDatabase : public QxSingleton<QxSqlDatabase> 00066 { 00067 00068 friend class QxSingleton<QxSqlDatabase>; 00069 00070 public: 00071 00072 enum ph_style { ph_style_question_mark, ph_style_2_point_name, ph_style_at_name }; 00073 00074 private: 00075 00076 QHash<Qt::HANDLE, QString> m_lstDbByThread; 00077 QMutex m_oDbMutex; 00078 QString m_sDriverName; 00079 QString m_sConnectOptions; 00080 QString m_sDatabaseName; 00081 QString m_sUserName; 00082 QString m_sPassword; 00083 QString m_sHostName; 00084 int m_iPort; 00085 bool m_bTraceSqlQuery; 00086 bool m_bTraceSqlRecord; 00087 ph_style m_ePlaceHolderStyle; 00088 bool m_bSessionThrowable; 00089 bool m_bSessionAutoTransaction; 00090 bool m_bValidatorThrowable; 00091 qx::dao::detail::IxSqlGenerator_ptr m_pSqlGenerator; 00092 bool m_bAutoReplaceSqlAliasIntoQuery; 00093 bool m_bVerifyOffsetRelation; 00094 00095 private: 00096 00097 QxSqlDatabase() : QxSingleton<QxSqlDatabase>("qx::QxSqlDatabase"), QX_CONSTRUCT_QX_SQL_DATABASE() { ; } 00098 virtual ~QxSqlDatabase() { ; } 00099 00100 public: 00101 00102 QString getDriverName() const { return m_sDriverName; } 00103 QString getConnectOptions() const { return m_sConnectOptions; } 00104 QString getDatabaseName() const { return m_sDatabaseName; } 00105 QString getUserName() const { return m_sUserName; } 00106 QString getPassword() const { return m_sPassword; } 00107 QString getHostName() const { return m_sHostName; } 00108 int getPort() const { return m_iPort; } 00109 bool getTraceSqlQuery() const { return m_bTraceSqlQuery; } 00110 bool getTraceSqlRecord() const { return m_bTraceSqlRecord; } 00111 ph_style getSqlPlaceHolderStyle() const { return m_ePlaceHolderStyle; } 00112 bool getSessionThrowable() const { return m_bSessionThrowable; } 00113 bool getSessionAutoTransaction() const { return m_bSessionAutoTransaction; } 00114 bool getValidatorThrowable() const { return m_bValidatorThrowable; } 00115 bool getAutoReplaceSqlAliasIntoQuery() const { return m_bAutoReplaceSqlAliasIntoQuery; } 00116 bool getVerifyOffsetRelation() const { return m_bVerifyOffsetRelation; } 00117 00118 void setDriverName(const QString & s) { m_sDriverName = s; getSqlGenerator(); } 00119 void setConnectOptions(const QString & s) { m_sConnectOptions = s; } 00120 void setDatabaseName(const QString & s) { m_sDatabaseName = s; } 00121 void setUserName(const QString & s) { m_sUserName = s; } 00122 void setPassword(const QString & s) { m_sPassword = s; } 00123 void setHostName(const QString & s) { m_sHostName = s; } 00124 void setPort(int i) { m_iPort = i; } 00125 void setTraceSqlQuery(bool b) { m_bTraceSqlQuery = b; } 00126 void setTraceSqlRecord(bool b) { m_bTraceSqlRecord = b; } 00127 void setSqlPlaceHolderStyle(ph_style e) { m_ePlaceHolderStyle = e; } 00128 void setSessionThrowable(bool b) { m_bSessionThrowable = b; } 00129 void setSessionAutoTransaction(bool b) { m_bSessionAutoTransaction = b; } 00130 void setValidatorThrowable(bool b) { m_bValidatorThrowable = b; } 00131 void setSqlGenerator(qx::dao::detail::IxSqlGenerator_ptr p) { m_pSqlGenerator = p; } 00132 void setAutoReplaceSqlAliasIntoQuery(bool b) { m_bAutoReplaceSqlAliasIntoQuery = b; } 00133 void setVerifyOffsetRelation(bool b) { m_bVerifyOffsetRelation = b; } 00134 00135 static QSqlDatabase getDatabase(); 00136 static QSqlDatabase getDatabase(QSqlError & dbError); 00137 static QSqlDatabase getDatabaseCloned(); 00138 00139 qx::dao::detail::IxSqlGenerator * getSqlGenerator(); 00140 00141 private: 00142 00143 QSqlDatabase getDatabaseByCurrThreadId(QSqlError & dbError); 00144 QSqlDatabase createDatabase(QSqlError & dbError); 00145 00146 void displayLastError(const QSqlDatabase & db, const QString & sDesc) const; 00147 QString formatLastError(const QSqlDatabase & db) const; 00148 00149 bool isValid() const { return (! m_sDriverName.isEmpty() && ! m_sDatabaseName.isEmpty()); } 00150 00151 }; 00152 00153 } // namespace qx 00154 00155 QX_DLL_EXPORT_QX_SINGLETON_HPP(qx::QxSqlDatabase) 00156 00157 #endif // _QX_SQL_DATABASE_H_