![]() |
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_ONE_TO_ONE_H_ 00027 #define _QX_SQL_RELATION_ONE_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_OneToOne : 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_OneToOne(IxDataMember * p) : QxSqlRelation<DataType, Owner>(p) { this->m_eRelationType = qx::IxSqlRelation::one_to_one; } 00060 virtual ~QxSqlRelation_OneToOne() { ; } 00061 00062 virtual QString getDescription() const { return "relation one-to-one"; } 00063 virtual QString getExtraTable() const { return ""; } 00064 virtual QString createExtraTable() const { return ""; } 00065 virtual bool getCartesianProduct() const { return false; } 00066 virtual void createTable(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00067 virtual void lazySelect(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00068 virtual void lazyFrom(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00069 virtual void eagerFrom(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00070 virtual void lazyJoin(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00071 virtual void lazyWhere(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00072 virtual void eagerWhere(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00073 virtual void lazyWhereSoftDelete(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00074 virtual void lazyFetch_ResolveInput(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00075 virtual void eagerFetch_ResolveInput(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00076 virtual void lazyFetch_ResolveOutput(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00077 virtual void lazyInsert(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00078 virtual void lazyInsert_Values(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00079 virtual void lazyUpdate(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00080 virtual void lazyInsert_ResolveInput(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00081 virtual void lazyUpdate_ResolveInput(QxSqlRelationParams & params) const { Q_UNUSED(params); } 00082 virtual QSqlError onBeforeSave(QxSqlRelationParams & params) const { Q_UNUSED(params); return QSqlError(); } 00083 00084 virtual QSqlError onAfterSave(QxSqlRelationParams & params) const 00085 { 00086 if (this->isNullData(params)) { return QSqlError(); } 00087 return qx::dao::save(this->getData(params), (& params.database())); 00088 } 00089 00090 virtual QVariant getIdFromQuery(bool bEager, QxSqlRelationParams & params) const 00091 { 00092 QString sId; IxDataMember * pId = this->getDataId(); if (! bEager || ! pId) { return QVariant(); } 00093 for (int i = 0; i < pId->getNameCount(); i++) { sId += params.query().value(params.offset() + i).toString() + "|"; } 00094 return QVariant(sId); 00095 } 00096 00097 virtual void updateOffset(bool bEager, QxSqlRelationParams & params) const 00098 { 00099 if (! bEager) { return; } 00100 long lOffsetNew = params.offset() + this->getDataCount(); 00101 lOffsetNew += (this->getDataId() ? this->getDataId()->getNameCount() : 0); 00102 lOffsetNew += (this->m_oSoftDelete.isEmpty() ? 0 : 1); 00103 params.setOffset(lOffsetNew); 00104 } 00105 00106 virtual void eagerSelect(QxSqlRelationParams & params) const 00107 { 00108 long l1(0); 00109 QString & sql = params.sql(); 00110 QString tableAlias = this->tableAlias(params); 00111 IxDataMember * p = NULL; IxDataMember * pId = this->getDataId(); qAssert(pId); 00112 if (pId) { sql += (pId->getSqlTablePointNameAsAlias(tableAlias) + ", "); } 00113 while ((p = this->nextData(l1))) { sql += (p->getSqlTablePointNameAsAlias(tableAlias) + ", "); } 00114 if (! this->m_oSoftDelete.isEmpty()) { sql += (this->m_oSoftDelete.buildSqlTablePointName(tableAlias) + ", "); } 00115 } 00116 00117 virtual void eagerJoin(QxSqlRelationParams & params) const 00118 { 00119 QString & sql = params.sql(); 00120 IxDataMember * pId = this->getDataId(); qAssert(pId); 00121 IxDataMember * pIdRef = this->getDataIdOwner(); qAssert(pIdRef); 00122 QString table = this->table(); QString tableAlias = this->tableAlias(params); 00123 QString tableRef = this->tableAliasOwner(params); 00124 if (! pId || ! pIdRef) { return; } 00125 sql += this->getSqlJoin(params.joinType()) + table + " " + tableAlias + " ON "; 00126 params.builder().addSqlQueryAlias(table, tableAlias); 00127 for (int i = 0; i < pId->getNameCount(); i++) 00128 { sql += pId->getSqlAlias(tableAlias, true, i) + " = " + pIdRef->getSqlAlias(tableRef, true, i) + " AND "; } 00129 sql = sql.left(sql.count() - 5); // Remove last " AND " 00130 } 00131 00132 virtual void eagerWhereSoftDelete(QxSqlRelationParams & params) const 00133 { 00134 if (this->m_oSoftDelete.isEmpty()) { return; } 00135 QString & sql = params.sql(); 00136 QString tableAlias = this->tableAlias(params); 00137 sql += qx::IxSqlQueryBuilder::addSqlCondition(sql); 00138 sql += this->m_oSoftDelete.buildSqlQueryToFetch(tableAlias); 00139 } 00140 00141 virtual void * eagerFetch_ResolveOutput(QxSqlRelationParams & params) const 00142 { 00143 if (! this->verifyOffset(params, true)) { return NULL; } 00144 QSqlQuery & query = params.query(); 00145 IxDataMember * p = NULL; IxDataMember * pId = this->getDataId(); qAssert(pId); if (! pId) { return NULL; } 00146 long lIndex = 0; long lOffsetId = (pId ? pId->getNameCount() : 0); bool bValidId(false); 00147 long lOffsetOld = params.offset(); this->updateOffset(true, params); 00148 for (int i = 0; i < pId->getNameCount(); i++) 00149 { QVariant v = query.value(lOffsetOld + i); bValidId = (bValidId || qx::trait::is_valid_primary_key(v)); } 00150 if (! bValidId) { return NULL; } 00151 type_data & currData = this->getData(params); 00152 for (int i = 0; i < pId->getNameCount(); i++) 00153 { QVariant v = query.value(lOffsetOld + i); pId->fromVariant((& currData), v, i); } 00154 while ((p = this->nextData(lIndex))) 00155 { p->fromVariant((& currData), query.value(lIndex + lOffsetOld + lOffsetId - 1)); } 00156 return (& currData); 00157 } 00158 00159 }; 00160 00161 } // namespace qx 00162 00163 #endif // _QX_SQL_RELATION_ONE_TO_ONE_H_