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