QxOrm  1.4.3
C++ Object Relational Mapping library
QxSqlDatabase.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** http://www.qxorm.com/
00004 ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
00005 **
00006 ** This file is part of the QxOrm library
00007 **
00008 ** This software is provided 'as-is', without any express or implied
00009 ** warranty. In no event will the authors be held liable for any
00010 ** damages arising from the use of this software
00011 **
00012 ** Commercial Usage
00013 ** Licensees holding valid commercial QxOrm licenses may use this file in
00014 ** accordance with the commercial license agreement provided with the
00015 ** Software or, alternatively, in accordance with the terms contained in
00016 ** a written agreement between you and Lionel Marty
00017 **
00018 ** GNU General Public License Usage
00019 ** Alternatively, this file may be used under the terms of the GNU
00020 ** General Public License version 3.0 as published by the Free Software
00021 ** Foundation and appearing in the file 'license.gpl3.txt' included in the
00022 ** packaging of this file. Please review the following information to
00023 ** ensure the GNU General Public License version 3.0 requirements will be
00024 ** met : http://www.gnu.org/copyleft/gpl.html
00025 **
00026 ** If you are unsure which license is appropriate for your use, or
00027 ** if you have questions regarding the use of this file, please contact :
00028 ** contact@qxorm.com
00029 **
00030 ****************************************************************************/
00031 
00032 #ifndef _QX_SQL_DATABASE_H_
00033 #define _QX_SQL_DATABASE_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
00046 #include <QtCore/qhash.h>
00047 #include <QtCore/qmutex.h>
00048 #include <QtCore/qthread.h>
00049 #include <QtCore/quuid.h>
00050 
00051 #include <QtSql/qsqldatabase.h>
00052 #include <QtSql/qsqlquery.h>
00053 #include <QtSql/qsqlerror.h>
00054 
00055 #include <QxSingleton/QxSingleton.h>
00056 
00057 #include <QxDao/QxSqlGenerator/IxSqlGenerator.h>
00058 
00059 #define QX_CONSTRUCT_QX_SQL_DATABASE() \
00060 m_iPort(-1), m_bTraceSqlQuery(true), m_bTraceSqlRecord(false), \
00061 m_bTraceSqlBoundValues(false), m_bTraceSqlBoundValuesOnError(true), \
00062 m_ePlaceHolderStyle(ph_style_2_point_name), m_bSessionThrowable(false), \
00063 m_bSessionAutoTransaction(true), m_bValidatorThrowable(false), \
00064 m_bAutoReplaceSqlAliasIntoQuery(true), m_bVerifyOffsetRelation(false), \
00065 m_bAddAutoIncrementIdToUpdateQuery(true), m_bForceParentIdToAllChildren(false)
00066 
00067 namespace qx {
00068 
00073 class QX_DLL_EXPORT QxSqlDatabase : public QxSingleton<QxSqlDatabase>
00074 {
00075 
00076    friend class QxSingleton<QxSqlDatabase>;
00077 
00078 public:
00079 
00080    enum ph_style { ph_style_question_mark, ph_style_2_point_name, ph_style_at_name };
00081 
00082 private:
00083 
00084    QHash<Qt::HANDLE, QString> m_lstDbByThread;              
00085    QMutex m_oDbMutex;                                       
00086    QString m_sDriverName;                                   
00087    QString m_sConnectOptions;                               
00088    QString m_sDatabaseName;                                 
00089    QString m_sUserName;                                     
00090    QString m_sPassword;                                     
00091    QString m_sHostName;                                     
00092    int m_iPort;                                             
00093    bool m_bTraceSqlQuery;                                   
00094    bool m_bTraceSqlRecord;                                  
00095    bool m_bTraceSqlBoundValues;                             
00096    bool m_bTraceSqlBoundValuesOnError;                      
00097    ph_style m_ePlaceHolderStyle;                            
00098    bool m_bSessionThrowable;                                
00099    bool m_bSessionAutoTransaction;                          
00100    bool m_bValidatorThrowable;                              
00101    qx::dao::detail::IxSqlGenerator_ptr m_pSqlGenerator;     
00102    bool m_bAutoReplaceSqlAliasIntoQuery;                    
00103    bool m_bVerifyOffsetRelation;                            
00104    bool m_bAddAutoIncrementIdToUpdateQuery;                 
00105    bool m_bForceParentIdToAllChildren;                      
00106 
00107 private:
00108 
00109    QxSqlDatabase() : QxSingleton<QxSqlDatabase>("qx::QxSqlDatabase"), QX_CONSTRUCT_QX_SQL_DATABASE() { ; }
00110    virtual ~QxSqlDatabase() { ; }
00111 
00112 public:
00113 
00114    QString getDriverName() const                   { return m_sDriverName; }
00115    QString getConnectOptions() const               { return m_sConnectOptions; }
00116    QString getDatabaseName() const                 { return m_sDatabaseName; }
00117    QString getUserName() const                     { return m_sUserName; }
00118    QString getPassword() const                     { return m_sPassword; }
00119    QString getHostName() const                     { return m_sHostName; }
00120    int getPort() const                             { return m_iPort; }
00121    bool getTraceSqlQuery() const                   { return m_bTraceSqlQuery; }
00122    bool getTraceSqlRecord() const                  { return m_bTraceSqlRecord; }
00123    bool getTraceSqlBoundValues() const             { return m_bTraceSqlBoundValues; }
00124    bool getTraceSqlBoundValuesOnError() const      { return m_bTraceSqlBoundValuesOnError; }
00125    ph_style getSqlPlaceHolderStyle() const         { return m_ePlaceHolderStyle; }
00126    bool getSessionThrowable() const                { return m_bSessionThrowable; }
00127    bool getSessionAutoTransaction() const          { return m_bSessionAutoTransaction; }
00128    bool getValidatorThrowable() const              { return m_bValidatorThrowable; }
00129    bool getAutoReplaceSqlAliasIntoQuery() const    { return m_bAutoReplaceSqlAliasIntoQuery; }
00130    bool getVerifyOffsetRelation() const            { return m_bVerifyOffsetRelation; }
00131    bool getAddAutoIncrementIdToUpdateQuery() const { return m_bAddAutoIncrementIdToUpdateQuery; }
00132    bool getForceParentIdToAllChildren() const      { return m_bForceParentIdToAllChildren; }
00133 
00134    void setDriverName(const QString & s)                          { m_sDriverName = s; getSqlGenerator(); }
00135    void setConnectOptions(const QString & s)                      { m_sConnectOptions = s; }
00136    void setDatabaseName(const QString & s)                        { m_sDatabaseName = s; }
00137    void setUserName(const QString & s)                            { m_sUserName = s; }
00138    void setPassword(const QString & s)                            { m_sPassword = s; }
00139    void setHostName(const QString & s)                            { m_sHostName = s; }
00140    void setPort(int i)                                            { m_iPort = i; }
00141    void setTraceSqlQuery(bool b)                                  { m_bTraceSqlQuery = b; }
00142    void setTraceSqlRecord(bool b)                                 { m_bTraceSqlRecord = b; }
00143    void setTraceSqlBoundValues(bool b)                            { m_bTraceSqlBoundValues = b; }
00144    void setTraceSqlBoundValuesOnError(bool b)                     { m_bTraceSqlBoundValuesOnError = b; }
00145    void setSqlPlaceHolderStyle(ph_style e)                        { m_ePlaceHolderStyle = e; }
00146    void setSessionThrowable(bool b)                               { m_bSessionThrowable = b; }
00147    void setSessionAutoTransaction(bool b)                         { m_bSessionAutoTransaction = b; }
00148    void setValidatorThrowable(bool b)                             { m_bValidatorThrowable = b; }
00149    void setSqlGenerator(qx::dao::detail::IxSqlGenerator_ptr p)    { m_pSqlGenerator = p; if (p) { p->init(); } }
00150    void setAutoReplaceSqlAliasIntoQuery(bool b)                   { m_bAutoReplaceSqlAliasIntoQuery = b; }
00151    void setVerifyOffsetRelation(bool b)                           { m_bVerifyOffsetRelation = b; }
00152    void setAddAutoIncrementIdToUpdateQuery(bool b)                { m_bAddAutoIncrementIdToUpdateQuery = b; }
00153    void setForceParentIdToAllChildren(bool b)                     { m_bForceParentIdToAllChildren = b; }
00154 
00155    static QSqlDatabase getDatabase();
00156    static QSqlDatabase getDatabase(QSqlError & dbError);
00157    static QSqlDatabase getDatabaseCloned();
00158    static void closeAllDatabases();
00159    static void clearAllDatabases();
00160 
00161    qx::dao::detail::IxSqlGenerator * getSqlGenerator();
00162 
00163 private:
00164 
00165    QSqlDatabase getDatabaseByCurrThreadId(QSqlError & dbError);
00166    QSqlDatabase createDatabase(QSqlError & dbError);
00167 
00168    void displayLastError(const QSqlDatabase & db, const QString & sDesc) const;
00169    QString formatLastError(const QSqlDatabase & db) const;
00170 
00171    bool isValid() const { return (! m_sDriverName.isEmpty() && ! m_sDatabaseName.isEmpty()); }
00172 
00173 };
00174 
00175 } // namespace qx
00176 
00177 QX_DLL_EXPORT_QX_SINGLETON_HPP(qx::QxSqlDatabase)
00178 
00179 #endif // _QX_SQL_DATABASE_H_