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