| QxOrm 1.4.3 C++ Object Relational Mapping library

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

- File List
- File Members

QxModelService.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_SERVICE_H_
    00033 #define _QX_MODEL_SERVICE_H_
    00034
    00035 #ifdef _MSC_VER
    00036 #pragma once
    00037 #endif
    00038
    00046 #include <QxModelView/QxModel.h>
    00047
    00048 namespace qx {
    00049
    00057 template <class T, class S>
    00058 class QxModelService : public qx::QxModel<T>
    00059 {
    00060
    00061 public:
    00062
    00063    typedef typename qx::QxModel<T>::type_ptr type_ptr;
    00064    typedef typename qx::QxModel<T>::type_primary_key type_primary_key;
    00065    typedef typename qx::QxModel<T>::type_collection type_collection;
    00066    typedef qx_shared_ptr<type_collection> type_collection_ptr;
    00067
    00068 public:
    00069
    00070    QxModelService(QObject * parent = 0) : qx::QxModel<T>(parent) { ; }
    00071    QxModelService(qx::IxModel * other, QObject * parent) : qx::QxModel<T>(other, parent) { ; }
    00072    virtual ~QxModelService() { ; }
    00073
    00074    virtual long qxCount(const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL)
    00075    {
    00076       long lCount = 0;
    00077       this->qxCount(lCount, query, pDatabase);
    00078       return lCount;
    00079    }
    00080
    00081    virtual QSqlError qxCount(long & lCount, const qx::QxSqlQuery & query = qx::QxSqlQuery(), QSqlDatabase * pDatabase = NULL)
    00082    {
    00083       Q_UNUSED(pDatabase);
    00084       S services;
    00085       this->m_lastError = services.count(lCount, query);
    00086       return this->m_lastError;
    00087    }
    00088
    00089    virtual QSqlError qxFetchById(const QVariant & id, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
    00090    {
    00091       Q_UNUSED(pDatabase);
    00092       this->clear();
    00093       type_ptr pItem = type_ptr(new T());
    00094       if (! this->m_pDataMemberId) { qDebug("[QxOrm] problem with 'qxFetchById()' method : '%s'", "data member id not registered"); qAssert(false); }
    00095       if (! this->m_pDataMemberId) { this->m_lastError = QSqlError("[QxOrm] problem with 'qxFetchById()' method : 'data member id not registered'", "", QSqlError::UnknownError); return this->m_lastError; }
    00096       this->m_pDataMemberId->fromVariant(pItem.get(), id);
    00097
    00098       type_primary_key primaryKey;
    00099       qx::cvt::from_variant(id, primaryKey);
    00100       this->beginInsertRows(QModelIndex(), 0, 0);
    00101       this->m_model.insert(primaryKey, pItem);
    00102
    00103       S services;
    00104       this->m_lastError = services.fetchById(pItem, this->m_lstColumns, relation);
    00105       this->endInsertRows();
    00106       return this->m_lastError;
    00107    }
    00108
    00109    virtual QSqlError qxFetchAll(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
    00110    {
    00111       Q_UNUSED(pDatabase);
    00112       this->clear();
    00113
    00114       S services;
    00115       type_collection_ptr tmp;
    00116       tmp.reset(new type_collection());
    00117       this->m_lastError = services.fetchAll(tmp, this->m_lstColumns, relation);
    00118
    00119       if (tmp->count() <= 0) { return this->m_lastError; }
    00120       this->beginInsertRows(QModelIndex(), 0, (tmp->count() - 1));
    00121       this->m_model = (* tmp);
    00122       this->endInsertRows();
    00123       return this->m_lastError;
    00124    }
    00125
    00126    virtual QSqlError qxFetchByQuery(const qx::QxSqlQuery & query, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
    00127    {
    00128       Q_UNUSED(pDatabase);
    00129       this->clear();
    00130
    00131       S services;
    00132       type_collection_ptr tmp;
    00133       tmp.reset(new type_collection());
    00134       this->m_lastError = services.fetchByQuery(query, tmp, this->m_lstColumns, relation);
    00135
    00136       if (tmp->count() <= 0) { return this->m_lastError; }
    00137       this->beginInsertRows(QModelIndex(), 0, (tmp->count() - 1));
    00138       this->m_model = (* tmp);
    00139       this->endInsertRows();
    00140       return this->m_lastError;
    00141    }
    00142
    00143    virtual QSqlError qxFetchRow(int row, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
    00144    {
    00145       Q_UNUSED(pDatabase);
    00146       if ((row < 0) || (row >= this->m_model.count())) { return QSqlError(); }
    00147       type_ptr pItem = this->m_model.getByIndex(row); if (! pItem) { return QSqlError(); }
    00148
    00149       S services;
    00150       this->m_lastError = services.fetchById(pItem, this->m_lstColumns, relation);
    00151       if (this->m_lastError.isValid()) { return this->m_lastError; }
    00152
    00153       QModelIndex idxTopLeft = this->index(row, 0);
    00154       QModelIndex idxBottomRight = this->index(row, (this->m_lstDataMember.count() - 1));
    00155       this->raiseEvent_dataChanged(idxTopLeft, idxBottomRight);
    00156       this->updateKey(row);
    00157       return this->m_lastError;
    00158    }
    00159
    00160    virtual QSqlError qxInsert(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
    00161    {
    00162       Q_UNUSED(pDatabase);
    00163       if (relation.count() > 0) { this->syncAllNestedModel(relation); }
    00164
    00165       type_collection_ptr tmp;
    00166       tmp.reset(new type_collection());
    00167       (* tmp) = this->m_model;
    00168
    00169       S services;
    00170       this->m_lastError = services.insert(tmp, relation);
    00171
    00172       if (! this->m_lastError.isValid()) { this->updateAllKeys(); }
    00173       return this->m_lastError;
    00174    }
    00175
    00176    virtual QSqlError qxInsertRow(int row, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
    00177    {
    00178       Q_UNUSED(pDatabase);
    00179       if ((row < 0) || (row >= this->m_model.count())) { return QSqlError(); }
    00180       if (relation.count() > 0) { this->syncNestedModel(row, relation); }
    00181       type_ptr pItem = this->m_model.getByIndex(row); if (! pItem) { return QSqlError(); }
    00182
    00183       S services;
    00184       this->m_lastError = services.insert(pItem, relation);
    00185
    00186       if (! this->m_lastError.isValid()) { this->updateKey(row); }
    00187       return this->m_lastError;
    00188    }
    00189
    00190    virtual QSqlError qxUpdate(const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
    00191    {
    00192       Q_UNUSED(pDatabase);
    00193       if (relation.count() > 0) { this->syncAllNestedModel(relation); }
    00194
    00195       type_collection_ptr tmp;
    00196       tmp.reset(new type_collection());
    00197       (* tmp) = this->m_model;
    00198
    00199       S services;
    00200       this->m_lastError = services.update(tmp, query, this->m_lstColumns, relation);
    00201
    00202       if (! this->m_lastError.isValid()) { this->updateAllKeys(); }
    00203       return this->m_lastError;
    00204    }
    00205
    00206    virtual QSqlError qxUpdateRow(int row, const qx::QxSqlQuery & query = qx::QxSqlQuery(), const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
    00207    {
    00208       Q_UNUSED(pDatabase);
    00209       if ((row < 0) || (row >= this->m_model.count())) { return QSqlError(); }
    00210       if (relation.count() > 0) { this->syncNestedModel(row, relation); }
    00211       type_ptr pItem = this->m_model.getByIndex(row); if (! pItem) { return QSqlError(); }
    00212
    00213       S services;
    00214       this->m_lastError = services.update(pItem, query, this->m_lstColumns, relation);
    00215
    00216       if (! this->m_lastError.isValid()) { this->updateKey(row); }
    00217       return this->m_lastError;
    00218    }
    00219
    00220    virtual QSqlError qxSave(const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
    00221    {
    00222       Q_UNUSED(pDatabase);
    00223       if (relation.count() > 0) { this->syncAllNestedModel(relation); }
    00224
    00225       type_collection_ptr tmp;
    00226       tmp.reset(new type_collection());
    00227       (* tmp) = this->m_model;
    00228
    00229       S services;
    00230       this->m_lastError = services.save(tmp, relation);
    00231
    00232       if (! this->m_lastError.isValid()) { this->updateAllKeys(); }
    00233       return this->m_lastError;
    00234    }
    00235
    00236    virtual QSqlError qxSaveRow(int row, const QStringList & relation = QStringList(), QSqlDatabase * pDatabase = NULL)
    00237    {
    00238       Q_UNUSED(pDatabase);
    00239       if ((row < 0) || (row >= this->m_model.count())) { return QSqlError(); }
    00240       if (relation.count() > 0) { this->syncNestedModel(row, relation); }
    00241       type_ptr pItem = this->m_model.getByIndex(row); if (! pItem) { return QSqlError(); }
    00242
    00243       S services;
    00244       this->m_lastError = services.save(pItem, relation);
    00245
    00246       if (! this->m_lastError.isValid()) { this->updateKey(row); }
    00247       return this->m_lastError;
    00248    }
    00249
    00250    virtual QSqlError qxDeleteById(const QVariant & id, QSqlDatabase * pDatabase = NULL)
    00251    {
    00252       Q_UNUSED(pDatabase);
    00253       type_ptr pItem = type_ptr(new T());
    00254       if (! this->m_pDataMemberId) { qDebug("[QxOrm] problem with 'qxDeleteById()' method : '%s'", "data member id not registered"); qAssert(false); }
    00255       if (! this->m_pDataMemberId) { this->m_lastError = QSqlError("[QxOrm] problem with 'qxDeleteById()' method : 'data member id not registered'", "", QSqlError::UnknownError); return this->m_lastError; }
    00256       this->m_pDataMemberId->fromVariant(pItem.get(), id);
    00257
    00258       S services;
    00259       this->m_lastError = services.deleteById(pItem);
    00260       return this->m_lastError;
    00261    }
    00262
    00263    virtual QSqlError qxDeleteAll(QSqlDatabase * pDatabase = NULL)
    00264    {
    00265       Q_UNUSED(pDatabase);
    00266       S services;
    00267       this->m_lastError = services.deleteAll();
    00268       return this->m_lastError;
    00269    }
    00270
    00271    virtual QSqlError qxDeleteByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL)
    00272    {
    00273       Q_UNUSED(pDatabase);
    00274       S services;
    00275       this->m_lastError = services.deleteByQuery(query);
    00276       return this->m_lastError;
    00277    }
    00278
    00279    virtual QSqlError qxDeleteRow(int row, QSqlDatabase * pDatabase = NULL)
    00280    {
    00281       Q_UNUSED(pDatabase);
    00282       if ((row < 0) || (row >= this->m_model.count())) { return QSqlError(); }
    00283       type_ptr pItem = this->m_model.getByIndex(row); if (! pItem) { return QSqlError(); }
    00284
    00285       S services;
    00286       this->m_lastError = services.deleteById(pItem);
    00287       return this->m_lastError;
    00288    }
    00289
    00290    virtual QSqlError qxDestroyById(const QVariant & id, QSqlDatabase * pDatabase = NULL)
    00291    {
    00292       Q_UNUSED(pDatabase);
    00293       type_ptr pItem = type_ptr(new T());
    00294       if (! this->m_pDataMemberId) { qDebug("[QxOrm] problem with 'qxDeleteById()' method : '%s'", "data member id not registered"); qAssert(false); }
    00295       if (! this->m_pDataMemberId) { this->m_lastError = QSqlError("[QxOrm] problem with 'qxDeleteById()' method : 'data member id not registered'", "", QSqlError::UnknownError); return this->m_lastError; }
    00296       this->m_pDataMemberId->fromVariant(pItem.get(), id);
    00297
    00298       S services;
    00299       this->m_lastError = services.destroyById(pItem);
    00300       return this->m_lastError;
    00301    }
    00302
    00303    virtual QSqlError qxDestroyAll(QSqlDatabase * pDatabase = NULL)
    00304    {
    00305       Q_UNUSED(pDatabase);
    00306       S services;
    00307       this->m_lastError = services.destroyAll();
    00308       return this->m_lastError;
    00309    }
    00310
    00311    virtual QSqlError qxDestroyByQuery(const qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL)
    00312    {
    00313       Q_UNUSED(pDatabase);
    00314       S services;
    00315       this->m_lastError = services.destroyByQuery(query);
    00316       return this->m_lastError;
    00317    }
    00318
    00319    virtual QSqlError qxDestroyRow(int row, QSqlDatabase * pDatabase = NULL)
    00320    {
    00321       Q_UNUSED(pDatabase);
    00322       if ((row < 0) || (row >= this->m_model.count())) { return QSqlError(); }
    00323       type_ptr pItem = this->m_model.getByIndex(row); if (! pItem) { return QSqlError(); }
    00324
    00325       S services;
    00326       this->m_lastError = services.destroyById(pItem);
    00327       return this->m_lastError;
    00328    }
    00329
    00330    virtual QSqlError qxExecuteQuery(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL)
    00331    {
    00332       Q_UNUSED(pDatabase);
    00333       this->clear();
    00334
    00335       S services;
    00336       type_collection_ptr tmp;
    00337       tmp.reset(new type_collection());
    00338       this->m_lastError = services.executeQuery(query, tmp);
    00339
    00340       if (tmp->count() <= 0) { return this->m_lastError; }
    00341       this->beginInsertRows(QModelIndex(), 0, (tmp->count() - 1));
    00342       this->m_model = (* tmp);
    00343       this->endInsertRows();
    00344       return this->m_lastError;
    00345    }
    00346
    00347    virtual qx_bool qxExist(const QVariant & id, QSqlDatabase * pDatabase = NULL)
    00348    {
    00349       Q_UNUSED(pDatabase);
    00350       type_ptr pItem = type_ptr(new T());
    00351       if (! this->m_pDataMemberId) { qDebug("[QxOrm] problem with 'qxExist()' method : '%s'", "data member id not registered"); qAssert(false); }
    00352       if (! this->m_pDataMemberId) { return qx_bool(false); }
    00353       this->m_pDataMemberId->fromVariant(pItem.get(), id);
    00354
    00355       S services;
    00356       return services.exist(pItem);
    00357    }
    00358
    00359    virtual qx::QxInvalidValueX qxValidate(const QStringList & groups = QStringList())
    00360    {
    00361       Q_UNUSED(groups);
    00362       S services;
    00363       type_collection_ptr tmp;
    00364       tmp.reset(new type_collection());
    00365       (* tmp) = this->m_model;
    00366       return services.isValid(tmp);
    00367    }
    00368
    00369    virtual qx::QxInvalidValueX qxValidateRow(int row, const QStringList & groups = QStringList())
    00370    {
    00371       Q_UNUSED(groups);
    00372       if ((row < 0) || (row >= this->m_model.count())) { return qx::QxInvalidValueX(); }
    00373       type_ptr pItem = this->m_model.getByIndex(row); if (! pItem) { return qx::QxInvalidValueX(); }
    00374
    00375       S services;
    00376       return services.isValid(pItem);
    00377    }
    00378
    00379 protected:
    00380
    00381 #ifndef _QX_NO_JSON
    00382
    00383    virtual QVariant getRelationshipValues_Helper(int row, const QString & relation, bool bLoadFromDatabase, const QString & sAppendRelations)
    00384    {
    00385       if ((row < 0) || (row >= this->m_model.count())) { return QVariant(); }
    00386       if (! this->m_pDataMemberId || ! this->m_pDataMemberX || ! this->m_pDataMemberX->exist(relation)) { return QVariant(); }
    00387       IxDataMember * pDataMember = this->m_pDataMemberX->get_WithDaoStrategy(relation); if (! pDataMember) { return QVariant(); }
    00388       IxSqlRelation * pRelation = (pDataMember->hasSqlRelation() ? pDataMember->getSqlRelation() : NULL); if (! pRelation) { return QVariant(); }
    00389       type_ptr pItem = this->m_model.getByIndex(row); if (! pItem) { return QVariant(); }
    00390       type_ptr pItemTemp = pItem;
    00391
    00392       if (bLoadFromDatabase)
    00393       {
    00394          QString sRelation = relation;
    00395          if (! sAppendRelations.isEmpty() && ! sAppendRelations.startsWith("->") && ! sAppendRelations.startsWith(">>")) { sRelation += "->" + sAppendRelations; }
    00396          else if (! sAppendRelations.isEmpty()) { sRelation += sAppendRelations; }
    00397          pItemTemp = type_ptr(new T());
    00398          QVariant id = this->m_pDataMemberId->toVariant(pItem.get());
    00399          this->m_pDataMemberId->fromVariant(pItemTemp.get(), id);
    00400
    00401          S services; QStringList columns;
    00402          QSqlError daoError = services.fetchById(pItemTemp, columns, QStringList() << sRelation);
    00403          if (daoError.isValid()) { return QVariant(); }
    00404       }
    00405
    00406       QJsonValue json = pDataMember->toJson(pItemTemp.get()); if (json.isNull()) { return QVariant(); }
    00407       if (json.isArray()) { return json.toArray().toVariantList(); }
    00408       return json.toObject().toVariantMap();
    00409    }
    00410
    00411 #endif // _QX_NO_JSON
    00412
    00413 };
    00414
    00415 } // namespace qx
    00416
    00417 #endif // _QX_MODEL_SERVICE_H_