QxOrm  1.4.5
C++ Object Relational Mapping library
IxSqlElement.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 _IX_SQL_ELEMENT_H_
00033 #define _IX_SQL_ELEMENT_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
00046 #ifdef _QX_ENABLE_BOOST_SERIALIZATION
00047 #include <boost/serialization/serialization.hpp>
00048 #include <boost/serialization/split_free.hpp>
00049 #include <boost/serialization/nvp.hpp>
00050 #endif // _QX_ENABLE_BOOST_SERIALIZATION
00051 
00052 #include <QtCore/qdatastream.h>
00053 
00054 #ifndef _QX_NO_JSON
00055 #include <QtCore/qjsonvalue.h>
00056 #endif // _QX_NO_JSON
00057 
00058 #include <QtSql/qsqlquery.h>
00059 
00060 #include <QxDao/QxSqlGenerator/IxSqlGenerator.h>
00061 
00062 #include <QxSerialize/Qt/QxSerialize_QList.h>
00063 #include <QxSerialize/Qt/QxSerialize_QStringList.h>
00064 #include <QxSerialize/Qt/QxSerialize_QVariant.h>
00065 
00066 #include <QxConvert/QxConvert.h>
00067 
00068 namespace qx {
00069 namespace dao {
00070 namespace detail {
00071 class IxSqlElement;
00072 } // namespace detail
00073 } // namespace dao
00074 } // namespace qx
00075 
00076 QX_DLL_EXPORT QDataStream & operator<< (QDataStream & stream, const qx::dao::detail::IxSqlElement & t) QX_USED;
00077 QX_DLL_EXPORT QDataStream & operator>> (QDataStream & stream, qx::dao::detail::IxSqlElement & t) QX_USED;
00078 
00079 #ifndef _QX_NO_JSON
00080 namespace qx {
00081 namespace cvt {
00082 namespace detail {
00083 template <> struct QxConvert_ToJson< qx::dao::detail::IxSqlElement >;
00084 template <> struct QxConvert_FromJson< qx::dao::detail::IxSqlElement >;
00085 QX_DLL_EXPORT QJsonValue QxConvert_ToJson_Helper(const qx::dao::detail::IxSqlElement & t, const QString & format) QX_USED;
00086 QX_DLL_EXPORT qx_bool QxConvert_FromJson_Helper(const QJsonValue & j, qx::dao::detail::IxSqlElement & t, const QString & format) QX_USED;
00087 } // namespace detail
00088 } // namespace cvt
00089 } // namespace qx
00090 #endif // _QX_NO_JSON
00091 
00092 namespace qx {
00093 namespace dao {
00094 namespace detail {
00095 
00100 class QX_DLL_EXPORT IxSqlElement
00101 {
00102 
00103    friend QX_DLL_EXPORT QDataStream & ::operator<< (QDataStream & stream, const qx::dao::detail::IxSqlElement & t);
00104    friend QX_DLL_EXPORT QDataStream & ::operator>> (QDataStream & stream, qx::dao::detail::IxSqlElement & t);
00105 
00106 #ifndef _QX_NO_JSON
00107    friend struct qx::cvt::detail::QxConvert_ToJson< qx::dao::detail::IxSqlElement >;
00108    friend struct qx::cvt::detail::QxConvert_FromJson< qx::dao::detail::IxSqlElement >;
00109    friend QX_DLL_EXPORT QJsonValue qx::cvt::detail::QxConvert_ToJson_Helper(const qx::dao::detail::IxSqlElement & t, const QString & format);
00110    friend QX_DLL_EXPORT qx_bool qx::cvt::detail::QxConvert_FromJson_Helper(const QJsonValue & j, qx::dao::detail::IxSqlElement & t, const QString & format);
00111 #endif // _QX_NO_JSON
00112 
00113 public:
00114 
00115    enum type_class { _no_type, _sql_compare, _sql_element_temp, _sql_expression, _sql_free_text, 
00116                      _sql_in, _sql_is_between, _sql_is_null, _sql_limit, _sql_sort };
00117 
00118 protected:
00119 
00120    int               m_iIndex;            
00121    QStringList       m_lstColumns;        
00122    QStringList       m_lstKeys;           
00123    QList<QVariant>   m_lstValues;         
00124    IxSqlGenerator *  m_pSqlGenerator;     
00125 
00126 public:
00127 
00128    IxSqlElement(int index);
00129    virtual ~IxSqlElement();
00130 
00131    void setColumn(const QString & column);
00132    void setColumns(const QStringList & columns);
00133    void setValue(const QVariant & val);
00134    void setValues(const QVariantList & values);
00135 
00136    virtual IxSqlElement::type_class getTypeClass() const = 0;
00137 
00138    virtual QString toString() const = 0;
00139    virtual void resolve(QSqlQuery & query) const = 0;
00140    virtual void postProcess(QString & sql) const = 0;
00141 
00142    virtual void clone(IxSqlElement * other);
00143 
00144 #ifdef _QX_ENABLE_BOOST_SERIALIZATION
00145    template <class Archive>
00146    void qxSave(Archive & ar) const
00147    {
00148       QString sExtraSettings = getExtraSettings();
00149       ar << boost::serialization::make_nvp("index", m_iIndex);
00150       ar << boost::serialization::make_nvp("list_columns", m_lstColumns);
00151       ar << boost::serialization::make_nvp("list_keys", m_lstKeys);
00152       ar << boost::serialization::make_nvp("list_values", m_lstValues);
00153       ar << boost::serialization::make_nvp("extra_settings", sExtraSettings);
00154    }
00155 #endif // _QX_ENABLE_BOOST_SERIALIZATION
00156 
00157 #ifdef _QX_ENABLE_BOOST_SERIALIZATION
00158    template <class Archive>
00159    void qxLoad(Archive & ar)
00160    {
00161       QString sExtraSettings;
00162       ar >> boost::serialization::make_nvp("index", m_iIndex);
00163       ar >> boost::serialization::make_nvp("list_columns", m_lstColumns);
00164       ar >> boost::serialization::make_nvp("list_keys", m_lstKeys);
00165       ar >> boost::serialization::make_nvp("list_values", m_lstValues);
00166       ar >> boost::serialization::make_nvp("extra_settings", sExtraSettings);
00167       setExtraSettings(sExtraSettings);
00168    }
00169 #endif // _QX_ENABLE_BOOST_SERIALIZATION
00170 
00171 protected:
00172 
00173    void updateKeys();
00174 
00175    virtual QString getExtraSettings() const = 0;
00176    virtual void setExtraSettings(const QString & s) = 0;
00177 
00178 };
00179 
00180 typedef std::shared_ptr<IxSqlElement> IxSqlElement_ptr;
00181 
00182 QX_DLL_EXPORT IxSqlElement_ptr create_sql_element(IxSqlElement::type_class e) QX_USED;
00183 
00184 } // namespace detail
00185 } // namespace dao
00186 } // namespace qx
00187 
00188 #endif // _IX_SQL_ELEMENT_H_