QxOrm  1.5.0
C++ Object Relational Mapping library
QxSqlRelation_ManyToOne.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** https://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_SQL_RELATION_MANY_TO_ONE_H_
00033 #define _QX_SQL_RELATION_MANY_TO_ONE_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
00046 #include <QxDao/QxSqlRelation.h>
00047 
00048 namespace qx {
00049 
00054 template <class DataType, class Owner>
00055 class QxSqlRelation_ManyToOne : public QxSqlRelation<DataType, Owner>
00056 {
00057 
00058 private:
00059 
00060    typedef typename QxSqlRelation<DataType, Owner>::type_owner type_owner;
00061    typedef typename QxSqlRelation<DataType, Owner>::type_data type_data;
00062 
00063 public:
00064 
00065    QxSqlRelation_ManyToOne(IxDataMember * p) : QxSqlRelation<DataType, Owner>(p) { this->setRelationType(qx::IxSqlRelation::many_to_one); }
00066    virtual ~QxSqlRelation_ManyToOne() { ; }
00067 
00068    virtual QString getDescription() const                                     { return "relation many-to-one"; }
00069    virtual QString createExtraTable() const                                   { return ""; }
00070    virtual bool getCartesianProduct() const                                   { return false; }
00071    virtual void lazyFrom(QxSqlRelationParams & params) const                  { Q_UNUSED(params); }
00072    virtual void eagerFrom(QxSqlRelationParams & params) const                 { Q_UNUSED(params); }
00073    virtual void lazyJoin(QxSqlRelationParams & params) const                  { Q_UNUSED(params); }
00074    virtual void lazyWhere(QxSqlRelationParams & params) const                 { Q_UNUSED(params); }
00075    virtual void eagerWhere(QxSqlRelationParams & params) const                { Q_UNUSED(params); }
00076    virtual void lazyWhereSoftDelete(QxSqlRelationParams & params) const       { Q_UNUSED(params); }
00077    virtual void lazyFetch_ResolveInput(QxSqlRelationParams & params) const    { Q_UNUSED(params); }
00078    virtual void eagerFetch_ResolveInput(QxSqlRelationParams & params) const   { Q_UNUSED(params); }
00079    virtual QSqlError onAfterSave(QxSqlRelationParams & params) const          { Q_UNUSED(params); return QSqlError(); }
00080 
00081    virtual QSqlError onBeforeSave(QxSqlRelationParams & params) const
00082    {
00083       if (this->isNullData(params) || params.recursiveMode()) { return QSqlError(); }
00084       return qx::dao::save(this->getData(params), (& params.database()));
00085    }
00086 
00087    virtual void lazyUpdate_ResolveInput(QxSqlRelationParams & params) const
00088    { this->lazyInsert_ResolveInput(params); }
00089 
00090    virtual QVariant getIdFromQuery(bool bEager, QxSqlRelationParams & params, int iOffset, int iNameIndex) const
00091    { return this->getIdFromQuery_ManyToOne(bEager, params, iOffset, iNameIndex); }
00092 
00093    virtual void updateOffset(bool bEager, QxSqlRelationParams & params) const
00094    { this->updateOffset_ManyToOne(bEager, params); }
00095 
00096    virtual void createTable(QxSqlRelationParams & params) const
00097    { this->createTable_ManyToOne(params); }
00098 
00099    virtual void lazySelect(QxSqlRelationParams & params) const
00100    { this->lazySelect_ManyToOne(params); }
00101 
00102    virtual void eagerSelect(QxSqlRelationParams & params) const
00103    { this->eagerSelect_ManyToOne(params); }
00104 
00105    virtual void eagerJoin(QxSqlRelationParams & params) const
00106    { this->eagerJoin_ManyToOne(params); }
00107 
00108    virtual void eagerWhereSoftDelete(QxSqlRelationParams & params) const
00109    { this->eagerWhereSoftDelete_ManyToOne(params); }
00110 
00111    virtual void lazyFetch_ResolveOutput(QxSqlRelationParams & params) const
00112    {
00113       if (! this->verifyOffset(params, false)) { return; }
00114       QSqlQuery & query = params.query();
00115       typename QxSqlRelation<DataType, Owner>::type_owner & currOwner = this->getOwner(params);
00116       IxDataMember * pData = this->getDataMember(); qAssert(pData); if (! pData) { return; }
00117       bool bValidId(false);
00118       for (int i = 0; i < pData->getNameCount(); i++)
00119       { QVariant vId = query.value(params.offset() + i); bValidId = (bValidId || qx::trait::is_valid_primary_key(vId)); }
00120       if (pData && bValidId) { for (int i = 0; i < pData->getNameCount(); i++) { pData->fromVariant((& currOwner), query.value(params.offset() + i), i, qx::cvt::context::e_database); } }
00121       this->updateOffset(false, params);
00122    }
00123 
00124    virtual void * eagerFetch_ResolveOutput(QxSqlRelationParams & params) const
00125    {
00126       if (! this->verifyOffset(params, false)) { return NULL; }
00127       QSqlQuery & query = params.query();
00128       typename QxSqlRelation<DataType, Owner>::type_owner & currOwner = this->getOwner(params);
00129       IxDataMember * pData = this->getDataMember(); qAssert(pData); if (! pData) { return NULL; }
00130       IxDataMember * p = NULL; IxDataMember * pId = this->getDataId(); qAssert(pId); if (! pId) { return NULL; }
00131       long lIndex = 0; long lOffsetId = ((pId && (! params.isDistinct())) ? pId->getNameCount() : 0);
00132       long lOffsetData = ((pData && (! params.isDistinct())) ? pData->getNameCount() : 0);
00133       long lOffsetOld = params.offset(); this->updateOffset(true, params);
00134       long lOffsetRelation = (lOffsetOld + lOffsetId + lOffsetData);
00135       long lRelation = 0; IxSqlRelation * pRelation = NULL;
00136       bool bValidId(false), bValidIdBis(false);
00137 
00138       if (! params.isDistinct())
00139       {
00140          for (int i = 0; i < pData->getNameCount(); i++)
00141          { QVariant vId = query.value(lOffsetOld + i); bValidId = (bValidId || qx::trait::is_valid_primary_key(vId)); }
00142          for (int i = 0; i < pId->getNameCount(); i++)
00143          { QVariant vIdBis = query.value(lOffsetOld + lOffsetData + i); bValidIdBis = (bValidIdBis || qx::trait::is_valid_primary_key(vIdBis)); }
00144          if (pData && bValidId) { for (int i = 0; i < pData->getNameCount(); i++) { pData->fromVariant((& currOwner), query.value(lOffsetOld + i), i, qx::cvt::context::e_database); } }
00145          if (! bValidIdBis) { return NULL; }
00146       }
00147 
00148       type_data & currData = this->getData(params);
00149       if (! this->callTriggerBeforeFetch(currData, params)) { return NULL; }
00150 
00151       if (pId && (! params.isDistinct()))
00152       { for (int i = 0; i < pId->getNameCount(); i++) { pId->fromVariant((& currData), query.value(lOffsetOld + lOffsetData + i), i, qx::cvt::context::e_database); } }
00153       while ((p = this->nextData(lIndex)))
00154       { if (params.checkColumns(p->getKey())) { p->fromVariant((& currData), query.value(lOffsetRelation++), -1, qx::cvt::context::e_database); } }
00155 
00156       if (params.relationX())
00157       {
00158          QString sOldCustomAliasOwner = params.getCustomAliasOwner(); params.setCustomAliasOwner(params.getCustomAlias());
00159          long lIndexOwnerOld = params.indexOwner(); params.setIndexOwner(params.index());
00160          void * pOwnerOld = params.owner(); params.setOwner(& currData);
00161          lOffsetOld = params.offset(); params.setOffset(lOffsetRelation++);
00162          while ((pRelation = this->nextRelation(lRelation)))
00163          { if (this->addLazyRelation(params, pRelation)) { pRelation->lazyFetch_ResolveOutput(params); } }
00164          params.setOwner(pOwnerOld); params.setOffset(lOffsetOld);
00165          params.setCustomAliasOwner(sOldCustomAliasOwner);
00166          params.setIndexOwner(lIndexOwnerOld);
00167       }
00168 
00169       if (! this->callTriggerAfterFetch(currData, params)) { return NULL; }
00170       return (& currData);
00171    }
00172 
00173    virtual void lazyInsert(QxSqlRelationParams & params) const
00174    { this->lazyInsert_ManyToOne(params); }
00175 
00176    virtual void lazyInsert_Values(QxSqlRelationParams & params) const
00177    { this->lazyInsert_Values_ManyToOne(params); }
00178 
00179    virtual void lazyUpdate(QxSqlRelationParams & params) const
00180    { this->lazyUpdate_ManyToOne(params); }
00181 
00182    virtual void lazyInsert_ResolveInput(QxSqlRelationParams & params) const
00183    {
00184       QSqlQuery & query = params.query();
00185       typename QxSqlRelation<DataType, Owner>::type_owner & currOwner = this->getOwner(params);
00186       IxDataMember * pData = this->getDataMember(); qAssert(pData);
00187       if (pData) { pData->setSqlPlaceHolder(query, (& currOwner), "", "", false, params.getLstExecBatch()); }
00188    }
00189 
00190 };
00191 
00192 } // namespace qx
00193 
00194 #endif // _QX_SQL_RELATION_MANY_TO_ONE_H_