QxOrm  1.2.3
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 #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)
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 
00094 private:
00095 
00096    QxSqlDatabase() : QxSingleton<QxSqlDatabase>("qx::QxSqlDatabase"), QX_CONSTRUCT_QX_SQL_DATABASE() { ; }
00097    virtual ~QxSqlDatabase() { ; }
00098 
00099 public:
00100 
00101    QString getDriverName() const                   { return m_sDriverName; }
00102    QString getConnectOptions() const               { return m_sConnectOptions; }
00103    QString getDatabaseName() const                 { return m_sDatabaseName; }
00104    QString getUserName() const                     { return m_sUserName; }
00105    QString getPassword() const                     { return m_sPassword; }
00106    QString getHostName() const                     { return m_sHostName; }
00107    int getPort() const                             { return m_iPort; }
00108    bool getTraceSqlQuery() const                   { return m_bTraceSqlQuery; }
00109    bool getTraceSqlRecord() const                  { return m_bTraceSqlRecord; }
00110    ph_style getSqlPlaceHolderStyle() const         { return m_ePlaceHolderStyle; }
00111    bool getSessionThrowable() const                { return m_bSessionThrowable; }
00112    bool getSessionAutoTransaction() const          { return m_bSessionAutoTransaction; }
00113    bool getValidatorThrowable() const              { return m_bValidatorThrowable; }
00114    bool getAutoReplaceSqlAliasIntoQuery() const    { return m_bAutoReplaceSqlAliasIntoQuery; }
00115 
00116    void setDriverName(const QString & s)                          { m_sDriverName = s; }
00117    void setConnectOptions(const QString & s)                      { m_sConnectOptions = s; }
00118    void setDatabaseName(const QString & s)                        { m_sDatabaseName = s; }
00119    void setUserName(const QString & s)                            { m_sUserName = s; }
00120    void setPassword(const QString & s)                            { m_sPassword = s; }
00121    void setHostName(const QString & s)                            { m_sHostName = s; }
00122    void setPort(int i)                                            { m_iPort = i; }
00123    void setTraceSqlQuery(bool b)                                  { m_bTraceSqlQuery = b; }
00124    void setTraceSqlRecord(bool b)                                 { m_bTraceSqlRecord = b; }
00125    void setSqlPlaceHolderStyle(ph_style e)                        { m_ePlaceHolderStyle = e; }
00126    void setSessionThrowable(bool b)                               { m_bSessionThrowable = b; }
00127    void setSessionAutoTransaction(bool b)                         { m_bSessionAutoTransaction = b; }
00128    void setValidatorThrowable(bool b)                             { m_bValidatorThrowable = b; }
00129    void setSqlGenerator(qx::dao::detail::IxSqlGenerator_ptr p)    { m_pSqlGenerator = p; }
00130    void setAutoReplaceSqlAliasIntoQuery(bool b)                   { m_bAutoReplaceSqlAliasIntoQuery = b; }
00131 
00132    static QSqlDatabase getDatabase()         { return qx::QxSqlDatabase::getSingleton()->getDatabaseByCurrThreadId(); }
00133    static QSqlDatabase getDatabaseCloned()   { return QSqlDatabase::cloneDatabase(qx::QxSqlDatabase::getDatabase(), QUuid::createUuid().toString()); }
00134 
00135    qx::dao::detail::IxSqlGenerator * getSqlGenerator();
00136 
00137 private:
00138 
00139    QSqlDatabase getDatabaseByCurrThreadId();
00140    QSqlDatabase createDatabase();
00141 
00142    void displayLastError(const QSqlDatabase & db, const QString & sDesc) const;
00143    QString formatLastError(const QSqlDatabase & db) const;
00144 
00145    bool isValid() const { return (! m_sDriverName.isEmpty() && ! m_sDatabaseName.isEmpty()); }
00146 
00147 };
00148 
00149 } // namespace qx
00150 
00151 QX_DLL_EXPORT_QX_SINGLETON_HPP(qx::QxSqlDatabase)
00152 
00153 #endif // _QX_SQL_DATABASE_H_