QxOrm  1.2.5
C++ Object Relational Mapping library
QxSqlRelation_ManyToOne.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_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->m_eRelationType = qx::IxSqlRelation::many_to_one; }
00066    virtual ~QxSqlRelation_ManyToOne() { ; }
00067 
00068    virtual QString getDescription() const                                     { return "relation many-to-one"; }
00069    virtual QString getExtraTable() const                                      { return ""; }
00070    virtual QString createExtraTable() const                                   { return ""; }
00071    virtual bool getCartesianProduct() const                                   { return false; }
00072    virtual void lazyFrom(QxSqlRelationParams & params) const                  { Q_UNUSED(params); }
00073    virtual void eagerFrom(QxSqlRelationParams & params) const                 { Q_UNUSED(params); }
00074    virtual void lazyJoin(QxSqlRelationParams & params) const                  { Q_UNUSED(params); }
00075    virtual void lazyWhere(QxSqlRelationParams & params) const                 { Q_UNUSED(params); }
00076    virtual void eagerWhere(QxSqlRelationParams & params) const                { Q_UNUSED(params); }
00077    virtual void lazyWhereSoftDelete(QxSqlRelationParams & params) const       { Q_UNUSED(params); }
00078    virtual void lazyFetch_ResolveInput(QxSqlRelationParams & params) const    { Q_UNUSED(params); }
00079    virtual void eagerFetch_ResolveInput(QxSqlRelationParams & params) const   { Q_UNUSED(params); }
00080    virtual QSqlError onAfterSave(QxSqlRelationParams & params) const          { Q_UNUSED(params); return QSqlError(); }
00081 
00082    virtual QSqlError onBeforeSave(QxSqlRelationParams & params) const
00083    {
00084       if (this->isNullData(params)) { return QSqlError(); }
00085       return qx::dao::save(this->getData(params), (& params.database()));
00086    }
00087 
00088    virtual void lazyUpdate_ResolveInput(QxSqlRelationParams & params) const
00089    { this->lazyInsert_ResolveInput(params); }
00090 
00091    virtual QVariant getIdFromQuery(bool bEager, QxSqlRelationParams & params) const
00092    {
00093       Q_UNUSED(bEager); QString sId; IxDataMember * pId = this->getDataId(); if (! pId) { return QVariant(); }
00094       for (int i = 0; i < pId->getNameCount(); i++) { sId += params.query().value(params.offset() + i).toString() + "|"; }
00095       return QVariant(sId);
00096    }
00097 
00098    virtual void updateOffset(bool bEager, QxSqlRelationParams & params) const
00099    {
00100       long lOffsetDataMember = (this->getDataMember() ? this->getDataMember()->getNameCount() : 0);
00101       long lOffsetDataId = (bEager ? (this->getDataId() ? this->getDataId()->getNameCount() : 0) : 0);
00102       long lOffsetDataCount = (bEager ? this->getDataCount() : 0);
00103       long lOffsetSoftDelete = (bEager ? (this->m_oSoftDelete.isEmpty() ? 0 : 1) : 0);
00104       params.setOffset(params.offset() + lOffsetDataMember + lOffsetDataId + lOffsetDataCount + lOffsetSoftDelete);
00105    }
00106 
00107    virtual void createTable(QxSqlRelationParams & params) const
00108    {
00109       QString & sql = params.sql();
00110       IxDataMember * pData = this->getDataMember(); qAssert(pData);
00111       if (pData) { sql += pData->getSqlNameAndTypeAndParams(", ") + ", "; qAssert(! pData->getSqlType().isEmpty()); }
00112    }
00113 
00114    virtual void lazySelect(QxSqlRelationParams & params) const
00115    {
00116       QString & sql = params.sql();
00117       QString tableRef = this->tableAliasOwner(params);
00118       IxDataMember * pData = this->getDataMember(); qAssert(pData);
00119       if (pData) { sql += (pData->getSqlTablePointNameAsAlias(tableRef) + ", "); }
00120    }
00121 
00122    virtual void eagerSelect(QxSqlRelationParams & params) const
00123    {
00124       long l1(0);
00125       QString & sql = params.sql();
00126       IxDataMember * pData = this->getDataMember(); qAssert(pData);
00127       IxDataMember * p = NULL; IxDataMember * pId = this->getDataId(); qAssert(pId);
00128       QString table = this->table(); QString tableAlias = this->tableAlias(params);
00129       QString tableRef = this->tableAliasOwner(params); QString sSuffixAlias;
00130       if (params.indexOwner() > 0) { sSuffixAlias = "_" + QString::number(params.indexOwner()); }
00131       if (pData) { sql += (pData->getSqlTablePointNameAsAlias(tableRef, ", ", sSuffixAlias) + ", "); }
00132       if (pId) { sql += (pId->getSqlTablePointNameAsAlias(tableAlias) + ", "); }
00133       while ((p = this->nextData(l1))) { sql += (p->getSqlTablePointNameAsAlias(tableAlias) + ", "); }
00134       if (! this->m_oSoftDelete.isEmpty()) { sql += (this->m_oSoftDelete.buildSqlTablePointName(tableAlias) + ", "); }
00135    }
00136 
00137    virtual void eagerJoin(QxSqlRelationParams & params) const
00138    {
00139       QString & sql = params.sql();
00140       IxDataMember * pId = this->getDataId(); qAssert(pId);
00141       IxDataMember * pData = this->getDataMember(); qAssert(pData);
00142       QString table = this->table(); QString tableAlias = this->tableAlias(params);
00143       QString tableRef = this->tableAliasOwner(params);
00144       if (! pId || ! pData) { return; }
00145       sql += this->getSqlJoin(params.joinType()) + table + " " + tableAlias + " ON ";
00146       params.builder().addSqlQueryAlias(table, tableAlias);
00147       for (int i = 0; i < pId->getNameCount(); i++)
00148       { sql += pId->getSqlAlias(tableAlias, true, i) + " = " + pData->getSqlAlias(tableRef, true, i) + " AND "; }
00149       sql = sql.left(sql.count() - 5); // Remove last " AND "
00150    }
00151 
00152    virtual void eagerWhereSoftDelete(QxSqlRelationParams & params) const
00153    {
00154       if (this->m_oSoftDelete.isEmpty()) { return; }
00155       QString & sql = params.sql();
00156       QString tableAlias = this->tableAlias(params);
00157       sql += qx::IxSqlQueryBuilder::addSqlCondition(sql);
00158       sql += this->m_oSoftDelete.buildSqlQueryToFetch(tableAlias);
00159    }
00160 
00161    virtual void lazyFetch_ResolveOutput(QxSqlRelationParams & params) const
00162    {
00163       if (! this->verifyOffset(params, false)) { return; }
00164       QSqlQuery & query = params.query();
00165       typename QxSqlRelation<DataType, Owner>::type_owner & currOwner = this->getOwner(params);
00166       IxDataMember * pData = this->getDataMember(); qAssert(pData); if (! pData) { return; }
00167       for (int i = 0; i < pData->getNameCount(); i++)
00168       { QVariant vId = query.value(params.offset() + i); pData->fromVariant((& currOwner), vId, i); }
00169       this->updateOffset(false, params);
00170    }
00171 
00172    virtual void * eagerFetch_ResolveOutput(QxSqlRelationParams & params) const
00173    {
00174       if (! this->verifyOffset(params, false)) { return NULL; }
00175       QSqlQuery & query = params.query();
00176       typename QxSqlRelation<DataType, Owner>::type_owner & currOwner = this->getOwner(params);
00177       IxDataMember * pData = this->getDataMember(); qAssert(pData); if (! pData) { return NULL; }
00178       IxDataMember * p = NULL; IxDataMember * pId = this->getDataId(); qAssert(pId); if (! pId) { return NULL; }
00179       long lIndex = 0; long lOffsetId = (pId ? pId->getNameCount() : 0); long lOffsetData = (pData ? pData->getNameCount() : 0);
00180       long lOffsetOld = params.offset(); this->updateOffset(true, params);
00181       long lOffsetRelation = (lOffsetOld + lOffsetId + lOffsetData);
00182       bool bValidId(false), bValidIdBis(false);
00183       for (int i = 0; i < pData->getNameCount(); i++)
00184       { QVariant vId = query.value(lOffsetOld + i); bValidId = (bValidId || qx::trait::is_valid_primary_key(vId)); }
00185       for (int i = 0; i < pId->getNameCount(); i++)
00186       { QVariant vIdBis = query.value(lOffsetOld + lOffsetData + i); bValidIdBis = (bValidIdBis || qx::trait::is_valid_primary_key(vIdBis)); }
00187       if (pData && bValidId) { for (int i = 0; i < pData->getNameCount(); i++) { pData->fromVariant((& currOwner), query.value(lOffsetOld + i), i); } }
00188       if (! bValidIdBis) { return NULL; }
00189       type_data & currData = this->getData(params);
00190       if (pId) { for (int i = 0; i < pId->getNameCount(); i++) { pId->fromVariant((& currData), query.value(lOffsetOld + lOffsetData + i), i); } }
00191       while ((p = this->nextData(lIndex)))
00192       { p->fromVariant((& currData), query.value(lOffsetRelation++)); }
00193       return (& currData);
00194    }
00195 
00196    virtual void lazyInsert(QxSqlRelationParams & params) const
00197    {
00198       QString & sql = params.sql();
00199       IxDataMember * pData = this->getDataMember(); qAssert(pData);
00200       if (pData) { sql += pData->getSqlName(", ") + ", "; }
00201    }
00202 
00203    virtual void lazyInsert_Values(QxSqlRelationParams & params) const
00204    {
00205       QString & sql = params.sql();
00206       IxDataMember * pData = this->getDataMember(); qAssert(pData);
00207       if (pData) { sql += pData->getSqlPlaceHolder("", -1, ", ") + ", "; }
00208    }
00209 
00210    virtual void lazyUpdate(QxSqlRelationParams & params) const
00211    {
00212       QString & sql = params.sql();
00213       IxDataMember * pData = this->getDataMember(); qAssert(pData);
00214       if (pData) { sql += pData->getSqlNameEqualToPlaceHolder("", ", ") + ", "; }
00215    }
00216 
00217    virtual void lazyInsert_ResolveInput(QxSqlRelationParams & params) const
00218    {
00219       QSqlQuery & query = params.query();
00220       typename QxSqlRelation<DataType, Owner>::type_owner & currOwner = this->getOwner(params);
00221       IxDataMember * pData = this->getDataMember(); qAssert(pData);
00222       if (pData) { pData->setSqlPlaceHolder(query, (& currOwner)); }
00223    }
00224 
00225 };
00226 
00227 } // namespace qx
00228 
00229 #endif // _QX_SQL_RELATION_MANY_TO_ONE_H_