IxSqlElement.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /****************************************************************************
  2. **
  3. ** https://www.qxorm.com/
  4. ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
  5. **
  6. ** This file is part of the QxOrm library
  7. **
  8. ** This software is provided 'as-is', without any express or implied
  9. ** warranty. In no event will the authors be held liable for any
  10. ** damages arising from the use of this software
  11. **
  12. ** Commercial Usage
  13. ** Licensees holding valid commercial QxOrm licenses may use this file in
  14. ** accordance with the commercial license agreement provided with the
  15. ** Software or, alternatively, in accordance with the terms contained in
  16. ** a written agreement between you and Lionel Marty
  17. **
  18. ** GNU General Public License Usage
  19. ** Alternatively, this file may be used under the terms of the GNU
  20. ** General Public License version 3.0 as published by the Free Software
  21. ** Foundation and appearing in the file 'license.gpl3.txt' included in the
  22. ** packaging of this file. Please review the following information to
  23. ** ensure the GNU General Public License version 3.0 requirements will be
  24. ** met : http://www.gnu.org/copyleft/gpl.html
  25. **
  26. ** If you are unsure which license is appropriate for your use, or
  27. ** if you have questions regarding the use of this file, please contact :
  28. ** contact@qxorm.com
  29. **
  30. ****************************************************************************/
  31. #ifndef _IX_SQL_ELEMENT_H_
  32. #define _IX_SQL_ELEMENT_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file IxSqlElement.h
  38. * \author Lionel Marty
  39. * \ingroup QxDao
  40. * \brief Common interface for all SQL elements to build SQL query
  41. */
  42. #ifdef _QX_ENABLE_BOOST_SERIALIZATION
  43. #include <boost/serialization/serialization.hpp>
  44. #include <boost/serialization/split_free.hpp>
  45. #include <boost/serialization/nvp.hpp>
  46. #endif // _QX_ENABLE_BOOST_SERIALIZATION
  47. #include <QtCore/qdatastream.h>
  48. #ifndef _QX_NO_JSON
  49. #include <QtCore/qjsonvalue.h>
  50. #endif // _QX_NO_JSON
  51. #include <QtSql/qsqlquery.h>
  52. #include <QxDao/QxSqlGenerator/IxSqlGenerator.h>
  53. #include <QxSerialize/Qt/QxSerialize_QList.h>
  54. #include <QxSerialize/Qt/QxSerialize_QStringList.h>
  55. #include <QxSerialize/Qt/QxSerialize_QVariant.h>
  56. #include <QxConvert/QxConvert.h>
  57. namespace qx {
  58. namespace dao {
  59. namespace detail {
  60. class IxSqlElement;
  61. } // namespace detail
  62. } // namespace dao
  63. } // namespace qx
  64. QX_DLL_EXPORT QDataStream & operator<< (QDataStream & stream, const qx::dao::detail::IxSqlElement & t) QX_USED;
  65. QX_DLL_EXPORT QDataStream & operator>> (QDataStream & stream, qx::dao::detail::IxSqlElement & t) QX_USED;
  66. #ifndef _QX_NO_JSON
  67. namespace qx {
  68. namespace cvt {
  69. namespace detail {
  70. template <> struct QxConvert_ToJson< qx::dao::detail::IxSqlElement >;
  71. template <> struct QxConvert_FromJson< qx::dao::detail::IxSqlElement >;
  72. QX_DLL_EXPORT QJsonValue QxConvert_ToJson_Helper(const qx::dao::detail::IxSqlElement & t, const QString & format) QX_USED;
  73. QX_DLL_EXPORT qx_bool QxConvert_FromJson_Helper(const QJsonValue & j, qx::dao::detail::IxSqlElement & t, const QString & format) QX_USED;
  74. } // namespace detail
  75. } // namespace cvt
  76. } // namespace qx
  77. #endif // _QX_NO_JSON
  78. namespace qx {
  79. namespace dao {
  80. namespace detail {
  81. /*!
  82. * \ingroup QxDao
  83. * \brief qx::dao::detail::IxSqlElement : common interface for all SQL elements to build SQL query
  84. */
  85. class QX_DLL_EXPORT IxSqlElement
  86. {
  87. friend QX_DLL_EXPORT QDataStream & ::operator<< (QDataStream & stream, const qx::dao::detail::IxSqlElement & t);
  88. friend QX_DLL_EXPORT QDataStream & ::operator>> (QDataStream & stream, qx::dao::detail::IxSqlElement & t);
  89. #ifndef _QX_NO_JSON
  90. friend struct qx::cvt::detail::QxConvert_ToJson< qx::dao::detail::IxSqlElement >;
  91. friend struct qx::cvt::detail::QxConvert_FromJson< qx::dao::detail::IxSqlElement >;
  92. friend QX_DLL_EXPORT QJsonValue qx::cvt::detail::QxConvert_ToJson_Helper(const qx::dao::detail::IxSqlElement & t, const QString & format);
  93. friend QX_DLL_EXPORT qx_bool qx::cvt::detail::QxConvert_FromJson_Helper(const QJsonValue & j, qx::dao::detail::IxSqlElement & t, const QString & format);
  94. #endif // _QX_NO_JSON
  95. public:
  96. enum type_class { _no_type, _sql_compare, _sql_element_temp, _sql_expression, _sql_free_text,
  97. _sql_in, _sql_is_between, _sql_is_null, _sql_limit, _sql_sort, _sql_embed_query };
  98. protected:
  99. int m_iIndex; //!< Index of SQL element to build unique string
  100. QStringList m_lstColumns; //!< List of columns associated to SQL element
  101. QStringList m_lstKeys; //!< List of keys associated to SQL element
  102. QList<QVariant> m_lstValues; //!< List of values associated to SQL element
  103. IxSqlGenerator * m_pSqlGenerator; //!< SQL generator to build SQL query specific for each database
  104. public:
  105. IxSqlElement(int index);
  106. virtual ~IxSqlElement();
  107. void setColumn(const QString & column);
  108. void setColumns(const QStringList & columns);
  109. void setValue(const QVariant & val);
  110. void setValues(const QVariantList & values);
  111. virtual IxSqlElement::type_class getTypeClass() const = 0;
  112. virtual QString toString() const = 0;
  113. virtual void resolve(QSqlQuery & query) const = 0;
  114. virtual void postProcess(QString & sql) const = 0;
  115. virtual void clone(IxSqlElement * other);
  116. #ifdef _QX_ENABLE_BOOST_SERIALIZATION
  117. template <class Archive>
  118. void qxSave(Archive & ar) const
  119. {
  120. QString sExtraSettings = getExtraSettings();
  121. ar << boost::serialization::make_nvp("index", m_iIndex);
  122. ar << boost::serialization::make_nvp("list_columns", m_lstColumns);
  123. ar << boost::serialization::make_nvp("list_keys", m_lstKeys);
  124. ar << boost::serialization::make_nvp("list_values", m_lstValues);
  125. ar << boost::serialization::make_nvp("extra_settings", sExtraSettings);
  126. }
  127. #endif // _QX_ENABLE_BOOST_SERIALIZATION
  128. #ifdef _QX_ENABLE_BOOST_SERIALIZATION
  129. template <class Archive>
  130. void qxLoad(Archive & ar)
  131. {
  132. QString sExtraSettings;
  133. ar >> boost::serialization::make_nvp("index", m_iIndex);
  134. ar >> boost::serialization::make_nvp("list_columns", m_lstColumns);
  135. ar >> boost::serialization::make_nvp("list_keys", m_lstKeys);
  136. ar >> boost::serialization::make_nvp("list_values", m_lstValues);
  137. ar >> boost::serialization::make_nvp("extra_settings", sExtraSettings);
  138. setExtraSettings(sExtraSettings);
  139. }
  140. #endif // _QX_ENABLE_BOOST_SERIALIZATION
  141. protected:
  142. void updateKeys();
  143. virtual QString getExtraSettings() const = 0;
  144. virtual void setExtraSettings(const QString & s) = 0;
  145. };
  146. typedef std::shared_ptr<IxSqlElement> IxSqlElement_ptr;
  147. QX_DLL_EXPORT IxSqlElement_ptr create_sql_element(IxSqlElement::type_class e) QX_USED;
  148. } // namespace detail
  149. } // namespace dao
  150. } // namespace qx
  151. #endif // _IX_SQL_ELEMENT_H_