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