QxOrm  1.2.8
C++ Object Relational Mapping library
IxModel.h
Go to the documentation of this file.
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 #include <QtCore/qabstractitemmodel.h>
00047 
00048 #ifndef Q_MOC_RUN
00049 #include <QxRegister/IxClass.h>
00050 #include <QxDataMember/IxDataMemberX.h>
00051 #include <QxCollection/QxCollection.h>
00052 #include <QxDao/IxSqlRelation.h>
00053 #include <QxDao/QxSqlQuery.h>
00054 #include <QxDao/QxDao.h>
00055 #include <QxValidator/QxInvalidValueX.h>
00056 #include <QxValidator/QxValidatorFct.h>
00057 #endif // Q_MOC_RUN
00058 
00059 namespace qx {
00060 
00144 class QX_DLL_EXPORT IxModel : public QAbstractItemModel
00145 {
00146 
00147    Q_OBJECT
00148 
00149 public:
00150 
00151    typedef QHash<QString, IxModel *> type_relation_by_name;
00152    typedef QList<type_relation_by_name> type_lst_relation_by_name;
00153 
00154 protected:
00155 
00156    IxClass * m_pClass;                          
00157    IxDataMemberX * m_pDataMemberX;              
00158    IxDataMember * m_pDataMemberId;              
00159    IxCollection * m_pCollection;                
00160    QHash<int, QByteArray> m_lstRoleNames;       
00161    QList<IxDataMember *> m_lstDataMember;       
00162    QHash<QString, int> m_lstDataMemberByKey;    
00163    QHash<QString, QString> m_lstHeaders;        
00164    QStringList m_lstColumns;                    
00165    QSqlDatabase m_database;                     
00166    QSqlError m_lastError;                       
00167    IxModel * m_pParent;                         
00168    type_lst_relation_by_name m_lstChild;        
00169 
00170 public:
00171 
00172    IxModel(QObject * parent = 0);
00173    virtual ~IxModel();
00174 
00175    IxClass * getClass() const;
00176    IxCollection * getCollection() const;
00177    QSqlDatabase getDatabase() const;
00178    QSqlError getLastError() const;
00179    Q_INVOKABLE QString getLastErrorAsString() const;
00180    Q_INVOKABLE QStringList getListOfColumns() const;
00181    QHash<QString, QString> getListOfHeaders() const;
00182    IxDataMember * getDataMember(int column) const;
00183    Q_INVOKABLE QString getDataMemberKey(int column) const;
00184    Q_INVOKABLE QVariant getModelValue(int row, const QString & column) const;
00185 
00186    void setDatabase(const QSqlDatabase & db);
00187    Q_INVOKABLE void setListOfColumns(const QStringList & lst);
00188    void setListOfHeaders(const QHash<QString, QString> & lst);
00189    Q_INVOKABLE bool setModelValue(int row, const QString & column, const QVariant & value);
00190    void setParentModel(IxModel * pParent);
00191 
00192    virtual long qxCount(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL) = 0;
00193    virtual QSqlError qxCount(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL) = 0;
00194    virtual QSqlError qxFetchById(const QVariant & id, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00195    virtual QSqlError qxFetchAll(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00196    virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00197    virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00198    virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00199    virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00200    virtual QSqlError qxSaveRow(int row, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL) = 0;
00201    virtual QSqlError qxDeleteById(const QVariant & id, QSqlDatabase * pDatabase = NULL) = 0;
00202    virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL) = 0;
00203    virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0;
00204    virtual QSqlError qxDestroyById(const QVariant & id, QSqlDatabase * pDatabase = NULL) = 0;
00205    virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL) = 0;
00206    virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0;
00207    virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL) = 0;
00208    virtual qx_bool qxExist(const QVariant & id, QSqlDatabase * pDatabase = NULL) = 0;
00209    virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList()) = 0;
00210 
00211    Q_INVOKABLE int qxCount_(const QString & sQuery);
00212    Q_INVOKABLE bool qxFetchById_(const QVariant & id, const QStringList & relation = QStringList());
00213    Q_INVOKABLE bool qxFetchAll_(const QStringList & relation = QStringList());
00214    Q_INVOKABLE bool qxFetchByQuery_(const QString & sQuery, const QStringList & relation = QStringList());
00215    Q_INVOKABLE bool qxInsert_(const QStringList & relation = QStringList());
00216    Q_INVOKABLE bool qxUpdate_(const QString & sQuery, const QStringList & relation = QStringList());
00217    Q_INVOKABLE bool qxSave_(const QStringList & relation = QStringList());
00218    Q_INVOKABLE bool qxSaveRow_(int row, const QStringList & relation = QStringList());
00219    Q_INVOKABLE bool qxDeleteById_(const QVariant & id);
00220    Q_INVOKABLE bool qxDeleteAll_();
00221    Q_INVOKABLE bool qxDeleteByQuery_(const QString & sQuery);
00222    Q_INVOKABLE bool qxDestroyById_(const QVariant & id);
00223    Q_INVOKABLE bool qxDestroyAll_();
00224    Q_INVOKABLE bool qxDestroyByQuery_(const QString & sQuery);
00225    Q_INVOKABLE bool qxExecuteQuery_(const QString & sQuery);
00226    Q_INVOKABLE bool qxExist_(const QVariant & id);
00227    Q_INVOKABLE QString qxValidate_(const QStringList & groups = QStringList());
00228 
00229 protected:
00230 
00231    void raiseEvent_headerDataChanged(Qt::Orientation orientation, int first, int last);
00232 #if (QT_VERSION >= 0x050000)
00233    void raiseEvent_dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight, const QVector<int> & roles = QVector<int>());
00234    void raiseEvent_layoutAboutToBeChanged(const QList<QPersistentModelIndex> & parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint);
00235    void raiseEvent_layoutChanged(const QList<QPersistentModelIndex> & parents = QList<QPersistentModelIndex>(), QAbstractItemModel::LayoutChangeHint hint = QAbstractItemModel::NoLayoutChangeHint);
00236 #else // (QT_VERSION >= 0x050000)
00237    void raiseEvent_dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight);
00238    void raiseEvent_layoutAboutToBeChanged();
00239    void raiseEvent_layoutChanged();
00240 #endif // (QT_VERSION >= 0x050000)
00241 
00242 public:
00243 
00244    Q_INVOKABLE void clear(bool bUpdateColumns = false);
00245 
00246    virtual int rowCount(const QModelIndex & parent = QModelIndex()) const;
00247    virtual int columnCount(const QModelIndex & parent = QModelIndex()) const;
00248    virtual QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const;
00249    virtual QModelIndex parent(const QModelIndex & index) const;
00250    virtual bool hasChildren(const QModelIndex & parent = QModelIndex()) const;
00251    virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
00252    virtual Qt::ItemFlags flags(const QModelIndex & index) const;
00253    virtual Qt::DropActions supportedDropActions() const;
00254    virtual bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex());
00255    virtual bool setHeaderData(int section, Qt::Orientation orientation, const QVariant & value, int role = Qt::EditRole);
00256 
00257 #if (QT_VERSION >= 0x050000)
00258    virtual QHash<int, QByteArray> roleNames() const;
00259    virtual Qt::DropActions supportedDragActions() const;
00260 #endif // (QT_VERSION >= 0x050000)
00261 
00262 protected:
00263 
00264    void generateRoleNames();
00265    QSqlDatabase * database(QSqlDatabase * other);
00266    IxModel * getChild(long row, const QString & relation);
00267    void insertChild(long row, const QString & relation, IxModel * pChild);
00268    void removeListOfChild(long row);
00269 
00270 };
00271 
00272 } // namespace qx
00273 
00274 #endif // _IX_MODEL_H_