/**************************************************************************** ** ** https://www.qxorm.com/ ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com) ** ** This file is part of the QxOrm library ** ** This software is provided 'as-is', without any express or implied ** warranty. In no event will the authors be held liable for any ** damages arising from the use of this software ** ** Commercial Usage ** Licensees holding valid commercial QxOrm licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and Lionel Marty ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file 'license.gpl3.txt' included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met : http://www.gnu.org/copyleft/gpl.html ** ** If you are unsure which license is appropriate for your use, or ** if you have questions regarding the use of this file, please contact : ** contact@qxorm.com ** ****************************************************************************/ namespace qx { namespace dao { namespace detail { template struct QxDao_Update_WithRelation_Generic { static QSqlError update(const QStringList & relation, const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase) { qx::dao::detail::QxDao_Helper dao(t, pDatabase, "update with relation", new qx::QxSqlQueryBuilder_Update()); if (! dao.isValid()) { return dao.error(); } if (dao.isReadOnly()) { return dao.errReadOnly(); } if (! dao.isValidPrimaryKey(t)) { return dao.errInvalidId(); } if (! dao.updateSqlRelationX(relation)) { return dao.errInvalidRelation(); } if (! pDatabase) { dao.transaction(); } dao.quiet(); qx::QxSqlRelationParams params(0, 0, NULL, (& dao.builder()), (& dao.query()), (& t)); params.setDatabase((& dao.database())); qx::QxSqlRelationLinked * pRelationLinked = dao.getSqlRelationLinked(); if (pRelationLinked) { dao.updateError(pRelationLinked->hierarchyOnBeforeSave(params)); } if (! dao.isValid()) { return dao.error(); } dao.updateError(qx::dao::update_by_query(query, t, (& dao.database()))); if (! dao.isValid()) { return dao.error(); } if (pRelationLinked) { dao.updateError(pRelationLinked->hierarchyOnAfterSave(params)); } if (! dao.isValid()) { return dao.error(); } return dao.error(); } }; template struct QxDao_Update_WithRelation_Container { static QSqlError update(const QStringList & relation, const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase) { typedef typename qx::trait::generic_container::type_value_qx type_item; if (qx::trait::generic_container::size(t) <= 0) { return QSqlError(); } qx::dao::detail::QxDao_Helper_Container dao(t, pDatabase, "update with relation", new qx::QxSqlQueryBuilder_Update()); if (! dao.isValid()) { return dao.error(); } if (dao.isReadOnly()) { return dao.errReadOnly(); } if (! dao.updateSqlRelationX(relation)) { return dao.errInvalidRelation(); } if (! pDatabase) { dao.transaction(); } dao.quiet(); for (typename T::iterator it = t.begin(); it != t.end(); ++it) { if (! updateItem(query, (* it), dao)) { return dao.error(); } } return dao.error(); } private: template static inline bool updateItem(const qx::QxSqlQuery & query, U & item, qx::dao::detail::QxDao_Helper_Container & dao) { bool bUpdateOk = updateItem_Helper::value || qx::trait::is_smart_ptr::value>::update(query, item, dao); if (bUpdateOk) { qx::dao::detail::QxDao_Keep_Original::backup(item); } return bUpdateOk; } template struct updateItem_Helper { static inline bool update(const qx::QxSqlQuery & query, U & item, qx::dao::detail::QxDao_Helper_Container & dao) { return (item ? qx::dao::detail::QxDao_Update_WithRelation_Container::updateItem(query, (* item), dao) : true); } }; template struct updateItem_Helper, false> { static inline bool update(const qx::QxSqlQuery & query, std::pair & item, qx::dao::detail::QxDao_Helper_Container & dao) { return qx::dao::detail::QxDao_Update_WithRelation_Container::updateItem(query, item.second, dao); } }; template struct updateItem_Helper, false> { static inline bool update(const qx::QxSqlQuery & query, const std::pair & item, qx::dao::detail::QxDao_Helper_Container & dao) { return qx::dao::detail::QxDao_Update_WithRelation_Container::updateItem(query, item.second, dao); } }; template struct updateItem_Helper, false> { static inline bool update(const qx::QxSqlQuery & query, QPair & item, qx::dao::detail::QxDao_Helper_Container & dao) { return qx::dao::detail::QxDao_Update_WithRelation_Container::updateItem(query, item.second, dao); } }; template struct updateItem_Helper, false> { static inline bool update(const qx::QxSqlQuery & query, const QPair & item, qx::dao::detail::QxDao_Helper_Container & dao) { return qx::dao::detail::QxDao_Update_WithRelation_Container::updateItem(query, item.second, dao); } }; template struct updateItem_Helper { static bool update(const qx::QxSqlQuery & query, U & item, qx::dao::detail::QxDao_Helper_Container & dao) { qx::QxSqlRelationParams params(0, 0, NULL, (& dao.builder()), (& dao.query()), (& item)); params.setDatabase((& dao.database())); qx::QxSqlRelationLinked * pRelationLinked = dao.getSqlRelationLinked(); if (pRelationLinked) { dao.updateError(pRelationLinked->hierarchyOnBeforeSave(params)); } if (! dao.isValid()) { return false; } dao.updateError(qx::dao::update_by_query(query, item, (& dao.database()))); if (! dao.isValid()) { return false; } if (pRelationLinked) { dao.updateError(pRelationLinked->hierarchyOnAfterSave(params)); } if (! dao.isValid()) { return false; } return dao.isValid(); } }; }; template struct QxDao_Update_WithRelation_Ptr { static inline QSqlError update(const QStringList & relation, const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase) { return (t ? qx::dao::update_by_query_with_relation(relation, query, (* t), pDatabase) : QSqlError()); } }; template struct QxDao_Update_WithRelation { static inline QSqlError update(const QString & relation, const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase) { QStringList lst; if (! relation.isEmpty()) { lst = relation.split("|"); } return QxDao_Update_WithRelation::update(lst, query, t, pDatabase); } static inline QSqlError update(const QStringList & relation, const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase) { typedef typename std::conditional< std::is_pointer::value, qx::dao::detail::QxDao_Update_WithRelation_Ptr, qx::dao::detail::QxDao_Update_WithRelation_Generic >::type type_dao_1; typedef typename std::conditional< qx::trait::is_smart_ptr::value, qx::dao::detail::QxDao_Update_WithRelation_Ptr, type_dao_1 >::type type_dao_2; typedef typename std::conditional< qx::trait::is_container::value, qx::dao::detail::QxDao_Update_WithRelation_Container, type_dao_2 >::type type_dao_3; QSqlError error = type_dao_3::update(relation, query, t, pDatabase); if (! error.isValid()) { qx::dao::detail::QxDao_Keep_Original::backup(t); } return error; } }; } // namespace detail } // namespace dao } // namespace qx