QxOrm  1.4.9
C++ Object Relational Mapping library
QxDao_IsDirty.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** https://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_DAO_IS_DIRTY_H_
00033 #define _QX_DAO_IS_DIRTY_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
00039 #include <QtCore/qstringlist.h>
00040 
00041 #include <QxDao/QxSqlQueryBuilder.h>
00042 #include <QxDao/IxSqlRelation.h>
00043 
00044 #include <QxTraits/is_qx_registered.h>
00045 #include <QxTraits/is_container.h>
00046 #include <QxTraits/is_smart_ptr.h>
00047 #include <QxTraits/generic_container.h>
00048 
00049 namespace qx {
00050 namespace dao {
00051 namespace detail {
00052 
00053 template <class T>
00054 inline void is_dirty(const T & obj1, const T & obj2, QStringList & lstDiff);
00055 
00056 template <class T>
00057 struct QxDao_IsDirty_Generic
00058 {
00059 
00060    static void compare(const T & obj1, const T & obj2, QStringList & lstDiff)
00061    {
00062       static_assert(qx::trait::is_qx_registered<T>::value, "qx::trait::is_qx_registered<T>::value");
00063 
00064       qx::QxSqlQueryBuilder_Count<T> builder; builder.init();
00065       qx::IxDataMember * pId = builder.getDataId();
00066       if (pId && (! pId->isEqual((& obj1), (& obj2)))) { lstDiff.append(pId->getKey()); }
00067 
00068       long l = 0;
00069       qx::IxDataMember * p = NULL;
00070       while ((p = builder.nextData(l)))
00071       { if (p && (! p->isEqual((& obj1), (& obj2)))) { lstDiff.append(p->getKey()); } }
00072    }
00073 
00074 };
00075 
00076 template <class T>
00077 struct QxDao_IsDirty_Container
00078 {
00079 
00080    static void compare(const T & obj1, const T & obj2, QStringList & lstDiff)
00081    {
00082       if (qx::trait::generic_container<T>::size(obj1) <= 0) { return; }
00083       if (qx::trait::generic_container<T>::size(obj1) != qx::trait::generic_container<T>::size(obj2)) { lstDiff.append("*"); return; }
00084 
00085       long lCurrIndex = 0;
00086       typename T::const_iterator it2 = obj2.begin();
00087 
00088       for (typename T::const_iterator it1 = obj1.begin(); it1 != obj1.end(); ++it1)
00089       {
00090          QStringList lstDiffItem;
00091          qx::dao::detail::is_dirty((* it1), (* it2), lstDiffItem);
00092          if (lstDiffItem.count() > 0) { lstDiff.append(QString::number(lCurrIndex) + "|" + lstDiffItem.join("|")); }
00093          ++lCurrIndex; ++it2;
00094       }
00095    }
00096 
00097 };
00098 
00099 template <class T>
00100 struct QxDao_IsDirty_Ptr
00101 {
00102 
00103    static void compare(const T & obj1, const T & obj2, QStringList & lstDiff)
00104    { qx::dao::detail::is_dirty((* obj1), (* obj2), lstDiff); }
00105 
00106 };
00107 
00108 template <class T>
00109 struct QxDao_IsDirty
00110 {
00111 
00112    static void compare(const T & obj1, const T & obj2, QStringList & lstDiff)
00113    {
00114       typedef typename std::conditional< std::is_pointer<T>::value, qx::dao::detail::QxDao_IsDirty_Ptr<T>, qx::dao::detail::QxDao_IsDirty_Generic<T> >::type type_dao_1;
00115       typedef typename std::conditional< qx::trait::is_smart_ptr<T>::value, qx::dao::detail::QxDao_IsDirty_Ptr<T>, type_dao_1 >::type type_dao_2;
00116       typedef typename std::conditional< qx::trait::is_container<T>::value, qx::dao::detail::QxDao_IsDirty_Container<T>, type_dao_2 >::type type_dao_3;
00117       type_dao_3::compare(obj1, obj2, lstDiff);
00118    }
00119 
00120 };
00121 
00122 template <class T>
00123 inline void is_dirty(const T & obj1, const T & obj2, QStringList & lstDiff)
00124 { return qx::dao::detail::QxDao_IsDirty<T>::compare(obj1, obj2, lstDiff); }
00125 
00126 } // namespace detail
00127 } // namespace dao
00128 } // namespace qx
00129 
00130 #endif // _QX_DAO_IS_DIRTY_H_