QxOrm  1.2.6
C++ Object Relational Mapping library
QxSqlRelation_ManyToMany.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_MANY_H_
00033 #define _QX_SQL_RELATION_MANY_TO_MANY_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_ManyToMany : 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    typedef typename QxSqlRelation<DataType, Owner>::type_container type_container;
00063    typedef typename QxSqlRelation<DataType, Owner>::type_generic_container type_generic_container;
00064    typedef typename QxSqlRelation<DataType, Owner>::type_item type_item;
00065    typedef typename type_generic_container::type_iterator type_iterator;
00066 
00067    enum { is_data_container = QxSqlRelation<DataType, Owner>::is_data_container };
00068 
00069 public:
00070 
00071    QxSqlRelation_ManyToMany(IxDataMember * p, const QString & sExtraTable, const QString & sForeignKeyOwner, const QString & sForeignKeyDataType) : QxSqlRelation<DataType, Owner>(p) { this->m_eRelationType = qx::IxSqlRelation::many_to_many; this->m_sExtraTable = sExtraTable; this->m_sForeignKeyOwner = sForeignKeyOwner; this->m_sForeignKeyDataType = sForeignKeyDataType; this->verifyParameters(); }
00072    virtual ~QxSqlRelation_ManyToMany() { BOOST_STATIC_ASSERT(is_data_container); }
00073 
00074    virtual QString getDescription() const                                     { return "relation many-to-many"; }
00075    virtual QString getExtraTable() const                                      { return this->m_sExtraTable; }
00076    virtual bool getCartesianProduct() const                                   { return true; }
00077    virtual void createTable(QxSqlRelationParams & params) const               { Q_UNUSED(params); }
00078    virtual void lazySelect(QxSqlRelationParams & params) const                { Q_UNUSED(params); }
00079    virtual void lazyFrom(QxSqlRelationParams & params) const                  { Q_UNUSED(params); }
00080    virtual void eagerFrom(QxSqlRelationParams & params) const                 { Q_UNUSED(params); }
00081    virtual void lazyJoin(QxSqlRelationParams & params) const                  { Q_UNUSED(params); }
00082    virtual void lazyWhere(QxSqlRelationParams & params) const                 { Q_UNUSED(params); }
00083    virtual void eagerWhere(QxSqlRelationParams & params) const                { Q_UNUSED(params); }
00084    virtual void lazyWhereSoftDelete(QxSqlRelationParams & params) const       { Q_UNUSED(params); }
00085    virtual void lazyFetch_ResolveInput(QxSqlRelationParams & params) const    { Q_UNUSED(params); }
00086    virtual void eagerFetch_ResolveInput(QxSqlRelationParams & params) const   { Q_UNUSED(params); }
00087    virtual void lazyFetch_ResolveOutput(QxSqlRelationParams & params) const   { Q_UNUSED(params); }
00088    virtual void lazyInsert(QxSqlRelationParams & params) const                { Q_UNUSED(params); }
00089    virtual void lazyInsert_Values(QxSqlRelationParams & params) const         { Q_UNUSED(params); }
00090    virtual void lazyUpdate(QxSqlRelationParams & params) const                { Q_UNUSED(params); }
00091    virtual void lazyInsert_ResolveInput(QxSqlRelationParams & params) const   { Q_UNUSED(params); }
00092    virtual void lazyUpdate_ResolveInput(QxSqlRelationParams & params) const   { Q_UNUSED(params); }
00093    virtual QSqlError onBeforeSave(QxSqlRelationParams & params) const         { Q_UNUSED(params); return QSqlError(); }
00094 
00095    virtual QVariant getIdFromQuery(bool bEager, QxSqlRelationParams & params) const
00096    { return this->getIdFromQuery_ManyToMany(bEager, params); }
00097 
00098    virtual void updateOffset(bool bEager, QxSqlRelationParams & params) const
00099    { this->updateOffset_ManyToMany(bEager, params); }
00100 
00101    virtual void eagerSelect(QxSqlRelationParams & params) const
00102    { this->eagerSelect_ManyToMany(params); }
00103 
00104    virtual void eagerJoin(QxSqlRelationParams & params) const
00105    { this->eagerJoin_ManyToMany(params); }
00106 
00107    virtual void eagerWhereSoftDelete(QxSqlRelationParams & params) const
00108    { this->eagerWhereSoftDelete_ManyToMany(params); }
00109 
00110    virtual void * eagerFetch_ResolveOutput(QxSqlRelationParams & params) const
00111    {
00112       if (! this->verifyOffset(params, true)) { return NULL; }
00113       QSqlQuery & query = params.query();
00114       IxDataMember * p = NULL; IxDataMember * pId = this->getDataId(); qAssert(pId);
00115       long lIndex = 0; long lOffsetId = (pId ? pId->getNameCount() : 0); bool bValidId(false);
00116       long lOffsetOld = params.offset(); this->updateOffset(true, params);
00117       long lRelation = 0; IxSqlRelation * pRelation = NULL;
00118       for (int i = 0; i < pId->getNameCount(); i++)
00119       { QVariant v = query.value(lOffsetOld + i); bValidId = (bValidId || qx::trait::is_valid_primary_key(v)); }
00120       if (! bValidId) { return NULL; }
00121       type_item item = this->createItem();
00122       type_data & item_val = item.value_qx();
00123       if (! this->callTriggerBeforeFetch(item_val, params)) { return NULL; }
00124       for (int i = 0; i < pId->getNameCount(); i++)
00125       { QVariant v = query.value(lOffsetOld + i); qx::cvt::from_variant(v, item.key(), "", i); }
00126       for (int i = 0; i < pId->getNameCount(); i++)
00127       { QVariant v = query.value(lOffsetOld + i); pId->fromVariant((& item_val), v, "", i); }
00128       while ((p = this->nextData(lIndex)))
00129       { p->fromVariant((& item_val), query.value(lIndex + lOffsetOld + lOffsetId - 1)); }
00130 
00131       if (params.relationX())
00132       {
00133          long lOffsetCurrent = (lIndex + lOffsetOld + lOffsetId);
00134          long lIndexOwnerOld = params.indexOwner(); params.setIndexOwner(params.index());
00135          void * pOwnerOld = params.owner(); params.setOwner(& item_val);
00136          lOffsetOld = params.offset(); params.setOffset(lOffsetCurrent);
00137          while ((pRelation = this->nextRelation(lRelation)))
00138          { if (this->addLazyRelation(params, pRelation)) { pRelation->lazyFetch_ResolveOutput(params); } }
00139          params.setOwner(pOwnerOld); params.setOffset(lOffsetOld);
00140          params.setIndexOwner(lIndexOwnerOld);
00141       }
00142 
00143       if (! this->callTriggerAfterFetch(item_val, params)) { return NULL; }
00144       type_generic_container::insertItem(this->getContainer(params), item);
00145       return (& item_val);
00146    }
00147 
00148    virtual QSqlError onAfterSave(QxSqlRelationParams & params) const
00149    {
00150       if (this->isNullData(params)) { return this->deleteFromExtraTable(params); }
00151       QSqlError daoError = qx::dao::save(this->getContainer(params), (& params.database()));
00152       if (daoError.isValid()) { return daoError; }
00153       daoError = this->deleteFromExtraTable(params);
00154       if (daoError.isValid()) { return daoError; }
00155       daoError = this->insertIntoExtraTable(params);
00156       if (daoError.isValid()) { return daoError; }
00157       return QSqlError();
00158    }
00159 
00160    virtual QString createExtraTable() const
00161    { return this->createExtraTable_ManyToMany(); }
00162 
00163 private:
00164 
00165    inline void verifyParameters()
00166    { qAssert(! this->m_sExtraTable.isEmpty() && ! this->m_sForeignKeyOwner.isEmpty() && ! this->m_sForeignKeyDataType.isEmpty() && (this->m_sForeignKeyOwner != this->m_sForeignKeyDataType)); }
00167 
00168    QSqlError deleteFromExtraTable(QxSqlRelationParams & params) const
00169    { return this->deleteFromExtraTable_ManyToMany(params); }
00170 
00171    QSqlError insertIntoExtraTable(QxSqlRelationParams & params) const
00172    {
00173       IxDataMember * pIdOwner = this->getDataIdOwner(); qAssert(pIdOwner);
00174       IxDataMember * pIdData = this->getDataId(); qAssert(pIdData);
00175       if (! pIdOwner || ! pIdData) { return QSqlError(); }
00176       QStringList lstForeignKeyOwner = this->m_sForeignKeyOwner.split("|");
00177       QStringList lstForeignKeyDataType = this->m_sForeignKeyDataType.split("|");
00178       qAssert(pIdOwner->getNameCount() == lstForeignKeyOwner.count());
00179       qAssert(pIdData->getNameCount() == lstForeignKeyDataType.count());
00180 
00181       QString sql = "INSERT INTO " + this->m_sExtraTable + " (";
00182       sql += pIdOwner->getSqlName(", ", this->m_sForeignKeyOwner) + ", " + pIdData->getSqlName(", ", this->m_sForeignKeyDataType);
00183       sql += ") VALUES (";
00184       sql += pIdOwner->getSqlPlaceHolder("", -1, ", ", this->m_sForeignKeyOwner) + ", " + pIdData->getSqlPlaceHolder("", -1, ", ", this->m_sForeignKeyDataType) + ")";
00185       if (this->traceSqlQuery()) { qDebug("[QxOrm] sql query (extra-table) : %s", qPrintable(sql)); }
00186 
00187       type_item item;
00188       type_container & container = this->getContainer(params);
00189       type_iterator itr = type_generic_container::begin(container, item);
00190       type_iterator itr_end = type_generic_container::end(container);
00191       QSqlQuery queryInsert(params.database());
00192       queryInsert.prepare(sql);
00193 
00194       while (itr != itr_end)
00195       {
00196          pIdOwner->setSqlPlaceHolder(queryInsert, params.owner(), "", this->m_sForeignKeyOwner);
00197          pIdData->setSqlPlaceHolder(queryInsert, (& item.value_qx()), "", this->m_sForeignKeyDataType);
00198          if (! queryInsert.exec()) { return queryInsert.lastError(); }
00199          itr = type_generic_container::next(container, itr, item);
00200       }
00201 
00202       return QSqlError();
00203    }
00204 
00205 };
00206 
00207 } // namespace qx
00208 
00209 #endif // _QX_SQL_RELATION_MANY_TO_MANY_H_