| QxOrm 1.2.5 C++ Object Relational Mapping library

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

- File List
- File Members

QxSqlRelation_OneToMany.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_ONE_TO_MANY_H_
    00033 #define _QX_SQL_RELATION_ONE_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_OneToMany : 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
    00066    enum { is_data_container = QxSqlRelation<DataType, Owner>::is_data_container };
    00067
    00068 protected:
    00069
    00070    QString m_sForeignKey;
    00071
    00072 public:
    00073
    00074    QxSqlRelation_OneToMany(IxDataMember * p, const QString & sForeignKey) : QxSqlRelation<DataType, Owner>(p), m_sForeignKey(sForeignKey) { this->m_eRelationType = qx::IxSqlRelation::one_to_many; qAssert(! m_sForeignKey.isEmpty()); }
    00075    virtual ~QxSqlRelation_OneToMany() { BOOST_STATIC_ASSERT(is_data_container); }
    00076
    00077    virtual QString getDescription() const                                     { return "relation one-to-many"; }
    00078    virtual QString getExtraTable() const                                      { return ""; }
    00079    virtual QString createExtraTable() const                                   { return ""; }
    00080    virtual bool getCartesianProduct() const                                   { return true; }
    00081    virtual void createTable(QxSqlRelationParams & params) const               { Q_UNUSED(params); }
    00082    virtual void lazySelect(QxSqlRelationParams & params) const                { Q_UNUSED(params); }
    00083    virtual void lazyFrom(QxSqlRelationParams & params) const                  { Q_UNUSED(params); }
    00084    virtual void eagerFrom(QxSqlRelationParams & params) const                 { Q_UNUSED(params); }
    00085    virtual void lazyJoin(QxSqlRelationParams & params) const                  { Q_UNUSED(params); }
    00086    virtual void lazyWhere(QxSqlRelationParams & params) const                 { Q_UNUSED(params); }
    00087    virtual void eagerWhere(QxSqlRelationParams & params) const                { Q_UNUSED(params); }
    00088    virtual void lazyWhereSoftDelete(QxSqlRelationParams & params) const       { Q_UNUSED(params); }
    00089    virtual void lazyFetch_ResolveInput(QxSqlRelationParams & params) const    { Q_UNUSED(params); }
    00090    virtual void eagerFetch_ResolveInput(QxSqlRelationParams & params) const   { Q_UNUSED(params); }
    00091    virtual void lazyFetch_ResolveOutput(QxSqlRelationParams & params) const   { Q_UNUSED(params); }
    00092    virtual void lazyInsert(QxSqlRelationParams & params) const                { Q_UNUSED(params); }
    00093    virtual void lazyInsert_Values(QxSqlRelationParams & params) const         { Q_UNUSED(params); }
    00094    virtual void lazyUpdate(QxSqlRelationParams & params) const                { Q_UNUSED(params); }
    00095    virtual void lazyInsert_ResolveInput(QxSqlRelationParams & params) const   { Q_UNUSED(params); }
    00096    virtual void lazyUpdate_ResolveInput(QxSqlRelationParams & params) const   { Q_UNUSED(params); }
    00097    virtual QSqlError onBeforeSave(QxSqlRelationParams & params) const         { Q_UNUSED(params); return QSqlError(); }
    00098
    00099    virtual QSqlError onAfterSave(QxSqlRelationParams & params) const
    00100    {
    00101       if (this->isNullData(params)) { return QSqlError(); }
    00102       return qx::dao::save(this->getContainer(params), (& params.database()));
    00103    }
    00104
    00105    virtual QVariant getIdFromQuery(bool bEager, QxSqlRelationParams & params) const
    00106    {
    00107       QString sId; IxDataMember * pId = this->getDataId(); if (! bEager || ! pId) { return QVariant(); }
    00108       for (int i = 0; i < pId->getNameCount(); i++) { sId += params.query().value(params.offset() + i).toString() + "|"; }
    00109       return QVariant(sId);
    00110    }
    00111
    00112    virtual void updateOffset(bool bEager, QxSqlRelationParams & params) const
    00113    {
    00114       if (! bEager) { return; }
    00115       IxDataMember * pForeign = this->getDataByKey(m_sForeignKey);
    00116       bool bAddOffsetForeign = (pForeign && ! this->getLstDataMember()->exist(m_sForeignKey));
    00117       long lOffsetId = (this->getDataId() ? this->getDataId()->getNameCount() : 0);
    00118       long lNewOffset = (params.offset() + this->getDataCount() + lOffsetId);
    00119       lNewOffset = (lNewOffset + (bAddOffsetForeign ? pForeign->getNameCount() : 0));
    00120       lNewOffset = (lNewOffset + (this->m_oSoftDelete.isEmpty() ? 0 : 1));
    00121       params.setOffset(lNewOffset);
    00122    }
    00123
    00124    virtual void eagerSelect(QxSqlRelationParams & params) const
    00125    {
    00126       long l1(0);
    00127       QString & sql = params.sql();
    00128       IxDataMember * pForeign = this->getDataByKey(m_sForeignKey); qAssert(pForeign);
    00129       IxDataMember * p = NULL; IxDataMember * pId = this->getDataId(); qAssert(pId);
    00130       QString tableAlias = this->tableAlias(params);
    00131       if (pId) { sql += (pId->getSqlTablePointNameAsAlias(tableAlias) + ", "); }
    00132       if (pForeign) { sql += (pForeign->getSqlTablePointNameAsAlias(tableAlias) + ", "); }
    00133       while ((p = this->nextData(l1))) { if (p != pForeign) { 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->getDataIdOwner(); qAssert(pId);
    00141       IxDataMember * pForeign = this->getDataByKey(m_sForeignKey); qAssert(pForeign);
    00142       QString table = this->table(); QString tableAlias = this->tableAlias(params);
    00143       QString tableRef = this->tableAliasOwner(params);
    00144       if (! pId || ! pForeign) { 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 += pForeign->getSqlAlias(tableAlias, true, i) + " = " + pId->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 * eagerFetch_ResolveOutput(QxSqlRelationParams & params) const
    00162    {
    00163       if (! this->verifyOffset(params, true)) { return NULL; }
    00164       QSqlQuery & query = params.query();
    00165       IxDataMember * p = NULL; IxDataMember * pId = this->getDataId(); qAssert(pId); if (! pId) { return NULL; }
    00166       IxDataMember * pForeign = this->getDataByKey(m_sForeignKey); qAssert(pForeign); if (! pForeign) { return NULL; }
    00167       long lIndex = 0; long lOffsetId = (pId ? pId->getNameCount() : 0); long lOffsetForeign = (pForeign ? pForeign->getNameCount() : 0);
    00168       long lOffsetOld = params.offset(); this->updateOffset(true, params);
    00169       long lOffsetRelation = (lOffsetOld + lOffsetId + lOffsetForeign);
    00170       bool bValidId(false), bValidForeign(false);
    00171       for (int i = 0; i < pId->getNameCount(); i++)
    00172       { QVariant vId = query.value(lOffsetOld + i); bValidId = (bValidId || qx::trait::is_valid_primary_key(vId)); }
    00173       for (int i = 0; i < pForeign->getNameCount(); i++)
    00174       { QVariant vForeign = query.value(lOffsetOld + lOffsetId + i); bValidForeign = (bValidForeign || qx::trait::is_valid_primary_key(vForeign)); }
    00175       if (! bValidId || ! bValidForeign) { return NULL; }
    00176       type_item item = this->createItem();
    00177       type_data & item_val = item.value_qx();
    00178       for (int i = 0; i < pId->getNameCount(); i++)
    00179       { QVariant v = query.value(lOffsetOld + i); qx::cvt::from_variant(v, item.key(), "", i); }
    00180       for (int i = 0; i < pId->getNameCount(); i++)
    00181       { QVariant v = query.value(lOffsetOld + i); pId->fromVariant((& item_val), v, "", i); }
    00182       for (int i = 0; i < pForeign->getNameCount(); i++)
    00183       { QVariant v = query.value(lOffsetOld + lOffsetId + i); pForeign->fromVariant((& item_val), v, "", i); }
    00184       while ((p = this->nextData(lIndex)))
    00185       { if (p != pForeign) { p->fromVariant((& item_val), query.value(lOffsetRelation++)); } }
    00186       type_generic_container::insertItem(this->getContainer(params), item);
    00187       return (& item_val);
    00188    }
    00189
    00190 };
    00191
    00192 } // namespace qx
    00193
    00194 #endif // _QX_SQL_RELATION_ONE_TO_MANY_H_