QxOrm  1.5.0
C++ Object Relational Mapping library
QxSqlDatabase.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** https://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 namespace qx {
00060 namespace dao {
00061 namespace detail {
00062 
00063 class IxDao_Helper;
00064 
00065 } // namespace detail
00066 } // namespace dao
00067 } // namespace qx
00068 
00069 namespace qx {
00070 
00075 class QX_DLL_EXPORT QxSqlDatabase : public QxSingleton<QxSqlDatabase>
00076 {
00077 
00078    friend class QxSingleton<QxSqlDatabase>;
00079    friend class qx::dao::detail::IxDao_Helper;
00080 
00081 public:
00082 
00083    enum ph_style { ph_style_question_mark, ph_style_2_point_name, ph_style_at_name };
00084 
00085    typedef std::function<void (QSqlDatabase &)> type_fct_db_open;
00086 
00087 private:
00088 
00089    struct QxSqlDatabaseImpl;
00090    std::unique_ptr<QxSqlDatabaseImpl> m_pImpl; 
00091 
00092    QxSqlDatabase();
00093    virtual ~QxSqlDatabase();
00094 
00095 public:
00096 
00097    QString getDriverName() const;
00098    QString getConnectOptions() const;
00099    QString getDatabaseName() const;
00100    QString getUserName() const;
00101    QString getPassword() const;
00102    QString getHostName() const;
00103    int getPort() const;
00104    bool getTraceSqlQuery() const;
00105    bool getTraceSqlRecord() const;
00106    bool getTraceSqlBoundValues() const;
00107    bool getTraceSqlBoundValuesOnError() const;
00108    ph_style getSqlPlaceHolderStyle() const;
00109    bool getSessionThrowable() const;
00110    bool getSessionAutoTransaction() const;
00111    bool getValidatorThrowable() const;
00112    bool getAutoReplaceSqlAliasIntoQuery() const;
00113    bool getVerifyOffsetRelation() const;
00114    bool getAddAutoIncrementIdToUpdateQuery() const;
00115    bool getForceParentIdToAllChildren() const;
00116    type_fct_db_open getFctDatabaseOpen() const;
00117    bool getAddSqlSquareBracketsForTableName() const;
00118    bool getAddSqlSquareBracketsForColumnName() const;
00119    bool getFormatSqlQueryBeforeLogging() const;
00120    QStringList getSqlDelimiterForTableName() const;
00121    QStringList getSqlDelimiterForColumnName() const;
00122    QStringList getSqlDelimiterForTableNameAlias() const;
00123    QStringList getSqlDelimiterForColumnNameAlias() const;
00124    int getTraceSqlOnlySlowQueriesDatabase() const;
00125    int getTraceSqlOnlySlowQueriesTotal() const;
00126    bool getDisplayTimerDetails() const;
00127 
00128    void setDriverName(const QString & s, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00129    void setConnectOptions(const QString & s, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00130    void setDatabaseName(const QString & s, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00131    void setUserName(const QString & s, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00132    void setPassword(const QString & s, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00133    void setHostName(const QString & s, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00134    void setPort(int i, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00135    void setTraceSqlQuery(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00136    void setTraceSqlRecord(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00137    void setTraceSqlBoundValues(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00138    void setTraceSqlBoundValuesOnError(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00139    void setSqlPlaceHolderStyle(ph_style e, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00140    void setSessionThrowable(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00141    void setSessionAutoTransaction(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00142    void setValidatorThrowable(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00143    void setSqlGenerator(qx::dao::detail::IxSqlGenerator_ptr p, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00144    void setAutoReplaceSqlAliasIntoQuery(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00145    void setVerifyOffsetRelation(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00146    void setAddAutoIncrementIdToUpdateQuery(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00147    void setForceParentIdToAllChildren(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00148    void setFctDatabaseOpen(type_fct_db_open fct, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00149    void setAddSqlSquareBracketsForTableName(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00150    void setAddSqlSquareBracketsForColumnName(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00151    void setFormatSqlQueryBeforeLogging(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00152    void setSqlDelimiterForTableName(const QStringList & lst, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00153    void setSqlDelimiterForColumnName(const QStringList & lst, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00154    void setSqlDelimiterForTableNameAlias(const QStringList & lst, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00155    void setSqlDelimiterForColumnNameAlias(const QStringList & lst, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00156    void setTraceSqlOnlySlowQueriesDatabase(int i, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00157    void setTraceSqlOnlySlowQueriesTotal(int i, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00158    void setDisplayTimerDetails(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00159 
00160    static QSqlDatabase getDatabase();
00161    static QSqlDatabase getDatabase(QSqlError & dbError);
00162    static QSqlDatabase getDatabaseCloned();
00163    static QSqlDatabase checkDatabaseByThread();
00164    static void removeDatabaseByThread();
00165    static void closeAllDatabases();
00166    static void clearAllDatabases();
00167    static bool isEmpty();
00168 
00169    qx::dao::detail::IxSqlGenerator * getSqlGenerator();
00170 
00171    void clearAllSettingsForCurrentThread();
00172    void clearAllSettingsForDatabase(QSqlDatabase * p);
00173 
00174 protected:
00175 
00176    bool setCurrentDatabaseByThread(QSqlDatabase * p);
00177    void clearCurrentDatabaseByThread();
00178 
00179 };
00180 
00181 } // namespace qx
00182 
00183 QX_DLL_EXPORT_QX_SINGLETON_HPP(qx::QxSqlDatabase)
00184 
00185 #endif // _QX_SQL_DATABASE_H_