![]() |
QxOrm 1.1.6
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 namespace qx { 00052 00057 class QX_DLL_EXPORT QxSqlDatabase : public QxSingleton<QxSqlDatabase> 00058 { 00059 00060 friend class QxSingleton<QxSqlDatabase>; 00061 00062 public: 00063 00064 enum ph_style { ph_style_question_mark, ph_style_2_point_name, ph_style_at_name }; 00065 00066 private: 00067 00068 QHash<Qt::HANDLE, QString> m_lstDbByThread; 00069 QMutex m_oDbMutex; 00070 QString m_sDriverName; 00071 QString m_sConnectOptions; 00072 QString m_sDatabaseName; 00073 QString m_sUserName; 00074 QString m_sPassword; 00075 QString m_sHostName; 00076 int m_iPort; 00077 bool m_bTraceSqlQuery; 00078 bool m_bTraceSqlRecord; 00079 ph_style m_ePlaceHolderStyle; 00080 00081 private: 00082 00083 QxSqlDatabase() : QxSingleton<QxSqlDatabase>("qx::QxSqlDatabase"), m_iPort(-1), m_bTraceSqlQuery(true), m_bTraceSqlRecord(false), m_ePlaceHolderStyle(ph_style_2_point_name) { ; } 00084 virtual ~QxSqlDatabase() { ; } 00085 00086 public: 00087 00088 QString getDriverName() const { return m_sDriverName; } 00089 QString getConnectOptions() const { return m_sConnectOptions; } 00090 QString getDatabaseName() const { return m_sDatabaseName; } 00091 QString getUserName() const { return m_sUserName; } 00092 QString getPassword() const { return m_sPassword; } 00093 QString getHostName() const { return m_sHostName; } 00094 int getPort() const { return m_iPort; } 00095 bool getTraceSqlQuery() const { return m_bTraceSqlQuery; } 00096 bool getTraceSqlRecord() const { return m_bTraceSqlRecord; } 00097 ph_style getSqlPlaceHolderStyle() const { return m_ePlaceHolderStyle; } 00098 00099 void setDriverName(const QString & s) { m_sDriverName = s; } 00100 void setConnectOptions(const QString & s) { m_sConnectOptions = s; } 00101 void setDatabaseName(const QString & s) { m_sDatabaseName = s; } 00102 void setUserName(const QString & s) { m_sUserName = s; } 00103 void setPassword(const QString & s) { m_sPassword = s; } 00104 void setHostName(const QString & s) { m_sHostName = s; } 00105 void setPort(int i) { m_iPort = i; } 00106 void setTraceSqlQuery(bool b) { m_bTraceSqlQuery = b; } 00107 void setTraceSqlRecord(bool b) { m_bTraceSqlRecord = b; } 00108 void setSqlPlaceHolderStyle(ph_style e) { m_ePlaceHolderStyle = e; } 00109 00110 static QSqlDatabase getDatabase() { return qx::QxSqlDatabase::getSingleton()->getDatabaseByCurrThreadId(); } 00111 00112 private: 00113 00114 QSqlDatabase getDatabaseByCurrThreadId(); 00115 QSqlDatabase createDatabase(); 00116 00117 void displayLastError(const QSqlDatabase & db, const QString & sDesc) const; 00118 QString formatLastError(const QSqlDatabase & db) const; 00119 00120 bool isValid() const { return (! m_sDriverName.isEmpty() && ! m_sDatabaseName.isEmpty()); } 00121 00122 }; 00123 00124 } // namespace qx 00125 00126 QX_DLL_EXPORT_QX_SINGLETON_HPP(qx::QxSqlDatabase) 00127 00128 #endif // _QX_SQL_DATABASE_H_