QxOrm  1.2.9
C++ Object Relational Mapping library
QxModel.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 _QX_MODEL_H_
00033 #define _QX_MODEL_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
00046 #include <QxModelView/IxModel.h>
00047 
00048 #include <QxCollection/QxCollection.h>
00049 
00050 #include <QxRegister/QxClass.h>
00051 
00052 #include <QxTraits/get_primary_key.h>
00053 #include <QxTraits/is_qx_registered.h>
00054 
00055 namespace qx {
00056 namespace model_view {
00057 namespace detail {
00058 
00059 template <class T> struct QxNestedModel;
00060 template <class T> struct QxNestedModel_Generic;
00061 template <class T> struct QxNestedModel_Container;
00062 
00063 } // namespace detail
00064 } // namespace model_view
00065 } // namespace qx
00066 
00067 namespace qx {
00068 
00152 template <class T>
00153 class QxModel : public qx::IxModel
00154 {
00155 
00156    friend struct qx::model_view::detail::QxNestedModel<T>;
00157    friend struct qx::model_view::detail::QxNestedModel_Generic<T>;
00158    template <typename U> friend struct qx::model_view::detail::QxNestedModel_Container;
00159 
00160 public:
00161 
00162    typedef boost::shared_ptr<T> type_ptr;
00163    typedef typename qx::trait::get_primary_key<T>::type type_primary_key;
00164    typedef qx::QxCollection<type_primary_key, type_ptr> type_collection;
00165 
00166    enum { qx_is_valid = qx::trait::is_qx_registered<T>::value };
00167 
00168 protected:
00169 
00170    type_collection m_model;      
00171    long m_lManualInsertIndex;    
00172 
00173 public:
00174 
00175    QxModel(QObject * parent = 0) : qx::IxModel(parent), m_lManualInsertIndex(0) { qx::QxModel<T>::init(); }
00176    QxModel(qx::IxModel * other, QObject * parent) : qx::IxModel(parent), m_lManualInsertIndex(0) { qx::QxModel<T>::initFrom(other); }
00177    virtual ~QxModel() { ; }
00178 
00179 protected:
00180 
00181    void init()
00182    {
00183       BOOST_STATIC_ASSERT(qx_is_valid);
00184       m_pClass = qx::QxClass<T>::getSingleton(); qAssert(m_pClass != NULL);
00185       m_pDataMemberX = (m_pClass ? m_pClass->getDataMemberX() : NULL); qAssert(m_pDataMemberX != NULL);
00186       m_pDataMemberId = (m_pDataMemberX ? m_pDataMemberX->getId_WithDaoStrategy() : NULL);
00187       m_pCollection = (& m_model);
00188       generateRoleNames();
00189    }
00190 
00191    void initFrom(qx::IxModel * pOther)
00192    {
00193       init();
00194       qx::QxModel<T> * pOtherWrk = static_cast<qx::QxModel<T> *>(pOther);
00195       this->m_model = pOtherWrk->m_model;
00196       this->m_lManualInsertIndex = pOtherWrk->m_lManualInsertIndex;
00197       this->m_pParent = pOtherWrk->m_pParent;
00198    }
00199 
00200 public:
00201 
00202    virtual QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const
00203    {
00204       if (! index.isValid()) { return QVariant(); }
00205       if ((role == Qt::DisplayRole) || (role == Qt::EditRole))
00206       {
00207          if ((index.column() < 0) || (index.column() >= m_lstDataMember.count())) { return QVariant(); }
00208          else if ((index.row() < 0) || (index.row() >= m_model.count())) { return QVariant(); }
00209          IxDataMember * pDataMember = m_lstDataMember.at(index.column());
00210          type_ptr pItem = m_model.getByIndex(index.row());
00211          if (! pDataMember || ! pItem) { return QVariant(); }
00212          return pDataMember->toVariant(pItem.get());
00213       }
00214       else if (role >= (Qt::UserRole + 1))
00215       {
00216          QModelIndex idx = this->index(index.row(), (role - Qt::UserRole - 1), QModelIndex());
00217          return data(idx, Qt::DisplayRole);
00218       }
00219       return QVariant();
00220    }
00221 
00222    virtual bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole)
00223    {
00224       if (! index.isValid()) { return false; }
00225       if (role == Qt::EditRole)
00226       {
00227          if ((index.column() < 0) || (index.column() >= m_lstDataMember.count())) { return false; }
00228          else if ((index.row() < 0) || (index.row() >= m_model.count())) { return false; }
00229          IxDataMember * pDataMember = m_lstDataMember.at(index.column());
00230          type_ptr pItem = m_model.getByIndex(index.row());
00231          if (! pDataMember || ! pItem) { return false; }
00232          QVariant vCurrentValue = pDataMember->toVariant(pItem.get());
00233          if (vCurrentValue == value) { return true; }
00234          qx_bool bSetData = pDataMember->fromVariant(pItem.get(), value);
00235          if (bSetData) { raiseEvent_dataChanged(index, index); }
00236          return bSetData;
00237       }
00238       else if (role >= (Qt::UserRole + 1))
00239       {
00240          QModelIndex idx = this->index(index.row(), (role - Qt::UserRole - 1), QModelIndex());
00241          return setData(idx, value, Qt::EditRole);
00242       }
00243       return false;
00244    }
00245 
00246    virtual bool insertRows(int row, int count, const QModelIndex & parent = QModelIndex())
00247    {
00248       if (parent.isValid()) { return false; }
00249       beginInsertRows(QModelIndex(), row, (row + count - 1));
00250       for (int i = 0; i < count; ++i)
00251       {
00252          m_lManualInsertIndex--;
00253          type_primary_key primaryKey;
00254          QVariant vNewId(static_cast<qlonglong>(m_lManualInsertIndex));
00255          qx::cvt::from_variant(vNewId, primaryKey);
00256          type_ptr pItem = type_ptr(new T());
00257          m_model.insert(row, primaryKey, pItem);
00258       }
00259       endInsertRows();
00260       return true;
00261    }
00262 
00263 public:
00264 
00265    virtual long qxCount(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL)
00266    {
00267       return qx::dao::count<T>(query, database(pDatabase));
00268    }
00269 
00270    virtual QSqlError qxCount(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL)
00271    {
00272       m_lastError = qx::dao::count<T>(lCount, query, database(pDatabase));
00273       return m_lastError;
00274    }
00275 
00276    virtual QSqlError qxFetchById(const QVariant & id, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
00277    {
00278       clear();
00279       type_ptr pItem = type_ptr(new T());
00280       if (! m_pDataMemberId) { qDebug("[QxOrm] problem with 'qxFetchById()' method : '%s'", "data member id not registered"); qAssert(false); }
00281       if (! m_pDataMemberId) { m_lastError = QSqlError("[QxOrm] problem with 'qxFetchById()' method : 'data member id not registered'", "", QSqlError::UnknownError); return m_lastError; }
00282       m_pDataMemberId->fromVariant(pItem.get(), id);
00283 
00284       type_primary_key primaryKey;
00285       qx::cvt::from_variant(id, primaryKey);
00286       beginInsertRows(QModelIndex(), 0, 0);
00287       m_model.insert(primaryKey, pItem);
00288 
00289       if (relation.count() == 0) { m_lastError = qx::dao::fetch_by_id((* pItem), database(pDatabase), m_lstColumns); }
00290       else { m_lastError = qx::dao::fetch_by_id_with_relation(relation, (* pItem), database(pDatabase)); }
00291       endInsertRows();
00292       return m_lastError;
00293    }
00294 
00295    virtual QSqlError qxFetchAll(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
00296    {
00297       clear();
00298       type_collection tmp;
00299       if (relation.count() == 0) { m_lastError = qx::dao::fetch_all(tmp, database(pDatabase), m_lstColumns); }
00300       else { m_lastError = qx::dao::fetch_all_with_relation(relation, tmp, database(pDatabase)); }
00301 
00302       beginInsertRows(QModelIndex(), 0, (tmp.count() - 1));
00303       m_model = tmp;
00304       endInsertRows();
00305       return m_lastError;
00306    }
00307 
00308    virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
00309    {
00310       clear();
00311       type_collection tmp;
00312       if (relation.count() == 0) { m_lastError = qx::dao::fetch_by_query(query, tmp, database(pDatabase), m_lstColumns); }
00313       else { m_lastError = qx::dao::fetch_by_query_with_relation(relation, query, tmp, database(pDatabase)); }
00314 
00315       beginInsertRows(QModelIndex(), 0, (tmp.count() - 1));
00316       m_model = tmp;
00317       endInsertRows();
00318       return m_lastError;
00319    }
00320 
00321    virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
00322    {
00323       if (relation.count() == 0) { m_lastError = qx::dao::insert(m_model, database(pDatabase)); }
00324       else { m_lastError = qx::dao::insert_with_relation(relation, m_model, database(pDatabase)); }
00325       return m_lastError;
00326    }
00327 
00328    virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
00329    {
00330       if (relation.count() == 0) { m_lastError = qx::dao::update_by_query(query, m_model, database(pDatabase), m_lstColumns); }
00331       else { m_lastError = qx::dao::update_by_query_with_relation(relation, query, m_model, database(pDatabase)); }
00332       return m_lastError;
00333    }
00334 
00335    virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
00336    {
00337       if (relation.count() == 0) { m_lastError = qx::dao::save(m_model, database(pDatabase)); }
00338       else { m_lastError = qx::dao::save_with_relation(relation, m_model, database(pDatabase)); }
00339       return m_lastError;
00340    }
00341 
00342    virtual QSqlError qxSaveRow(int row, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
00343    {
00344       if ((row < 0) || (row >= m_model.count())) { return QSqlError(); }
00345       type_ptr pItem = m_model.getByIndex(row); if (! pItem) { return QSqlError(); }
00346       if (relation.count() == 0) { m_lastError = qx::dao::save((* pItem), database(pDatabase)); }
00347       else { m_lastError = qx::dao::save_with_relation(relation, (* pItem), database(pDatabase)); }
00348       return m_lastError;
00349    }
00350 
00351    virtual QSqlError qxDeleteById(const QVariant & id, QSqlDatabase * pDatabase = NULL)
00352    {
00353       type_ptr pItem = type_ptr(new T());
00354       if (! m_pDataMemberId) { qDebug("[QxOrm] problem with 'qxDeleteById()' method : '%s'", "data member id not registered"); qAssert(false); }
00355       if (! m_pDataMemberId) { m_lastError = QSqlError("[QxOrm] problem with 'qxDeleteById()' method : 'data member id not registered'", "", QSqlError::UnknownError); return m_lastError; }
00356       m_pDataMemberId->fromVariant(pItem.get(), id);
00357       m_lastError = qx::dao::delete_by_id((* pItem), database(pDatabase));
00358       return m_lastError;
00359    }
00360 
00361    virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL)
00362    {
00363       m_lastError = qx::dao::delete_all<T>(database(pDatabase));
00364       return m_lastError;
00365    }
00366 
00367    virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL)
00368    {
00369       m_lastError = qx::dao::delete_by_query<T>(query, database(pDatabase));
00370       return m_lastError;
00371    }
00372 
00373    virtual QSqlError qxDestroyById(const QVariant & id, QSqlDatabase * pDatabase = NULL)
00374    {
00375       type_ptr pItem = type_ptr(new T());
00376       if (! m_pDataMemberId) { qDebug("[QxOrm] problem with 'qxDeleteById()' method : '%s'", "data member id not registered"); qAssert(false); }
00377       if (! m_pDataMemberId) { m_lastError = QSqlError("[QxOrm] problem with 'qxDeleteById()' method : 'data member id not registered'", "", QSqlError::UnknownError); return m_lastError; }
00378       m_pDataMemberId->fromVariant(pItem.get(), id);
00379       m_lastError = qx::dao::destroy_by_id((* pItem), database(pDatabase));
00380       return m_lastError;
00381    }
00382 
00383    virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL)
00384    {
00385       m_lastError = qx::dao::destroy_all<T>(database(pDatabase));
00386       return m_lastError;
00387    }
00388 
00389    virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL)
00390    {
00391       m_lastError = qx::dao::destroy_by_query<T>(query, database(pDatabase));
00392       return m_lastError;
00393    }
00394 
00395    virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL)
00396    {
00397       clear();
00398       type_collection tmp;
00399       m_lastError = qx::dao::execute_query(query, tmp, database(pDatabase));
00400 
00401       beginInsertRows(QModelIndex(), 0, (tmp.count() - 1));
00402       m_model = tmp;
00403       endInsertRows();
00404       return m_lastError;
00405    }
00406 
00407    virtual qx_bool qxExist(const QVariant & id, QSqlDatabase * pDatabase = NULL)
00408    {
00409       type_ptr pItem = type_ptr(new T());
00410       if (! m_pDataMemberId) { qDebug("[QxOrm] problem with 'qxExist()' method : '%s'", "data member id not registered"); qAssert(false); }
00411       if (! m_pDataMemberId) { return qx_bool(false); }
00412       m_pDataMemberId->fromVariant(pItem.get(), id);
00413       return qx::dao::exist((* pItem), database(pDatabase));
00414    }
00415 
00416    virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList())
00417    {
00418       return qx::validate(m_model, groups);
00419    }
00420 
00421 };
00422 
00423 } // namespace qx
00424 
00425 #endif // _QX_MODEL_H_