IxDataMember.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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_DATA_MEMBER_H_
  32. #define _IX_DATA_MEMBER_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file IxDataMember.h
  38. * \author Lionel Marty
  39. * \ingroup QxDataMember
  40. * \brief Common interface for all class properties registered into QxOrm context
  41. */
  42. #ifdef _MSC_VER
  43. #pragma warning(push)
  44. #pragma warning(disable:4996)
  45. #endif // _MSC_VER
  46. #include <QtSql/qsqlquery.h>
  47. #ifndef _QX_NO_JSON
  48. #include <QtCore/qjsonvalue.h>
  49. #endif // _QX_NO_JSON
  50. #include <QxCommon/QxAny.h>
  51. #include <QxCommon/QxBool.h>
  52. #include <QxCommon/QxPropertyBag.h>
  53. #include <QxSerialize/boost/QxSerializeInclude.h>
  54. #include <QxConvert/QxConvert.h>
  55. #ifdef _MSC_VER
  56. #pragma warning(pop)
  57. #endif // _MSC_VER
  58. #define QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(ArchiveInput, ArchiveOutput) \
  59. virtual void toArchive(const void * pOwner, ArchiveOutput & ar) const = 0; \
  60. virtual void fromArchive(void * pOwner, ArchiveInput & ar) = 0;
  61. namespace qx {
  62. class IxDataMemberX;
  63. class IxSqlRelation;
  64. /*!
  65. * \ingroup QxDataMember
  66. * \brief qx::IxDataMember : common interface for all class properties registered into QxOrm context
  67. */
  68. class QX_DLL_EXPORT IxDataMember : public qx::QxPropertyBag
  69. {
  70. private:
  71. struct IxDataMemberImpl;
  72. std::unique_ptr<IxDataMemberImpl> m_pImpl; //!< Private implementation idiom
  73. public:
  74. IxDataMember(const QString & sKey);
  75. IxDataMember(const QString & sKey, long lVersion, bool bSerialize, bool bDao);
  76. virtual ~IxDataMember() = 0;
  77. QString getKey() const;
  78. QString getName() const;
  79. int getNameCount() const;
  80. QString getNameParent() const;
  81. const char * getNamePtr() const;
  82. QString getDescription() const;
  83. QString getFormat() const;
  84. long getVersion() const;
  85. bool getSerialize() const;
  86. bool getDao() const;
  87. QVariant getDefaultValue() const;
  88. QVariant getMinValue() const;
  89. QVariant getMaxValue() const;
  90. int getPrecision() const;
  91. int getMinLength() const;
  92. int getMaxLength() const;
  93. bool getRequired() const;
  94. bool getReadOnly() const;
  95. bool getAutoIncrement() const;
  96. bool getNotNull() const;
  97. bool getIsPrimaryKey() const;
  98. bool getIsIndex() const;
  99. bool getIsUnique() const;
  100. IxDataMemberX * getParent() const;
  101. IxSqlRelation * getSqlRelation() const;
  102. bool hasSqlRelation() const;
  103. bool getAccessDataPointer() const;
  104. virtual QString getType() const;
  105. QString getTypeParent() const;
  106. void setName(const QString & s);
  107. void setNameParent(const QString & s);
  108. void setDescription(const QString & s);
  109. void setFormat(const QString & s);
  110. void setSqlType(const QString & s);
  111. void setSqlAlias(const QString & s);
  112. void setVersion(long l);
  113. void setSerialize(bool b);
  114. void setDao(bool b);
  115. void setDefaultValue(const QVariant & v);
  116. void setPrecision(int i);
  117. void setRequired(bool b);
  118. void setReadOnly(bool b);
  119. void setAutoIncrement(bool b);
  120. void setIsPrimaryKey(bool b);
  121. void setIsIndex(bool b);
  122. void setIsUnique(bool b);
  123. void setParent(IxDataMemberX * p);
  124. void setSqlRelation(IxSqlRelation * p);
  125. void setAccessDataPointer(bool b);
  126. void setMinValue(long lMinValue, const QString & sMessage = QString());
  127. void setMinValue(double dMinValue, const QString & sMessage = QString());
  128. void setMaxValue(long lMaxValue, const QString & sMessage = QString());
  129. void setMaxValue(double dMaxValue, const QString & sMessage = QString());
  130. void setMinLength(int iMinLength, const QString & sMessage = QString());
  131. void setMaxLength(int iMaxLength, const QString & sMessage = QString());
  132. void setNotNull(bool bNotNull, const QString & sMessage = QString());
  133. bool isThereRelationPartOfPrimaryKey(int iIndexNamePK, IxSqlRelation * & pRelation, int & iIndexNameFK) const;
  134. bool isPartOfPrimaryKey(int iIndexNameFK, IxDataMember * & pPrimaryKey, int & iIndexNamePK) const;
  135. void setRelationPartOfPrimaryKey(int iIndexNamePK, IxSqlRelation * pRelation, int iIndexNameFK);
  136. void setPartOfPrimaryKey(int iIndexNameFK, IxDataMember * pPrimaryKey, int iIndexNamePK);
  137. QString getName(int iIndex, const QString & sOtherName = QString()) const;
  138. QString getSqlAlias(const QString & sTable = QString(), bool bClauseWhere = false, int iIndexName = 0) const;
  139. QString getSqlType(int iIndexName = -1) const;
  140. QString getSqlTypeAndParams(int iIndexName = -1) const;
  141. QString getSqlPlaceHolder(const QString & sAppend = QString(), int iIndexName = 0, const QString & sSep = QString(", "), const QString & sOtherName = QString(), bool bCheckFKPartOfPK = false) const;
  142. void setSqlPlaceHolder(QSqlQuery & query, void * pOwner, const QString & sAppend = QString(), const QString & sOtherName = QString(), bool bCheckFKPartOfPK = false) const;
  143. QString getSqlAliasEqualToPlaceHolder(const QString & sTable = QString(), bool bClauseWhere = false, const QString & sAppend = QString(), const QString & sSep = QString(" AND "), bool bCheckFKPartOfPK = false) const;
  144. QString getSqlNameEqualToPlaceHolder(const QString & sAppend = QString(), const QString & sSep = QString(" AND "), bool bCheckFKPartOfPK = false) const;
  145. QString getSqlTablePointNameAsAlias(const QString & sTable, const QString & sSep = QString(", "), const QString & sSuffixAlias = QString(), bool bCheckFKPartOfPK = false, const QString & sCustomAlias = QString()) const;
  146. QString getSqlName(const QString & sSep = QString(", "), const QString & sOtherName = QString(), bool bCheckFKPartOfPK = false) const;
  147. QString getSqlNameAndTypeAndParams(const QString & sSep = QString(", "), const QString & sOtherName = QString(), bool bCheckFKPartOfPK = false) const;
  148. static QString getSqlFromTable(const QString & sTable, const QString & sCustomAlias = QString());
  149. static QString getSqlTableName(const QString & sTable);
  150. static QString getSqlColumnName(const QString & sColumn);
  151. virtual bool isEqual(const void * pOwner1, const void * pOwner2) const = 0;
  152. virtual QVariant toVariant(const void * pOwner, const QString & sFormat, int iIndexName = -1, qx::cvt::context::ctx_type ctx = qx::cvt::context::e_no_context) const = 0;
  153. virtual qx_bool fromVariant(void * pOwner, const QVariant & v, const QString & sFormat, int iIndexName = -1, qx::cvt::context::ctx_type ctx = qx::cvt::context::e_no_context) = 0;
  154. QVariant toVariant(const void * pOwner, int iIndexName = -1, qx::cvt::context::ctx_type ctx = qx::cvt::context::e_no_context) const;
  155. qx_bool fromVariant(void * pOwner, const QVariant & v, int iIndexName = -1, qx::cvt::context::ctx_type ctx = qx::cvt::context::e_no_context);
  156. #ifndef _QX_NO_JSON
  157. virtual QJsonValue toJson(const void * pOwner, const QString & sFormat) const = 0;
  158. virtual qx_bool fromJson(void * pOwner, const QJsonValue & j, const QString & sFormat) = 0;
  159. QJsonValue toJson(const void * pOwner) const;
  160. qx_bool fromJson(void * pOwner, const QJsonValue & j);
  161. #endif // _QX_NO_JSON
  162. protected:
  163. virtual qx::any getDataPtr(const void * pOwner) const = 0;
  164. virtual qx::any getDataPtr(void * pOwner) = 0;
  165. virtual void * getDataVoidPtr(const void * pOwner) const = 0;
  166. virtual void * getDataVoidPtr(void * pOwner) = 0;
  167. public:
  168. qx::any getValueAnyPtr(const void * pOwner) const { return this->getDataPtr(pOwner); }
  169. qx::any getValueAnyPtr(void * pOwner) { return this->getDataPtr(pOwner); }
  170. void * getValueVoidPtr(const void * pOwner) const { return this->getDataVoidPtr(pOwner); }
  171. void * getValueVoidPtr(void * pOwner) { return this->getDataVoidPtr(pOwner); }
  172. template <typename T>
  173. T * getValuePtr(void * pOwner, bool * bOk = NULL)
  174. {
  175. if (bOk) { (* bOk) = false; }
  176. if (! getAccessDataPointer()) { qDebug("[QxOrm] qx::IxDataMember::getValuePtr<T>() : '%s'", "cannot access data-member pointer"); return NULL; }
  177. qx::any a = this->getDataPtr(pOwner);
  178. try { T * t = qx::any_cast<T *>(a); if (bOk) { (* bOk) = (t != NULL); }; return t; }
  179. catch (const qx::bad_any_cast & err) { Q_UNUSED(err); qDebug("[QxOrm] qx::IxDataMember::getValuePtr<T>() : '%s'", "bad any cast exception"); return NULL; }
  180. catch (...) { qDebug("[QxOrm] qx::IxDataMember::getValuePtr<T>() : '%s'", "unknown cast exception"); return NULL; }
  181. }
  182. template <typename T>
  183. T getValue(void * pOwner, bool * bOk = NULL)
  184. {
  185. if (! getAccessDataPointer()) { return qxCannotAccessDataPointer<T, 0>::getValue(this, pOwner, bOk); }
  186. T * t = this->getValuePtr<T>(pOwner, bOk);
  187. return (t ? (* t) : T());
  188. }
  189. template <typename T>
  190. bool setValue(void * pOwner, const T & val)
  191. {
  192. if (! getAccessDataPointer()) { return qxCannotAccessDataPointer<T, 0>::setValue(this, pOwner, val); }
  193. T * t = this->getValuePtr<T>(pOwner);
  194. if (t) { (* t) = val; }
  195. return (t != NULL);
  196. }
  197. private:
  198. template <typename T, int dummy>
  199. struct qxCannotAccessDataPointer
  200. {
  201. static T getValue(IxDataMember * pData, void * pOwner, bool * bOk)
  202. { Q_UNUSED(pData); Q_UNUSED(pOwner); qDebug("[QxOrm] qx::IxDataMember::qxCannotAccessDataPointer<T>::getValue() : '%s'", "type T not supported"); if (bOk) { (* bOk) = false; }; return T(); }
  203. static bool setValue(IxDataMember * pData, void * pOwner, const T & val)
  204. { Q_UNUSED(pData); Q_UNUSED(pOwner); Q_UNUSED(val); qDebug("[QxOrm] qx::IxDataMember::qxCannotAccessDataPointer<T>::setValue() : '%s'", "type T not supported"); return false; }
  205. };
  206. template <int dummy>
  207. struct qxCannotAccessDataPointer<QVariant, dummy>
  208. {
  209. static QVariant getValue(IxDataMember * pData, void * pOwner, bool * bOk)
  210. { if (bOk) { (* bOk) = (pData != NULL); }; return (pData ? pData->toVariant(pOwner, "") : QVariant()); }
  211. static bool setValue(IxDataMember * pData, void * pOwner, const QVariant & val)
  212. { return (pData ? pData->fromVariant(pOwner, val, "").getValue() : false); }
  213. };
  214. template <int dummy>
  215. struct qxCannotAccessDataPointer<QString, dummy>
  216. {
  217. static QString getValue(IxDataMember * pData, void * pOwner, bool * bOk)
  218. { if (bOk) { (* bOk) = (pData != NULL); }; return (pData ? pData->toVariant(pOwner, "").toString() : QString()); }
  219. static bool setValue(IxDataMember * pData, void * pOwner, const QString & val)
  220. { QVariant tmp(val); return (pData ? pData->fromVariant(pOwner, tmp, "").getValue() : false); }
  221. };
  222. public:
  223. #ifdef _QX_ENABLE_BOOST_SERIALIZATION
  224. #if _QX_SERIALIZE_POLYMORPHIC
  225. QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::polymorphic_iarchive, boost::archive::polymorphic_oarchive)
  226. #endif // _QX_SERIALIZE_POLYMORPHIC
  227. #if _QX_SERIALIZE_BINARY
  228. QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::binary_iarchive, boost::archive::binary_oarchive)
  229. #endif // _QX_SERIALIZE_BINARY
  230. #if _QX_SERIALIZE_TEXT
  231. QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::text_iarchive, boost::archive::text_oarchive)
  232. #endif // _QX_SERIALIZE_TEXT
  233. #if _QX_SERIALIZE_XML
  234. QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::xml_iarchive, boost::archive::xml_oarchive)
  235. #endif // _QX_SERIALIZE_XML
  236. #if _QX_SERIALIZE_PORTABLE_BINARY
  237. QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(eos::portable_iarchive, eos::portable_oarchive)
  238. #endif // _QX_SERIALIZE_PORTABLE_BINARY
  239. #if _QX_SERIALIZE_WIDE_BINARY
  240. QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::binary_wiarchive, boost::archive::binary_woarchive)
  241. #endif // _QX_SERIALIZE_WIDE_BINARY
  242. #if _QX_SERIALIZE_WIDE_TEXT
  243. QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::text_wiarchive, boost::archive::text_woarchive)
  244. #endif // _QX_SERIALIZE_WIDE_TEXT
  245. #if _QX_SERIALIZE_WIDE_XML
  246. QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::xml_wiarchive, boost::archive::xml_woarchive)
  247. #endif // _QX_SERIALIZE_WIDE_XML
  248. #endif // _QX_ENABLE_BOOST_SERIALIZATION
  249. private:
  250. #ifdef _QX_ENABLE_BOOST_SERIALIZATION
  251. template <class Archive>
  252. void serialize(Archive & ar, const unsigned int version);
  253. #endif // _QX_ENABLE_BOOST_SERIALIZATION
  254. };
  255. typedef std::shared_ptr<IxDataMember> IxDataMember_ptr;
  256. } // namespace qx
  257. QX_DLL_EXPORT_INLINE_FCT bool operator< (const qx::IxDataMember & i1, const qx::IxDataMember & i2);
  258. QX_DLL_EXPORT_INLINE_FCT bool operator> (const qx::IxDataMember & i1, const qx::IxDataMember & i2);
  259. #endif // _IX_DATA_MEMBER_H_