QxOrm  1.4.5
C++ Object Relational Mapping library
IxSqlQueryBuilder.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 _IX_SQL_QUERY_BUILDER_H_
00033 #define _IX_SQL_QUERY_BUILDER_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
00046 #include <QxDataMember/IxDataMemberX.h>
00047 
00048 #include <QxDao/IxSqlRelation.h>
00049 #include <QxDao/QxSoftDelete.h>
00050 #include <QxDao/QxSqlRelationLinked.h>
00051 
00052 namespace qx {
00053 namespace dao {
00054 namespace detail {
00055 
00056 class IxDao_Helper;
00057 
00058 } // namespace detail
00059 } // namespace dao
00060 } // namespace qx
00061 
00062 namespace qx {
00063 
00068 class QX_DLL_EXPORT IxSqlQueryBuilder
00069 {
00070 
00071 private:
00072 
00073    struct IxSqlQueryBuilderImpl;
00074    std::unique_ptr<IxSqlQueryBuilderImpl> m_pImpl; 
00075 
00076 public:
00077 
00078    IxSqlQueryBuilder();
00079    virtual ~IxSqlQueryBuilder() = 0;
00080 
00081    IxDataMemberX * getDataMemberX() const;
00082    QxCollection<QString, IxDataMember *> * getLstDataMember() const;
00083    IxSqlRelationX * getLstRelation() const;
00084    qx::dao::detail::IxDao_Helper * getDaoHelper() const;
00085    void setDaoHelper(qx::dao::detail::IxDao_Helper * p);
00086 
00087    void setHashRelation(const QString & s);
00088    void setCartesianProduct(bool b);
00089    QString getSqlQuery() const;
00090    QString getHashRelation() const;
00091    QString table() const;
00092    QxSoftDelete getSoftDelete() const;
00093    bool getCartesianProduct() const;
00094    long getDataCount() const;
00095    long getRelationCount() const;
00096    IxDataMember * getDataId() const;
00097    IxDataMember * nextData(long & l) const;
00098    IxSqlRelation * nextRelation(long & l) const;
00099 
00100    void initIdX(long lAllRelationCount);
00101    bool insertIdX(long lIndex, const QVariant & idOwner, const QVariant & idData, void * ptr);
00102    void * existIdX(long lIndex, const QVariant & idOwner, const QVariant & idData);
00103    void setSqlQuery(const QString & sql, const QString & key = QString());
00104    void addSqlQueryAlias(const QString & sql, const QString & sqlAlias);
00105    bool getAddAutoIncrementIdToUpdateQuery() const;
00106    void replaceSqlQueryAlias(QString & sql) const;
00107    void displaySqlQuery(int time_ms = -1, int time_db = -1, const QString & query = QString()) const;
00108 
00109    virtual void init();
00110    virtual void clone(const IxSqlQueryBuilder & other);
00111    virtual IxSqlQueryBuilder & buildSql(const QStringList & columns = QStringList(), QxSqlRelationLinked * pRelationX = NULL) = 0;
00112 
00113    static QString addSqlCondition(const QString & sql) { return (sql.contains(" WHERE ") ? " AND " : " WHERE "); }
00114 
00115    static void sql_CreateTable(QString & sql, IxSqlQueryBuilder & builder);
00116    static void sql_DeleteById(QString & sql, IxSqlQueryBuilder & builder, bool bSoftDelete);
00117    static void sql_Exist(QString & sql, IxSqlQueryBuilder & builder);
00118    static void sql_FetchAll(QString & sql, IxSqlQueryBuilder & builder);
00119    static void sql_FetchAll(QString & sql, IxSqlQueryBuilder & builder, const QStringList & columns);
00120    static void sql_FetchAll_WithRelation(qx::QxSqlRelationLinked * pRelationX, QString & sql, IxSqlQueryBuilder & builder);
00121    static void sql_FetchById(QString & sql, IxSqlQueryBuilder & builder);
00122    static void sql_FetchById(QString & sql, IxSqlQueryBuilder & builder, const QStringList & columns);
00123    static void sql_FetchById_WithRelation(qx::QxSqlRelationLinked * pRelationX, QString & sql, IxSqlQueryBuilder & builder);
00124    static void sql_Insert(QString & sql, IxSqlQueryBuilder & builder);
00125    static void sql_Update(QString & sql, IxSqlQueryBuilder & builder);
00126    static void sql_Update(QString & sql, IxSqlQueryBuilder & builder, const QStringList & columns);
00127 
00128    static void resolveOutput_FetchAll(void * t, QSqlQuery & query, IxSqlQueryBuilder & builder);
00129    static void resolveOutput_FetchAll(void * t, QSqlQuery & query, IxSqlQueryBuilder & builder, const QStringList & columns);
00130    static void resolveOutput_FetchAll_WithRelation(qx::QxSqlRelationLinked * pRelationX, void * t, QSqlQuery & query, IxSqlQueryBuilder & builder);
00131 
00132    static void resolveInput_Insert(void * t, QSqlQuery & query, IxSqlQueryBuilder & builder);
00133    static void resolveInput_Update(void * t, QSqlQuery & query, IxSqlQueryBuilder & builder);
00134    static void resolveInput_Update(void * t, QSqlQuery & query, IxSqlQueryBuilder & builder, const QStringList & columns);
00135 
00136 protected:
00137 
00138    bool verifyColumns(const QStringList & columns) const QX_USED;
00139 
00140    IxDataMember * isValid_DataMember(long lIndex) const;
00141    IxDataMember * isValid_SqlRelation(long lIndex) const;
00142 
00143    bool isInitDone() const;
00144    QxSoftDelete & softDelete();
00145    const QxSoftDelete & softDelete() const;
00146    void setSoftDelete(const QxSoftDelete & o);
00147    void setDataMemberX(IxDataMemberX * p);
00148    QHash<QString, QString> & listSqlQueryAlias();
00149    const QHash<QString, QString> & listSqlQueryAlias() const;
00150    static QHash<QString, QString> & listSqlQuery();
00151    static QHash<QString, QHash<QString, QString> > & listSqlAlias();
00152    static QMutex & getMutex();
00153 
00154 };
00155 
00156 typedef std::shared_ptr<IxSqlQueryBuilder> IxSqlQueryBuilder_ptr;
00157 
00158 } // namespace qx
00159 
00160 #include <QxDao/IxDao_Helper.h>
00161 
00162 #endif // _IX_SQL_QUERY_BUILDER_H_