| QxOrm 1.2.4 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 ** http://sourceforge.net/projects/qxorm/
    00005 ** Original file by Lionel Marty
    00006 **
    00007 ** This file is part of the QxOrm library
    00008 **
    00009 ** This software is provided 'as-is', without any express or implied
    00010 ** warranty. In no event will the authors be held liable for any
    00011 ** damages arising from the use of this software.
    00012 **
    00013 ** GNU Lesser General Public License Usage
    00014 ** This file must be used under the terms of the GNU Lesser
    00015 ** General Public License version 2.1 as published by the Free Software
    00016 ** Foundation and appearing in the file 'license.lgpl.txt' included in the
    00017 ** packaging of this file.  Please review the following information to
    00018 ** ensure the GNU Lesser General Public License version 2.1 requirements
    00019 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    00020 **
    00021 ** If you have questions regarding the use of this file, please contact :
    00022 ** contact@qxorm.com
    00023 **
    00024 ****************************************************************************/
    00025
    00026 #ifndef _QX_SQL_RELATION_ONE_TO_MANY_H_
    00027 #define _QX_SQL_RELATION_ONE_TO_MANY_H_
    00028
    00029 #ifdef _MSC_VER
    00030 #pragma once
    00031 #endif
    00032
    00040 #include <QxDao/QxSqlRelation.h>
    00041
    00042 namespace qx {
    00043
    00048 template <class DataType, class Owner>
    00049 class QxSqlRelation_OneToMany : public QxSqlRelation<DataType, Owner>
    00050 {
    00051
    00052 private:
    00053
    00054    typedef typename QxSqlRelation<DataType, Owner>::type_owner type_owner;
    00055    typedef typename QxSqlRelation<DataType, Owner>::type_data type_data;
    00056    typedef typename QxSqlRelation<DataType, Owner>::type_container type_container;
    00057    typedef typename QxSqlRelation<DataType, Owner>::type_generic_container type_generic_container;
    00058    typedef typename QxSqlRelation<DataType, Owner>::type_item type_item;
    00059
    00060    enum { is_data_container = QxSqlRelation<DataType, Owner>::is_data_container };
    00061
    00062 protected:
    00063
    00064    QString m_sForeignKey;
    00065
    00066 public:
    00067
    00068    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()); }
    00069    virtual ~QxSqlRelation_OneToMany() { BOOST_STATIC_ASSERT(is_data_container); }
    00070
    00071    virtual QString getDescription() const                                     { return "relation one-to-many"; }
    00072    virtual QString getExtraTable() const                                      { return ""; }
    00073    virtual QString createExtraTable() const                                   { return ""; }
    00074    virtual bool getCartesianProduct() const                                   { return true; }
    00075    virtual void createTable(QxSqlRelationParams & params) const               { Q_UNUSED(params); }
    00076    virtual void lazySelect(QxSqlRelationParams & params) const                { Q_UNUSED(params); }
    00077    virtual void lazyFrom(QxSqlRelationParams & params) const                  { Q_UNUSED(params); }
    00078    virtual void eagerFrom(QxSqlRelationParams & params) const                 { Q_UNUSED(params); }
    00079    virtual void lazyJoin(QxSqlRelationParams & params) const                  { Q_UNUSED(params); }
    00080    virtual void lazyWhere(QxSqlRelationParams & params) const                 { Q_UNUSED(params); }
    00081    virtual void eagerWhere(QxSqlRelationParams & params) const                { Q_UNUSED(params); }
    00082    virtual void lazyWhereSoftDelete(QxSqlRelationParams & params) const       { Q_UNUSED(params); }
    00083    virtual void lazyFetch_ResolveInput(QxSqlRelationParams & params) const    { Q_UNUSED(params); }
    00084    virtual void eagerFetch_ResolveInput(QxSqlRelationParams & params) const   { Q_UNUSED(params); }
    00085    virtual void lazyFetch_ResolveOutput(QxSqlRelationParams & params) const   { Q_UNUSED(params); }
    00086    virtual void lazyInsert(QxSqlRelationParams & params) const                { Q_UNUSED(params); }
    00087    virtual void lazyInsert_Values(QxSqlRelationParams & params) const         { Q_UNUSED(params); }
    00088    virtual void lazyUpdate(QxSqlRelationParams & params) const                { Q_UNUSED(params); }
    00089    virtual void lazyInsert_ResolveInput(QxSqlRelationParams & params) const   { Q_UNUSED(params); }
    00090    virtual void lazyUpdate_ResolveInput(QxSqlRelationParams & params) const   { Q_UNUSED(params); }
    00091    virtual QSqlError onBeforeSave(QxSqlRelationParams & params) const         { Q_UNUSED(params); return QSqlError(); }
    00092
    00093    virtual QSqlError onAfterSave(QxSqlRelationParams & params) const
    00094    {
    00095       if (this->isNullData(params)) { return QSqlError(); }
    00096       return qx::dao::save(this->getContainer(params), (& params.database()));
    00097    }
    00098
    00099    virtual QVariant getIdFromQuery(bool bEager, QxSqlRelationParams & params) const
    00100    {
    00101       QString sId; IxDataMember * pId = this->getDataId(); if (! bEager || ! pId) { return QVariant(); }
    00102       for (int i = 0; i < pId->getNameCount(); i++) { sId += params.query().value(params.offset() + i).toString() + "|"; }
    00103       return QVariant(sId);
    00104    }
    00105
    00106    virtual void updateOffset(bool bEager, QxSqlRelationParams & params) const
    00107    {
    00108       if (! bEager) { return; }
    00109       IxDataMember * pForeign = this->getDataByKey(m_sForeignKey);
    00110       bool bAddOffsetForeign = (pForeign && ! this->getLstDataMember()->exist(m_sForeignKey));
    00111       long lOffsetId = (this->getDataId() ? this->getDataId()->getNameCount() : 0);
    00112       long lNewOffset = (params.offset() + this->getDataCount() + lOffsetId);
    00113       lNewOffset = (lNewOffset + (bAddOffsetForeign ? pForeign->getNameCount() : 0));
    00114       lNewOffset = (lNewOffset + (this->m_oSoftDelete.isEmpty() ? 0 : 1));
    00115       params.setOffset(lNewOffset);
    00116    }
    00117
    00118    virtual void eagerSelect(QxSqlRelationParams & params) const
    00119    {
    00120       long l1(0);
    00121       QString & sql = params.sql();
    00122       IxDataMember * pForeign = this->getDataByKey(m_sForeignKey); qAssert(pForeign);
    00123       IxDataMember * p = NULL; IxDataMember * pId = this->getDataId(); qAssert(pId);
    00124       QString tableAlias = this->tableAlias(params);
    00125       if (pId) { sql += (pId->getSqlTablePointNameAsAlias(tableAlias) + ", "); }
    00126       if (pForeign) { sql += (pForeign->getSqlTablePointNameAsAlias(tableAlias) + ", "); }
    00127       while ((p = this->nextData(l1))) { if (p != pForeign) { sql += (p->getSqlTablePointNameAsAlias(tableAlias) + ", "); } }
    00128       if (! this->m_oSoftDelete.isEmpty()) { sql += (this->m_oSoftDelete.buildSqlTablePointName(tableAlias) + ", "); }
    00129    }
    00130
    00131    virtual void eagerJoin(QxSqlRelationParams & params) const
    00132    {
    00133       QString & sql = params.sql();
    00134       IxDataMember * pId = this->getDataIdOwner(); qAssert(pId);
    00135       IxDataMember * pForeign = this->getDataByKey(m_sForeignKey); qAssert(pForeign);
    00136       QString table = this->table(); QString tableAlias = this->tableAlias(params);
    00137       QString tableRef = this->tableAliasOwner(params);
    00138       if (! pId || ! pForeign) { return; }
    00139       sql += this->getSqlJoin(params.joinType()) + table + " " + tableAlias + " ON ";
    00140       params.builder().addSqlQueryAlias(table, tableAlias);
    00141       for (int i = 0; i < pId->getNameCount(); i++)
    00142       { sql += pForeign->getSqlAlias(tableAlias, true, i) + " = " + pId->getSqlAlias(tableRef, true, i) + " AND "; }
    00143       sql = sql.left(sql.count() - 5); // Remove last " AND "
    00144    }
    00145
    00146    virtual void eagerWhereSoftDelete(QxSqlRelationParams & params) const
    00147    {
    00148       if (this->m_oSoftDelete.isEmpty()) { return; }
    00149       QString & sql = params.sql();
    00150       QString tableAlias = this->tableAlias(params);
    00151       sql += qx::IxSqlQueryBuilder::addSqlCondition(sql);
    00152       sql += this->m_oSoftDelete.buildSqlQueryToFetch(tableAlias);
    00153    }
    00154
    00155    virtual void * eagerFetch_ResolveOutput(QxSqlRelationParams & params) const
    00156    {
    00157       if (! this->verifyOffset(params, true)) { return NULL; }
    00158       QSqlQuery & query = params.query();
    00159       IxDataMember * p = NULL; IxDataMember * pId = this->getDataId(); qAssert(pId); if (! pId) { return NULL; }
    00160       IxDataMember * pForeign = this->getDataByKey(m_sForeignKey); qAssert(pForeign); if (! pForeign) { return NULL; }
    00161       long lIndex = 0; long lOffsetId = (pId ? pId->getNameCount() : 0); long lOffsetForeign = (pForeign ? pForeign->getNameCount() : 0);
    00162       long lOffsetOld = params.offset(); this->updateOffset(true, params);
    00163       long lOffsetRelation = (lOffsetOld + lOffsetId + lOffsetForeign);
    00164       bool bValidId(false), bValidForeign(false);
    00165       for (int i = 0; i < pId->getNameCount(); i++)
    00166       { QVariant vId = query.value(lOffsetOld + i); bValidId = (bValidId || qx::trait::is_valid_primary_key(vId)); }
    00167       for (int i = 0; i < pForeign->getNameCount(); i++)
    00168       { QVariant vForeign = query.value(lOffsetOld + lOffsetId + i); bValidForeign = (bValidForeign || qx::trait::is_valid_primary_key(vForeign)); }
    00169       if (! bValidId || ! bValidForeign) { return NULL; }
    00170       type_item item = this->createItem();
    00171       type_data & item_val = item.value_qx();
    00172       for (int i = 0; i < pId->getNameCount(); i++)
    00173       { QVariant v = query.value(lOffsetOld + i); qx::cvt::from_variant(v, item.key(), "", i); }
    00174       for (int i = 0; i < pId->getNameCount(); i++)
    00175       { QVariant v = query.value(lOffsetOld + i); pId->fromVariant((& item_val), v, "", i); }
    00176       for (int i = 0; i < pForeign->getNameCount(); i++)
    00177       { QVariant v = query.value(lOffsetOld + lOffsetId + i); pForeign->fromVariant((& item_val), v, "", i); }
    00178       while ((p = this->nextData(lIndex)))
    00179       { if (p != pForeign) { p->fromVariant((& item_val), query.value(lOffsetRelation++)); } }
    00180       type_generic_container::insertItem(this->getContainer(params), item);
    00181       return (& item_val);
    00182    }
    00183
    00184 };
    00185
    00186 } // namespace qx
    00187
    00188 #endif // _QX_SQL_RELATION_ONE_TO_MANY_H_