| QxOrm 1.2.8 C++ Object Relational Mapping library

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

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    virtual ~QxModel() { ; }
    00177
    00178 protected:
    00179
    00180    void init()
    00181    {
    00182       BOOST_STATIC_ASSERT(qx_is_valid);
    00183       m_pClass = qx::QxClass<T>::getSingleton(); qAssert(m_pClass != NULL);
    00184       m_pDataMemberX = (m_pClass ? m_pClass->getDataMemberX() : NULL); qAssert(m_pDataMemberX != NULL);
    00185       m_pDataMemberId = (m_pDataMemberX ? m_pDataMemberX->getId_WithDaoStrategy() : NULL);
    00186       m_pCollection = (& m_model);
    00187       generateRoleNames();
    00188    }
    00189
    00190 public:
    00191
    00192    virtual QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const
    00193    {
    00194       if (! index.isValid()) { return QVariant(); }
    00195       if ((role == Qt::DisplayRole) || (role == Qt::EditRole))
    00196       {
    00197          if ((index.column() < 0) || (index.column() >= m_lstDataMember.count())) { return QVariant(); }
    00198          else if ((index.row() < 0) || (index.row() >= m_model.count())) { return QVariant(); }
    00199          IxDataMember * pDataMember = m_lstDataMember.at(index.column());
    00200          type_ptr pItem = m_model.getByIndex(index.row());
    00201          if (! pDataMember || ! pItem) { return QVariant(); }
    00202          return pDataMember->toVariant(pItem.get());
    00203       }
    00204       else if (role >= (Qt::UserRole + 1))
    00205       {
    00206          QModelIndex idx = this->index(index.row(), (role - Qt::UserRole - 1), QModelIndex());
    00207          return data(idx, Qt::DisplayRole);
    00208       }
    00209       return QVariant();
    00210    }
    00211
    00212    virtual bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole)
    00213    {
    00214       if (! index.isValid()) { return false; }
    00215       if (role == Qt::EditRole)
    00216       {
    00217          if ((index.column() < 0) || (index.column() >= m_lstDataMember.count())) { return false; }
    00218          else if ((index.row() < 0) || (index.row() >= m_model.count())) { return false; }
    00219          IxDataMember * pDataMember = m_lstDataMember.at(index.column());
    00220          type_ptr pItem = m_model.getByIndex(index.row());
    00221          if (! pDataMember || ! pItem) { return false; }
    00222          QVariant vCurrentValue = pDataMember->toVariant(pItem.get());
    00223          if (vCurrentValue == value) { return true; }
    00224          qx_bool bSetData = pDataMember->fromVariant(pItem.get(), value);
    00225          if (bSetData) { raiseEvent_dataChanged(index, index); }
    00226          return bSetData;
    00227       }
    00228       else if (role >= (Qt::UserRole + 1))
    00229       {
    00230          QModelIndex idx = this->index(index.row(), (role - Qt::UserRole - 1), QModelIndex());
    00231          return setData(idx, value, Qt::EditRole);
    00232       }
    00233       return false;
    00234    }
    00235
    00236    virtual bool insertRows(int row, int count, const QModelIndex & parent = QModelIndex())
    00237    {
    00238       if (parent.isValid()) { return false; }
    00239       beginInsertRows(QModelIndex(), row, (row + count - 1));
    00240       for (int i = 0; i < count; ++i)
    00241       {
    00242          m_lManualInsertIndex--;
    00243          type_primary_key primaryKey;
    00244          QVariant vNewId(static_cast<qlonglong>(m_lManualInsertIndex));
    00245          qx::cvt::from_variant(vNewId, primaryKey);
    00246          type_ptr pItem = type_ptr(new T());
    00247          m_model.insert(row, primaryKey, pItem);
    00248       }
    00249       endInsertRows();
    00250       return true;
    00251    }
    00252
    00253 public:
    00254
    00255    virtual long qxCount(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL)
    00256    {
    00257       return qx::dao::count<T>(query, database(pDatabase));
    00258    }
    00259
    00260    virtual QSqlError qxCount(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL)
    00261    {
    00262       m_lastError = qx::dao::count<T>(lCount, query, database(pDatabase));
    00263       return m_lastError;
    00264    }
    00265
    00266    virtual QSqlError qxFetchById(const QVariant & id, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
    00267    {
    00268       clear();
    00269       type_ptr pItem = type_ptr(new T());
    00270       if (! m_pDataMemberId) { qDebug("[QxOrm] problem with 'qxFetchById()' method : '%s'", "data member id not registered"); qAssert(false); }
    00271       if (! m_pDataMemberId) { m_lastError = QSqlError("[QxOrm] problem with 'qxFetchById()' method : 'data member id not registered'", "", QSqlError::UnknownError); return m_lastError; }
    00272       m_pDataMemberId->fromVariant(pItem.get(), id);
    00273
    00274       type_primary_key primaryKey;
    00275       qx::cvt::from_variant(id, primaryKey);
    00276       beginInsertRows(QModelIndex(), 0, 0);
    00277       m_model.insert(primaryKey, pItem);
    00278
    00279       if (relation.count() == 0) { m_lastError = qx::dao::fetch_by_id((* pItem), database(pDatabase), m_lstColumns); }
    00280       else { m_lastError = qx::dao::fetch_by_id_with_relation(relation, (* pItem), database(pDatabase)); }
    00281       endInsertRows();
    00282       return m_lastError;
    00283    }
    00284
    00285    virtual QSqlError qxFetchAll(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
    00286    {
    00287       clear();
    00288       type_collection tmp;
    00289       if (relation.count() == 0) { m_lastError = qx::dao::fetch_all(tmp, database(pDatabase), m_lstColumns); }
    00290       else { m_lastError = qx::dao::fetch_all_with_relation(relation, tmp, database(pDatabase)); }
    00291
    00292       beginInsertRows(QModelIndex(), 0, (tmp.count() - 1));
    00293       m_model = tmp;
    00294       endInsertRows();
    00295       return m_lastError;
    00296    }
    00297
    00298    virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
    00299    {
    00300       clear();
    00301       type_collection tmp;
    00302       if (relation.count() == 0) { m_lastError = qx::dao::fetch_by_query(query, tmp, database(pDatabase), m_lstColumns); }
    00303       else { m_lastError = qx::dao::fetch_by_query_with_relation(relation, query, tmp, database(pDatabase)); }
    00304
    00305       beginInsertRows(QModelIndex(), 0, (tmp.count() - 1));
    00306       m_model = tmp;
    00307       endInsertRows();
    00308       return m_lastError;
    00309    }
    00310
    00311    virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
    00312    {
    00313       if (relation.count() == 0) { m_lastError = qx::dao::insert(m_model, database(pDatabase)); }
    00314       else { m_lastError = qx::dao::insert_with_relation(relation, m_model, database(pDatabase)); }
    00315       return m_lastError;
    00316    }
    00317
    00318    virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
    00319    {
    00320       if (relation.count() == 0) { m_lastError = qx::dao::update_by_query(query, m_model, database(pDatabase), m_lstColumns); }
    00321       else { m_lastError = qx::dao::update_by_query_with_relation(relation, query, m_model, database(pDatabase)); }
    00322       return m_lastError;
    00323    }
    00324
    00325    virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
    00326    {
    00327       if (relation.count() == 0) { m_lastError = qx::dao::save(m_model, database(pDatabase)); }
    00328       else { m_lastError = qx::dao::save_with_relation(relation, m_model, database(pDatabase)); }
    00329       return m_lastError;
    00330    }
    00331
    00332    virtual QSqlError qxSaveRow(int row, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
    00333    {
    00334       if ((row < 0) || (row >= m_model.count())) { return QSqlError(); }
    00335       type_ptr pItem = m_model.getByIndex(row); if (! pItem) { return QSqlError(); }
    00336       if (relation.count() == 0) { m_lastError = qx::dao::save((* pItem), database(pDatabase)); }
    00337       else { m_lastError = qx::dao::save_with_relation(relation, (* pItem), database(pDatabase)); }
    00338       return m_lastError;
    00339    }
    00340
    00341    virtual QSqlError qxDeleteById(const QVariant & id, QSqlDatabase * pDatabase = NULL)
    00342    {
    00343       type_ptr pItem = type_ptr(new T());
    00344       if (! m_pDataMemberId) { qDebug("[QxOrm] problem with 'qxDeleteById()' method : '%s'", "data member id not registered"); qAssert(false); }
    00345       if (! m_pDataMemberId) { m_lastError = QSqlError("[QxOrm] problem with 'qxDeleteById()' method : 'data member id not registered'", "", QSqlError::UnknownError); return m_lastError; }
    00346       m_pDataMemberId->fromVariant(pItem.get(), id);
    00347       m_lastError = qx::dao::delete_by_id((* pItem), database(pDatabase));
    00348       return m_lastError;
    00349    }
    00350
    00351    virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL)
    00352    {
    00353       m_lastError = qx::dao::delete_all<T>(database(pDatabase));
    00354       return m_lastError;
    00355    }
    00356
    00357    virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL)
    00358    {
    00359       m_lastError = qx::dao::delete_by_query<T>(query, database(pDatabase));
    00360       return m_lastError;
    00361    }
    00362
    00363    virtual QSqlError qxDestroyById(const QVariant & id, QSqlDatabase * pDatabase = NULL)
    00364    {
    00365       type_ptr pItem = type_ptr(new T());
    00366       if (! m_pDataMemberId) { qDebug("[QxOrm] problem with 'qxDeleteById()' method : '%s'", "data member id not registered"); qAssert(false); }
    00367       if (! m_pDataMemberId) { m_lastError = QSqlError("[QxOrm] problem with 'qxDeleteById()' method : 'data member id not registered'", "", QSqlError::UnknownError); return m_lastError; }
    00368       m_pDataMemberId->fromVariant(pItem.get(), id);
    00369       m_lastError = qx::dao::destroy_by_id((* pItem), database(pDatabase));
    00370       return m_lastError;
    00371    }
    00372
    00373    virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL)
    00374    {
    00375       m_lastError = qx::dao::destroy_all<T>(database(pDatabase));
    00376       return m_lastError;
    00377    }
    00378
    00379    virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL)
    00380    {
    00381       m_lastError = qx::dao::destroy_by_query<T>(query, database(pDatabase));
    00382       return m_lastError;
    00383    }
    00384
    00385    virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL)
    00386    {
    00387       clear();
    00388       type_collection tmp;
    00389       m_lastError = qx::dao::execute_query(query, tmp, database(pDatabase));
    00390
    00391       beginInsertRows(QModelIndex(), 0, (tmp.count() - 1));
    00392       m_model = tmp;
    00393       endInsertRows();
    00394       return m_lastError;
    00395    }
    00396
    00397    virtual qx_bool qxExist(const QVariant & id, QSqlDatabase * pDatabase = NULL)
    00398    {
    00399       type_ptr pItem = type_ptr(new T());
    00400       if (! m_pDataMemberId) { qDebug("[QxOrm] problem with 'qxExist()' method : '%s'", "data member id not registered"); qAssert(false); }
    00401       if (! m_pDataMemberId) { return qx_bool(false); }
    00402       m_pDataMemberId->fromVariant(pItem.get(), id);
    00403       return qx::dao::exist((* pItem), database(pDatabase));
    00404    }
    00405
    00406    virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList())
    00407    {
    00408       return qx::validate(m_model, groups);
    00409    }
    00410
    00411 };
    00412
    00413 } // namespace qx
    00414
    00415 #endif // _QX_MODEL_H_