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