![]() |
QxOrm
1.4.3
C++ Object Relational Mapping library
|
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 _IX_MODEL_H_ 00033 #define _IX_MODEL_H_ 00034 00035 #ifdef _MSC_VER 00036 #pragma once 00037 #endif 00038 00046 #ifdef _QX_NO_PRECOMPILED_HEADER 00047 #ifndef Q_MOC_RUN 00048 #include <QxPrecompiled.h> // Need to include precompiled header for the generated moc file 00049 #endif // Q_MOC_RUN 00050 #endif // _QX_NO_PRECOMPILED_HEADER 00051 00052 #include <QtCore/qabstractitemmodel.h> 00053 00054 #ifndef Q_MOC_RUN 00055 #include <QxRegister/IxClass.h> 00056 #include <QxDataMember/IxDataMemberX.h> 00057 #include <QxCollection/QxCollection.h> 00058 #include <QxDao/IxSqlRelation.h> 00059 #include <QxDao/QxSqlQuery.h> 00060 #include <QxDao/QxDao.h> 00061 #include <QxValidator/QxInvalidValueX.h> 00062 #include <QxValidator/QxValidatorFct.h> 00063 #endif // Q_MOC_RUN 00064 00065 namespace qx { 00066 00150 class QX_DLL_EXPORT IxModel : public QAbstractItemModel 00151 { 00152 00153 Q_OBJECT 00154 00155 public: 00156 00157 enum e_auto_update_database { e_no_auto_update, e_auto_update_on_field_change }; 00158 00159 typedef QHash<QString, IxModel *> type_relation_by_name; 00160 typedef QList<type_relation_by_name> type_lst_relation_by_name; 00161 00162 protected: 00163 00164 IxClass * m_pClass; 00165 IxDataMemberX * m_pDataMemberX; 00166 IxDataMember * m_pDataMemberId; 00167 IxCollection * m_pCollection; 00168 QHash<int, QByteArray> m_lstRoleNames; 00169 QList<IxDataMember *> m_lstDataMember; 00170 QHash<QString, int> m_lstDataMemberByKey; 00171 QHash<QString, QVariant> m_lstHeadersData; 00172 QStringList m_lstColumns; 00173 QSqlDatabase m_database; 00174 QSqlError m_lastError; 00175 IxModel * m_pParent; 00176 type_lst_relation_by_name m_lstChild; 00177 e_auto_update_database m_eAutoUpdateDatabase; 00178 00179 public: 00180 00181 IxModel(QObject * parent = 0); 00182 virtual ~IxModel(); 00183 00184 IxClass * getClass() const; 00185 IxCollection * getCollection() const; 00186 QSqlDatabase getDatabase() const; 00187 QSqlError getLastError() const; 00188 Q_INVOKABLE QString getLastErrorAsString() const; 00189 Q_INVOKABLE QStringList getListOfColumns() const; 00190 QHash<QString, QString> getListOfHeaders() const; 00191 IxDataMember * getDataMember(int column) const; 00192 Q_INVOKABLE QString getDataMemberKey(int column) const; 00193 Q_INVOKABLE int getRowCount() const; 00194 Q_INVOKABLE QVariant getModelValue(int row, const QString & column) const; 00195 Q_INVOKABLE int getColumnIndex(const QString & sColumnName) const; 00196 Q_INVOKABLE int getAutoUpdateDatabase_() const; 00197 e_auto_update_database getAutoUpdateDatabase() const; 00198 Q_INVOKABLE void dumpModel(bool bJsonFormat = true) const; 00199 Q_INVOKABLE QObject * cloneModel(); 00200 00201 void setDatabase(const QSqlDatabase & db); 00202 Q_INVOKABLE void setListOfColumns(const QStringList & lst); 00203 void setListOfHeaders(const QHash<QString, QString> & lst); 00204 Q_INVOKABLE bool setModelValue(int row, const QString & column, const QVariant & value); 00205 void setParentModel(IxModel * pParent); 00206 Q_INVOKABLE void setAutoUpdateDatabase_(int i); 00207 void setAutoUpdateDatabase(e_auto_update_database e); 00208 00209 Q_INVOKABLE QString toJson(int row = -1) const; 00210 Q_INVOKABLE bool fromJson(const QString & json, int row = -1); 00211 00212 Q_INVOKABLE QVariant getRelationshipValues(int row, const QString & relation, bool bLoadFromDatabase = false, const QString & sAppendRelations = QString()); 00213 Q_INVOKABLE bool setRelationshipValues(int row, const QString & relation, const QVariant & values); 00214 00215 virtual long qxCount(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL) = 0; 00216 virtual QSqlError qxCount(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL) = 0; 00217 virtual QSqlError qxFetchById(const QVariant & id, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0; 00218 virtual QSqlError qxFetchAll(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0; 00219 virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0; 00220 virtual QSqlError qxFetchRow(int row, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0; 00221 virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0; 00222 virtual QSqlError qxInsertRow(int row, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0; 00223 virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0; 00224 virtual QSqlError qxUpdateRow(int row, const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0; 00225 virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0; 00226 virtual QSqlError qxSaveRow(int row, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0; 00227 virtual QSqlError qxDeleteById(const QVariant & id, QSqlDatabase * pDatabase = NULL) = 0; 00228 virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL) = 0; 00229 virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0; 00230 virtual QSqlError qxDeleteRow(int row, QSqlDatabase * pDatabase = NULL) = 0; 00231 virtual QSqlError qxDestroyById(const QVariant & id, QSqlDatabase * pDatabase = NULL) = 0; 00232 virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL) = 0; 00233 virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0; 00234 virtual QSqlError qxDestroyRow(int row, QSqlDatabase * pDatabase = NULL) = 0; 00235 virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0; 00236 virtual qx_bool qxExist(const QVariant & id, QSqlDatabase * pDatabase = NULL) = 0; 00237 virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList()) = 0; 00238 virtual qx::QxInvalidValueX qxValidateRow(int row, const QStringList & groups = QStringList()) = 0; 00239 00240 Q_INVOKABLE int qxCount_(const QString & sQuery); 00241 Q_INVOKABLE bool qxFetchById_(const QVariant & id, const QStringList & relation = QStringList()); 00242 Q_INVOKABLE bool qxFetchAll_(const QStringList & relation = QStringList()); 00243 Q_INVOKABLE bool qxFetchByQuery_(const QString & sQuery, const QStringList & relation = QStringList()); 00244 Q_INVOKABLE bool qxFetchRow_(int row, const QStringList & relation = QStringList()); 00245 Q_INVOKABLE bool qxInsert_(const QStringList & relation = QStringList()); 00246 Q_INVOKABLE bool qxInsertRow_(int row, const QStringList & relation = QStringList()); 00247 Q_INVOKABLE bool qxUpdate_(const QString & sQuery, const QStringList & relation = QStringList()); 00248 Q_INVOKABLE bool qxUpdateRow_(int row, const QString & sQuery, const QStringList & relation = QStringList()); 00249 Q_INVOKABLE bool qxSave_(const QStringList & relation = QStringList()); 00250 Q_INVOKABLE bool qxSaveRow_(int row, const QStringList & relation = QStringList()); 00251 Q_INVOKABLE bool qxDeleteById_(const QVariant & id); 00252 Q_INVOKABLE bool qxDeleteAll_(); 00253 Q_INVOKABLE bool qxDeleteByQuery_(const QString & sQuery); 00254 Q_INVOKABLE bool qxDeleteRow_(int row); 00255 Q_INVOKABLE bool qxDestroyById_(const QVariant & id); 00256 Q_INVOKABLE bool qxDestroyAll_(); 00257 Q_INVOKABLE bool qxDestroyByQuery_(const QString & sQuery); 00258 Q_INVOKABLE bool qxDestroyRow_(int row); 00259 Q_INVOKABLE bool qxExecuteQuery_(const QString & sQuery); 00260 Q_INVOKABLE bool qxExist_(const QVariant & id); 00261 Q_INVOKABLE QString qxValidate_(const QStringList & groups = QStringList()); 00262 Q_INVOKABLE QString qxValidateRow_(int row, const QStringList & groups = QStringList()); 00263 00264 protected: 00265 00266 void raiseEvent_headerDataChanged(Qt::Orientation orientation, int first, int last); 00267 #if (QT_VERSION >= 0x050000) 00268 void raiseEvent_dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight, const QVector<int> & roles = QVector<int>()); 00269 void raiseEvent_layoutAboutToBeChanged(const QList<QPersistentModelIndex> & parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint); 00270 void raiseEvent_layoutChanged(const QList<QPersistentModelIndex> & parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint); 00271 #else // (QT_VERSION >= 0x050000) 00272 void raiseEvent_dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight); 00273 void raiseEvent_layoutAboutToBeChanged(); 00274 void raiseEvent_layoutChanged(); 00275 #endif // (QT_VERSION >= 0x050000) 00276 00277 public: 00278 00279 Q_INVOKABLE void clear(bool bUpdateColumns = false); 00280 00281 virtual int rowCount(const QModelIndex & parent = QModelIndex()) const; 00282 virtual int columnCount(const QModelIndex & parent = QModelIndex()) const; 00283 virtual QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const; 00284 virtual QModelIndex parent(const QModelIndex & index) const; 00285 virtual bool hasChildren(const QModelIndex & parent = QModelIndex()) const; 00286 virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; 00287 virtual Qt::ItemFlags flags(const QModelIndex & index) const; 00288 virtual Qt::DropActions supportedDropActions() const; 00289 virtual bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex()); 00290 virtual bool setHeaderData(int section, Qt::Orientation orientation, const QVariant & value, int role = Qt::EditRole); 00291 bool setHeaderData(const QString & sColumnName, const QVariant & value, int role = Qt::EditRole); 00292 00293 #if (QT_VERSION >= 0x050000) 00294 virtual QHash<int, QByteArray> roleNames() const; 00295 virtual Qt::DropActions supportedDragActions() const; 00296 #endif // (QT_VERSION >= 0x050000) 00297 00298 protected: 00299 00300 virtual QObject * cloneModelImpl() = 0; 00301 virtual void dumpModelImpl(bool bJsonFormat) const = 0; 00302 virtual void syncNestedModel(int row, const QStringList & relation); 00303 virtual void syncAllNestedModel(const QStringList & relation); 00304 void syncNestedModelRecursive(IxModel * pNestedModel, const QStringList & relation); 00305 00306 void generateRoleNames(); 00307 QSqlDatabase * database(QSqlDatabase * other); 00308 IxModel * getChild(long row, const QString & relation); 00309 void insertChild(long row, const QString & relation, IxModel * pChild); 00310 void removeListOfChild(long row); 00311 00312 #ifndef _QX_NO_JSON 00313 00314 virtual QString toJson_Helper(int row) const = 0; 00315 virtual bool fromJson_Helper(const QString & json, int row) = 0; 00316 00317 virtual QVariant getRelationshipValues_Helper(int row, const QString & relation, bool bLoadFromDatabase, const QString & sAppendRelations) = 0; 00318 virtual bool setRelationshipValues_Helper(int row, const QString & relation, const QVariant & values) = 0; 00319 00320 #endif // _QX_NO_JSON 00321 00322 }; 00323 00324 } // namespace qx 00325 00326 #endif // _IX_MODEL_H_