QxOrm 1.2.1
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 namespace qx {
00054 
00059 class QX_DLL_EXPORT QxSqlDatabase : public QxSingleton<QxSqlDatabase>
00060 {
00061 
00062    friend class QxSingleton<QxSqlDatabase>;
00063 
00064 public:
00065 
00066    enum ph_style { ph_style_question_mark, ph_style_2_point_name, ph_style_at_name };
00067 
00068 private:
00069 
00070    QHash<Qt::HANDLE, QString> m_lstDbByThread;              
00071    QMutex m_oDbMutex;                                       
00072    QString m_sDriverName;                                   
00073    QString m_sConnectOptions;                               
00074    QString m_sDatabaseName;                                 
00075    QString m_sUserName;                                     
00076    QString m_sPassword;                                     
00077    QString m_sHostName;                                     
00078    int m_iPort;                                             
00079    bool m_bTraceSqlQuery;                                   
00080    bool m_bTraceSqlRecord;                                  
00081    ph_style m_ePlaceHolderStyle;                            
00082    bool m_bSessionThrowable;                                
00083    bool m_bSessionAutoTransaction;                          
00084    qx::dao::detail::IxSqlGenerator_ptr m_pSqlGenerator;     
00085 
00086 private:
00087 
00088    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) { ; }
00089    virtual ~QxSqlDatabase() { ; }
00090 
00091 public:
00092 
00093    QString getDriverName() const                { return m_sDriverName; }
00094    QString getConnectOptions() const            { return m_sConnectOptions; }
00095    QString getDatabaseName() const              { return m_sDatabaseName; }
00096    QString getUserName() const                  { return m_sUserName; }
00097    QString getPassword() const                  { return m_sPassword; }
00098    QString getHostName() const                  { return m_sHostName; }
00099    int getPort() const                          { return m_iPort; }
00100    bool getTraceSqlQuery() const                { return m_bTraceSqlQuery; }
00101    bool getTraceSqlRecord() const               { return m_bTraceSqlRecord; }
00102    ph_style getSqlPlaceHolderStyle() const      { return m_ePlaceHolderStyle; }
00103    bool getSessionThrowable() const             { return m_bSessionThrowable; }
00104    bool getSessionAutoTransaction() const       { return m_bSessionAutoTransaction; }
00105 
00106    void setDriverName(const QString & s)                          { m_sDriverName = s; }
00107    void setConnectOptions(const QString & s)                      { m_sConnectOptions = s; }
00108    void setDatabaseName(const QString & s)                        { m_sDatabaseName = s; }
00109    void setUserName(const QString & s)                            { m_sUserName = s; }
00110    void setPassword(const QString & s)                            { m_sPassword = s; }
00111    void setHostName(const QString & s)                            { m_sHostName = s; }
00112    void setPort(int i)                                            { m_iPort = i; }
00113    void setTraceSqlQuery(bool b)                                  { m_bTraceSqlQuery = b; }
00114    void setTraceSqlRecord(bool b)                                 { m_bTraceSqlRecord = b; }
00115    void setSqlPlaceHolderStyle(ph_style e)                        { m_ePlaceHolderStyle = e; }
00116    void setSessionThrowable(bool b)                               { m_bSessionThrowable = b; }
00117    void setSessionAutoTransaction(bool b)                         { m_bSessionAutoTransaction = b; }
00118    void setSqlGenerator(qx::dao::detail::IxSqlGenerator_ptr p)    { m_pSqlGenerator = p; }
00119 
00120    static QSqlDatabase getDatabase()         { return qx::QxSqlDatabase::getSingleton()->getDatabaseByCurrThreadId(); }
00121    static QSqlDatabase getDatabaseCloned()   { return QSqlDatabase::cloneDatabase(qx::QxSqlDatabase::getDatabase(), QUuid::createUuid().toString()); }
00122 
00123    qx::dao::detail::IxSqlGenerator * getSqlGenerator();
00124 
00125 private:
00126 
00127    QSqlDatabase getDatabaseByCurrThreadId();
00128    QSqlDatabase createDatabase();
00129 
00130    void displayLastError(const QSqlDatabase & db, const QString & sDesc) const;
00131    QString formatLastError(const QSqlDatabase & db) const;
00132 
00133    bool isValid() const { return (! m_sDriverName.isEmpty() && ! m_sDatabaseName.isEmpty()); }
00134 
00135 };
00136 
00137 } // namespace qx
00138 
00139 QX_DLL_EXPORT_QX_SINGLETON_HPP(qx::QxSqlDatabase)
00140 
00141 #endif // _QX_SQL_DATABASE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines