QxOrm 1.1.8
C++ Object Relational Mapping library
QxSqlDatabase.h
Go to the documentation of this file.
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    bool m_bSessionThrowable;                    
00081    bool m_bSessionAutoTransaction;              
00082 
00083 private:
00084 
00085    QxSqlDatabase() : QxSingleton<QxSqlDatabase>("qx::QxSqlDatabase"), m_iPort(-1), m_bTraceSqlQuery(true), m_bTraceSqlRecord(false), m_ePlaceHolderStyle(ph_style_2_point_name), m_bSessionThrowable(false), m_bSessionAutoTransaction(true) { ; }
00086    virtual ~QxSqlDatabase() { ; }
00087 
00088 public:
00089 
00090    QString getDriverName() const                { return m_sDriverName; }
00091    QString getConnectOptions() const            { return m_sConnectOptions; }
00092    QString getDatabaseName() const              { return m_sDatabaseName; }
00093    QString getUserName() const                  { return m_sUserName; }
00094    QString getPassword() const                  { return m_sPassword; }
00095    QString getHostName() const                  { return m_sHostName; }
00096    int getPort() const                          { return m_iPort; }
00097    bool getTraceSqlQuery() const                { return m_bTraceSqlQuery; }
00098    bool getTraceSqlRecord() const               { return m_bTraceSqlRecord; }
00099    ph_style getSqlPlaceHolderStyle() const      { return m_ePlaceHolderStyle; }
00100    bool getSessionThrowable() const             { return m_bSessionThrowable; }
00101    bool getSessionAutoTransaction() const       { return m_bSessionAutoTransaction; }
00102 
00103    void setDriverName(const QString & s)        { m_sDriverName = s; }
00104    void setConnectOptions(const QString & s)    { m_sConnectOptions = s; }
00105    void setDatabaseName(const QString & s)      { m_sDatabaseName = s; }
00106    void setUserName(const QString & s)          { m_sUserName = s; }
00107    void setPassword(const QString & s)          { m_sPassword = s; }
00108    void setHostName(const QString & s)          { m_sHostName = s; }
00109    void setPort(int i)                          { m_iPort = i; }
00110    void setTraceSqlQuery(bool b)                { m_bTraceSqlQuery = b; }
00111    void setTraceSqlRecord(bool b)               { m_bTraceSqlRecord = b; }
00112    void setSqlPlaceHolderStyle(ph_style e)      { m_ePlaceHolderStyle = e; }
00113    void setSessionThrowable(bool b)             { m_bSessionThrowable = b; }
00114    void setSessionAutoTransaction(bool b)       { m_bSessionAutoTransaction = b; }
00115 
00116    static QSqlDatabase getDatabase()         { return qx::QxSqlDatabase::getSingleton()->getDatabaseByCurrThreadId(); }
00117    static QSqlDatabase getDatabaseCloned()   { return QSqlDatabase::cloneDatabase(qx::QxSqlDatabase::getDatabase(), QUuid::createUuid().toString()); }
00118 
00119 private:
00120 
00121    QSqlDatabase getDatabaseByCurrThreadId();
00122    QSqlDatabase createDatabase();
00123 
00124    void displayLastError(const QSqlDatabase & db, const QString & sDesc) const;
00125    QString formatLastError(const QSqlDatabase & db) const;
00126 
00127    bool isValid() const { return (! m_sDriverName.isEmpty() && ! m_sDatabaseName.isEmpty()); }
00128 
00129 };
00130 
00131 } // namespace qx
00132 
00133 QX_DLL_EXPORT_QX_SINGLETON_HPP(qx::QxSqlDatabase)
00134 
00135 #endif // _QX_SQL_DATABASE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines