![]() |
QxOrm 1.1.8
C++ Object Relational Mapping library
|
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 _IX_SQL_QUERY_BUILDER_H_ 00027 #define _IX_SQL_QUERY_BUILDER_H_ 00028 00029 #ifdef _MSC_VER 00030 #pragma once 00031 #endif 00032 00040 #include <QxDataMember/IxDataMemberX.h> 00041 00042 #include <QxDao/IxSqlRelation.h> 00043 #include <QxDao/QxSoftDelete.h> 00044 00045 namespace qx { 00046 00051 class QX_DLL_EXPORT IxSqlQueryBuilder 00052 { 00053 00054 public: 00055 00056 typedef QPair<QString, QString> type_id; 00057 typedef QHash<type_id, void *> type_ptr_by_id; 00058 typedef boost::shared_ptr<type_ptr_by_id> type_ptr_by_id_ptr; 00059 typedef QList<type_ptr_by_id_ptr> type_lst_ptr_by_id; 00060 typedef boost::shared_ptr<type_lst_ptr_by_id> type_lst_ptr_by_id_ptr; 00061 00062 protected: 00063 00064 QxCollection<QString, IxDataMember *> * m_lstDataMemberPtr; 00065 IxSqlRelationX * m_lstSqlRelationPtr; 00066 IxDataMember * m_pDataMemberId; 00067 QString m_sSqlQuery; 00068 QString m_sTableName; 00069 QString m_sHashRelation; 00070 bool m_bCartesianProduct; 00071 type_lst_ptr_by_id_ptr m_pIdX; 00072 QxSoftDelete m_oSoftDelete; 00073 00074 public: 00075 00076 IxSqlQueryBuilder() : m_lstDataMemberPtr(NULL), m_lstSqlRelationPtr(NULL), m_pDataMemberId(NULL), m_bCartesianProduct(false) { ; } 00077 IxSqlQueryBuilder(const QString & sql) : m_lstDataMemberPtr(NULL), m_lstSqlRelationPtr(NULL), m_pDataMemberId(NULL), m_sSqlQuery(sql), m_bCartesianProduct(false) { ; } 00078 virtual ~IxSqlQueryBuilder() = 0; 00079 00080 inline QxCollection<QString, IxDataMember *> * getLstDataMember() const { return m_lstDataMemberPtr; } 00081 inline IxSqlRelationX * getLstRelation() const { return m_lstSqlRelationPtr; } 00082 00083 inline void setSqlQuery(const QString & sql) { m_sSqlQuery = sql; } 00084 inline void setHashRelation(const QString & s) { m_sHashRelation = s; } 00085 inline void setCartesianProduct(bool b) { m_bCartesianProduct = b; } 00086 inline QString getSqlQuery() const { return m_sSqlQuery; } 00087 inline QString getHashRelation() const { return m_sHashRelation; } 00088 inline QString table() const { return m_sTableName; } 00089 inline QxSoftDelete getSoftDelete() const { return m_oSoftDelete; } 00090 inline bool getCartesianProduct() const { return m_bCartesianProduct; } 00091 inline long getDataCount() const { return (m_lstDataMemberPtr ? m_lstDataMemberPtr->count() : 0); } 00092 inline long getRelationCount() const { return (m_lstSqlRelationPtr ? m_lstSqlRelationPtr->count() : 0); } 00093 inline IxDataMember * getDataId() const { return m_pDataMemberId; } 00094 inline IxDataMember * nextData(long & l) const { if ((! m_lstDataMemberPtr) || (l < 0) || (l >= m_lstDataMemberPtr->count())) { return NULL; }; ++l; return m_lstDataMemberPtr->getByIndex(l - 1); } 00095 inline IxSqlRelation * nextRelation(long & l) const { if ((! m_lstSqlRelationPtr) || (l < 0) || (l >= m_lstSqlRelationPtr->count())) { return NULL; }; ++l; return m_lstSqlRelationPtr->getByIndex(l - 1); } 00096 00097 void displaySqlQuery(int time_ms = -1) const; 00098 bool insertIdX(long lIndex, const QVariant & idOwner, const QVariant & idData, void * ptr); 00099 void * existIdX(long lIndex, const QVariant & idOwner, const QVariant & idData); 00100 00101 virtual void init() = 0; 00102 virtual IxDataMemberX * getDataMemberX() const = 0; 00103 00104 virtual IxSqlQueryBuilder & count() = 0; 00105 virtual IxSqlQueryBuilder & exist() = 0; 00106 virtual IxSqlQueryBuilder & fetchAll() = 0; 00107 virtual IxSqlQueryBuilder & fetchById() = 0; 00108 virtual IxSqlQueryBuilder & insert() = 0; 00109 virtual IxSqlQueryBuilder & update() = 0; 00110 virtual IxSqlQueryBuilder & deleteAll() = 0; 00111 virtual IxSqlQueryBuilder & softDeleteAll() = 0; 00112 virtual IxSqlQueryBuilder & deleteById() = 0; 00113 virtual IxSqlQueryBuilder & softDeleteById() = 0; 00114 virtual IxSqlQueryBuilder & createTable() = 0; 00115 00116 virtual IxSqlQueryBuilder & fetchAll(const QStringList & columns) = 0; 00117 virtual IxSqlQueryBuilder & fetchById(const QStringList & columns) = 0; 00118 virtual IxSqlQueryBuilder & update(const QStringList & columns) = 0; 00119 00120 virtual IxSqlQueryBuilder & fetchAll_WithRelation(IxSqlRelationX * pRelationX) = 0; 00121 virtual IxSqlQueryBuilder & fetchById_WithRelation(IxSqlRelationX * pRelationX) = 0; 00122 00123 static QString addSqlCondition(const QString & sql) { return (sql.contains(" WHERE ") ? " AND " : " WHERE "); } 00124 00125 private: 00126 00127 void initIdX(); 00128 00129 }; 00130 00131 typedef boost::shared_ptr<IxSqlQueryBuilder> IxSqlQueryBuilder_ptr; 00132 00133 } // namespace qx 00134 00135 #endif // _IX_SQL_QUERY_BUILDER_H_