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