QxOrm  1.4.5
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    int getTraceSqlOnlySlowQueriesDatabase() const;
00123    int getTraceSqlOnlySlowQueriesTotal() const;
00124 
00125    void setDriverName(const QString & s, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00126    void setConnectOptions(const QString & s, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00127    void setDatabaseName(const QString & s, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00128    void setUserName(const QString & s, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00129    void setPassword(const QString & s, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00130    void setHostName(const QString & s, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00131    void setPort(int i, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00132    void setTraceSqlQuery(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00133    void setTraceSqlRecord(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00134    void setTraceSqlBoundValues(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00135    void setTraceSqlBoundValuesOnError(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00136    void setSqlPlaceHolderStyle(ph_style e, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00137    void setSessionThrowable(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00138    void setSessionAutoTransaction(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00139    void setValidatorThrowable(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00140    void setSqlGenerator(qx::dao::detail::IxSqlGenerator_ptr p, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00141    void setAutoReplaceSqlAliasIntoQuery(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00142    void setVerifyOffsetRelation(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00143    void setAddAutoIncrementIdToUpdateQuery(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00144    void setForceParentIdToAllChildren(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00145    void setFctDatabaseOpen(type_fct_db_open fct, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00146    void setAddSqlSquareBracketsForTableName(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00147    void setAddSqlSquareBracketsForColumnName(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00148    void setFormatSqlQueryBeforeLogging(bool b, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00149    void setSqlDelimiterForTableName(const QStringList & lst, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00150    void setSqlDelimiterForColumnName(const QStringList & lst, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00151    void setTraceSqlOnlySlowQueriesDatabase(int i, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00152    void setTraceSqlOnlySlowQueriesTotal(int i, bool bJustForCurrentThread = false, QSqlDatabase * pJustForThisDatabase = NULL);
00153 
00154    static QSqlDatabase getDatabase();
00155    static QSqlDatabase getDatabase(QSqlError & dbError);
00156    static QSqlDatabase getDatabaseCloned();
00157    static void closeAllDatabases();
00158    static void clearAllDatabases();
00159    static bool isEmpty();
00160 
00161    qx::dao::detail::IxSqlGenerator * getSqlGenerator();
00162 
00163    void clearAllSettingsForCurrentThread();
00164    void clearAllSettingsForDatabase(QSqlDatabase * p);
00165 
00166 protected:
00167 
00168    bool setCurrentDatabaseByThread(QSqlDatabase * p);
00169    void clearCurrentDatabaseByThread();
00170 
00171 };
00172 
00173 } // namespace qx
00174 
00175 QX_DLL_EXPORT_QX_SINGLETON_HPP(qx::QxSqlDatabase)
00176 
00177 #endif // _QX_SQL_DATABASE_H_