![]() |
QxOrm
1.4.2
C++ Object Relational Mapping library
|
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 typedef typename type_generic_container::type_iterator type_iterator; 00066 typedef typename type_item::type_value type_value; 00067 00068 enum { is_data_container = QxSqlRelation<DataType, Owner>::is_data_container }; 00069 00070 public: 00071 00072 QxSqlRelation_OneToMany(IxDataMember * p, const QString & sForeignKey) : QxSqlRelation<DataType, Owner>(p) { this->m_eRelationType = qx::IxSqlRelation::one_to_many; this->m_sForeignKey = sForeignKey; qAssert(! this->m_sForeignKey.isEmpty()); } 00073 virtual ~QxSqlRelation_OneToMany() { BOOST_STATIC_ASSERT(is_data_container); } 00074 00075 virtual QString getDescription() const { return "relation one-to-many"; } 00076 virtual QString getExtraTable() const { return ""; } 00077 virtual QString createExtraTable() const { return ""; } 00078 virtual bool getCartesianProduct() const { return true; } 00079 virtual void createTable(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00080 virtual void lazySelect(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00081 virtual void lazyFrom(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00082 virtual void eagerFrom(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00083 virtual void lazyJoin(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00084 virtual void lazyWhere(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00085 virtual void eagerWhere(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00086 virtual void lazyWhereSoftDelete(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00087 virtual void lazyFetch_ResolveInput(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00088 virtual void eagerFetch_ResolveInput(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00089 virtual void lazyFetch_ResolveOutput(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00090 virtual void lazyInsert(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00091 virtual void lazyInsert_Values(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00092 virtual void lazyUpdate(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00093 virtual void lazyInsert_ResolveInput(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00094 virtual void lazyUpdate_ResolveInput(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00095 virtual QSqlError onBeforeSave(QxSqlRelationParams & params) const { Q_UNUSED(params); return QSqlError(); } 00096 00097 virtual QSqlError onAfterSave(QxSqlRelationParams & params) const 00098 { 00099 if (this->isNullData(params)) { return QSqlError(); } 00100 this->forceParentIdToAllChildren(params); 00101 if (! params.recursiveMode()) { return qx::dao::save(this->getContainer(params), (& params.database())); } 00102 else { return qx::dao::save_with_relation_recursive(this->getContainer(params), params.saveMode(), (& params.database()), (& params)); } 00103 } 00104 00105 virtual QVariant getIdFromQuery(bool bEager, QxSqlRelationParams & params) const 00106 { return this->getIdFromQuery_OneToMany(bEager, params); } 00107 00108 virtual void updateOffset(bool bEager, QxSqlRelationParams & params) const 00109 { this->updateOffset_OneToMany(bEager, params); } 00110 00111 virtual void eagerSelect(QxSqlRelationParams & params) const 00112 { this->eagerSelect_OneToMany(params); } 00113 00114 virtual void eagerJoin(QxSqlRelationParams & params) const 00115 { this->eagerJoin_OneToMany(params); } 00116 00117 virtual void eagerWhereSoftDelete(QxSqlRelationParams & params) const 00118 { this->eagerWhereSoftDelete_OneToMany(params); } 00119 00120 virtual void * eagerFetch_ResolveOutput(QxSqlRelationParams & params) const 00121 { 00122 if (! this->verifyOffset(params, true)) { return NULL; } 00123 QSqlQuery & query = params.query(); 00124 IxDataMember * p = NULL; IxDataMember * pId = this->getDataId(); qAssert(pId); if (! pId) { return NULL; } 00125 IxDataMember * pForeign = this->getDataByKey(this->m_sForeignKey); qAssert(pForeign); if (! pForeign) { return NULL; } 00126 long lIndex = 0; long lOffsetId = (pId ? pId->getNameCount() : 0); long lOffsetForeign = (pForeign ? pForeign->getNameCount() : 0); 00127 long lOffsetOld = params.offset(); this->updateOffset(true, params); 00128 long lOffsetRelation = (lOffsetOld + lOffsetId + lOffsetForeign); 00129 long lRelation = 0; IxSqlRelation * pRelation = NULL; 00130 bool bValidId(false), bValidForeign(false); 00131 for (int i = 0; i < pId->getNameCount(); i++) 00132 { QVariant vId = query.value(lOffsetOld + i); bValidId = (bValidId || qx::trait::is_valid_primary_key(vId)); } 00133 for (int i = 0; i < pForeign->getNameCount(); i++) 00134 { QVariant vForeign = query.value(lOffsetOld + lOffsetId + i); bValidForeign = (bValidForeign || qx::trait::is_valid_primary_key(vForeign)); } 00135 if (! bValidId || ! bValidForeign) { return NULL; } 00136 type_item item = this->createItem(); 00137 type_data & item_val = item.value_qx(); 00138 if (! this->callTriggerBeforeFetch(item_val, params)) { return NULL; } 00139 for (int i = 0; i < pId->getNameCount(); i++) 00140 { QVariant v = query.value(lOffsetOld + i); qx::cvt::from_variant(v, item.key(), "", i); } 00141 for (int i = 0; i < pId->getNameCount(); i++) 00142 { QVariant v = query.value(lOffsetOld + i); pId->fromVariant((& item_val), v, "", i); } 00143 for (int i = 0; i < pForeign->getNameCount(); i++) 00144 { QVariant v = query.value(lOffsetOld + lOffsetId + i); pForeign->fromVariant((& item_val), v, "", i); } 00145 while ((p = this->nextData(lIndex))) 00146 { if ((p != pForeign) && (params.checkColumns(p->getKey()))) { p->fromVariant((& item_val), query.value(lOffsetRelation++)); } } 00147 00148 if (params.relationX()) 00149 { 00150 long lIndexOwnerOld = params.indexOwner(); params.setIndexOwner(params.index()); 00151 void * pOwnerOld = params.owner(); params.setOwner(& item_val); 00152 lOffsetOld = params.offset(); params.setOffset(lOffsetRelation++); 00153 while ((pRelation = this->nextRelation(lRelation))) 00154 { if (this->addLazyRelation(params, pRelation)) { pRelation->lazyFetch_ResolveOutput(params); } } 00155 params.setOwner(pOwnerOld); params.setOffset(lOffsetOld); 00156 params.setIndexOwner(lIndexOwnerOld); 00157 } 00158 00159 if (! this->callTriggerAfterFetch(item_val, params)) { return NULL; } 00160 type_value * pValue = type_generic_container::insertItem(this->getContainer(params), item); 00161 if (! type_item::is_value_pointer && pValue) { return pValue; } 00162 return (& item_val); 00163 } 00164 00165 private: 00166 00167 void forceParentIdToAllChildren(QxSqlRelationParams & params) const 00168 { 00169 bool bForce = qx::QxSqlDatabase::getSingleton()->getForceParentIdToAllChildren(); 00170 if (! bForce || ! params.owner()) { return; } 00171 IxDataMember * pIdOwner = this->getDataIdOwner(); if (! pIdOwner) { return; } 00172 IxDataMember * pForeign = this->getDataByKey(this->m_sForeignKey); if (! pForeign) { return; } 00173 if (pIdOwner->getNameCount() != pForeign->getNameCount()) { return; } 00174 00175 QList<QVariant> vIdOwner; 00176 for (int i = 0; i < pIdOwner->getNameCount(); i++) 00177 { vIdOwner.append(pIdOwner->toVariant(params.owner(), i)); } 00178 00179 type_item item; 00180 type_container & container = this->getContainer(params); 00181 type_iterator itr = type_generic_container::begin(container, item); 00182 type_iterator itr_end = type_generic_container::end(container); 00183 00184 while (itr != itr_end) 00185 { 00186 type_data & item_val = item.value_qx(); 00187 for (int i = 0; i < vIdOwner.count(); i++) 00188 { pForeign->fromVariant((& item_val), vIdOwner.at(i), "", i); } 00189 itr = type_generic_container::next(container, itr, item); 00190 } 00191 } 00192 00193 }; 00194 00195 } // namespace qx 00196 00197 #endif // _QX_SQL_RELATION_ONE_TO_MANY_H_