QxOrm
1.5.0
C++ Object Relational Mapping library
|
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_DATA_MEMBER_H_ 00033 #define _IX_DATA_MEMBER_H_ 00034 00035 #ifdef _MSC_VER 00036 #pragma once 00037 #endif 00038 00046 #ifdef _MSC_VER 00047 #pragma warning(push) 00048 #pragma warning(disable:4996) 00049 #endif // _MSC_VER 00050 00051 #include <QtCore/qmutex.h> 00052 #include <QtSql/qsqlquery.h> 00053 00054 #ifndef _QX_NO_JSON 00055 #include <QtCore/qjsonvalue.h> 00056 #endif // _QX_NO_JSON 00057 00058 #include <QxCommon/QxAny.h> 00059 #include <QxCommon/QxBool.h> 00060 #include <QxCommon/QxPropertyBag.h> 00061 00062 #include <QxCollection/QxCollection.h> 00063 00064 #include <QxSerialize/boost/QxSerializeInclude.h> 00065 00066 #include <QxConvert/QxConvert.h> 00067 00068 #ifdef _MSC_VER 00069 #pragma warning(pop) 00070 #endif // _MSC_VER 00071 00072 #define QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(ArchiveInput, ArchiveOutput) \ 00073 virtual void toArchive(const void * pOwner, ArchiveOutput & ar) const = 0; \ 00074 virtual void fromArchive(void * pOwner, ArchiveInput & ar) = 0; 00075 00076 namespace qx { 00077 00078 class IxDataMemberX; 00079 class IxSqlRelation; 00080 class IxSqlQueryBuilder; 00081 struct IxDataMemberSqlCallbackParams; 00082 00083 namespace dao { 00084 namespace detail { 00085 00086 class IxDao_Helper; 00087 00088 } // namespace detail 00089 } // namespace dao 00090 00095 class QX_DLL_EXPORT IxDataMember : public qx::QxPropertyBag 00096 { 00097 00098 template <typename DataType, class Owner> 00099 friend class QxDataMember; 00100 00101 public: 00102 00103 typedef std::function<void (IxDataMemberSqlCallbackParams &)> type_fct_sql_callback; 00104 00105 private: 00106 00107 struct IxDataMemberImpl; 00108 std::unique_ptr<IxDataMemberImpl> m_pImpl; 00109 00110 static QMutex m_mutex; 00111 00112 public: 00113 00114 IxDataMember(const QString & sKey, long lVersion, bool bSerialize, bool bDao, IxDataMember * pImpl); 00115 virtual ~IxDataMember() = 0; 00116 00117 QString getKey() const; 00118 QString getName() const; 00119 int getNameCount() const; 00120 QString getNameParent() const; 00121 const char * getNamePtr() const; 00122 QString getDescription() const; 00123 QString getFormat() const; 00124 long getVersion() const; 00125 bool getSerialize() const; 00126 bool getDao() const; 00127 QVariant getDefaultValue() const; 00128 QVariant getMinValue() const; 00129 QVariant getMaxValue() const; 00130 int getPrecision() const; 00131 int getMinLength() const; 00132 int getMaxLength() const; 00133 bool getRequired() const; 00134 bool getReadOnly() const; 00135 bool getAutoIncrement() const; 00136 bool getNotNull() const; 00137 bool getIsPrimaryKey() const; 00138 bool getIsIndex() const; 00139 bool getIsUnique() const; 00140 IxDataMemberX * getParent() const; 00141 IxSqlRelation * getSqlRelation() const; 00142 bool hasSqlRelation() const; 00143 bool getAccessDataPointer() const; 00144 virtual QString getType() const; 00145 QString getTypeParent() const; 00146 IxDataMember * getPImpl() const; 00147 00148 void setName(const QString & s); 00149 void setNameParent(const QString & s); 00150 void setDescription(const QString & s); 00151 void setFormat(const QString & s); 00152 void setSqlType(const QString & s); 00153 void setSqlAlias(const QString & s); 00154 void setVersion(long l); 00155 void setSerialize(bool b); 00156 void setDao(bool b); 00157 void setDefaultValue(const QVariant & v); 00158 void setPrecision(int i); 00159 void setRequired(bool b); 00160 void setReadOnly(bool b); 00161 void setAutoIncrement(bool b); 00162 void setIsPrimaryKey(bool b); 00163 void setIsIndex(bool b); 00164 void setIsUnique(bool b); 00165 void setParent(IxDataMemberX * p); 00166 void setSqlRelation(IxSqlRelation * p); 00167 void setAccessDataPointer(bool b); 00168 00169 void setMinValue(long lMinValue, const QString & sMessage = QString()); 00170 void setMinValue(double dMinValue, const QString & sMessage = QString()); 00171 void setMaxValue(long lMaxValue, const QString & sMessage = QString()); 00172 void setMaxValue(double dMaxValue, const QString & sMessage = QString()); 00173 void setMinLength(int iMinLength, const QString & sMessage = QString()); 00174 void setMaxLength(int iMaxLength, const QString & sMessage = QString()); 00175 void setNotNull(bool bNotNull, const QString & sMessage = QString()); 00176 00177 bool isThereRelationPartOfPrimaryKey(int iIndexNamePK, IxSqlRelation * & pRelation, int & iIndexNameFK) const; 00178 bool isPartOfPrimaryKey(int iIndexNameFK, IxDataMember * & pPrimaryKey, int & iIndexNamePK) const; 00179 void setRelationPartOfPrimaryKey(int iIndexNamePK, IxSqlRelation * pRelation, int iIndexNameFK); 00180 void setPartOfPrimaryKey(int iIndexNameFK, IxDataMember * pPrimaryKey, int iIndexNamePK); 00181 00182 QString getName(int iIndex, const QString & sOtherName = QString()) const; 00183 QString getSqlAlias(const QString & sTable = QString(), bool bClauseWhere = false, int iIndexName = 0, qx::IxSqlQueryBuilder * pSqlQueryBuilder = NULL) const; 00184 QString getSqlType(int iIndexName = -1) const; 00185 QString getSqlTypeAndParams(int iIndexName = -1) const; 00186 QString getSqlPlaceHolder(const QString & sAppend = QString(), int iIndexName = 0, const QString & sSep = QString(", "), const QString & sOtherName = QString(), bool bCheckFKPartOfPK = false) const; 00187 void setSqlPlaceHolder(QSqlQuery & query, void * pOwner, const QString & sAppend = QString(), const QString & sOtherName = QString(), bool bCheckFKPartOfPK = false, qx::QxCollection<QString, QVariantList> * pLstExecBatch = NULL) const; 00188 QString getSqlAliasEqualToPlaceHolder(const QString & sTable = QString(), bool bClauseWhere = false, const QString & sAppend = QString(), const QString & sSep = QString(" AND "), bool bCheckFKPartOfPK = false, qx::IxSqlQueryBuilder * pSqlQueryBuilder = NULL) const; 00189 QString getSqlNameEqualToPlaceHolder(const QString & sAppend = QString(), const QString & sSep = QString(" AND "), bool bCheckFKPartOfPK = false, qx::IxSqlQueryBuilder * pSqlQueryBuilder = NULL) const; 00190 QString getSqlTablePointNameAsAlias(const QString & sTable, const QString & sSep = QString(", "), const QString & sSuffixAlias = QString(), bool bCheckFKPartOfPK = false, const QString & sCustomAlias = QString(), qx::IxSqlQueryBuilder * pSqlQueryBuilder = NULL) const; 00191 QString getSqlName(const QString & sSep = QString(", "), const QString & sOtherName = QString(), bool bCheckFKPartOfPK = false, qx::IxSqlQueryBuilder * pSqlQueryBuilder = NULL) const; 00192 QString getSqlNameAndTypeAndParams(const QString & sSep = QString(", "), const QString & sOtherName = QString(), bool bCheckFKPartOfPK = false) const; 00193 00194 void customGetSqlName(type_fct_sql_callback fct); 00195 void customGetSqlTablePointNameAsAlias(type_fct_sql_callback fct); 00196 void customGetSqlNameEqualToPlaceHolder(type_fct_sql_callback fct); 00197 void customGetSqlAliasEqualToPlaceHolder(type_fct_sql_callback fct); 00198 void customGetSqlAlias(type_fct_sql_callback fct); 00199 00200 static QString getSqlFromTable(const QString & sTable, const QString & sCustomAlias = QString()); 00201 static QString getSqlTableName(const QString & sTable); 00202 static QString getSqlColumnName(const QString & sColumn); 00203 static QString getSqlTableNameAlias(const QString & sTable); 00204 static QString getSqlColumnNameAlias(const QString & sColumn); 00205 00206 virtual bool isEqual(const void * pOwner1, const void * pOwner2) const = 0; 00207 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; 00208 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; 00209 00210 QVariant toVariant(const void * pOwner, int iIndexName = -1, qx::cvt::context::ctx_type ctx = qx::cvt::context::e_no_context) const; 00211 qx_bool fromVariant(void * pOwner, const QVariant & v, int iIndexName = -1, qx::cvt::context::ctx_type ctx = qx::cvt::context::e_no_context); 00212 00213 #ifndef _QX_NO_JSON 00214 virtual QJsonValue toJson(const void * pOwner, const QString & sFormat) const = 0; 00215 virtual qx_bool fromJson(void * pOwner, const QJsonValue & j, const QString & sFormat) = 0; 00216 00217 QJsonValue toJson(const void * pOwner) const; 00218 qx_bool fromJson(void * pOwner, const QJsonValue & j); 00219 #endif // _QX_NO_JSON 00220 00221 protected: 00222 00223 virtual qx::any getDataPtr(const void * pOwner) const = 0; 00224 virtual qx::any getDataPtr(void * pOwner) = 0; 00225 virtual void * getDataVoidPtr(const void * pOwner) const = 0; 00226 virtual void * getDataVoidPtr(void * pOwner) = 0; 00227 00228 public: 00229 00230 qx::any getValueAnyPtr(const void * pOwner) const { return this->getDataPtr(pOwner); } 00231 qx::any getValueAnyPtr(void * pOwner) { return this->getDataPtr(pOwner); } 00232 void * getValueVoidPtr(const void * pOwner) const { return this->getDataVoidPtr(pOwner); } 00233 void * getValueVoidPtr(void * pOwner) { return this->getDataVoidPtr(pOwner); } 00234 00235 template <typename T> 00236 T * getValuePtr(void * pOwner, bool * bOk = NULL) 00237 { 00238 if (bOk) { (* bOk) = false; } 00239 if (! getAccessDataPointer()) { qDebug("[QxOrm] qx::IxDataMember::getValuePtr<T>() : '%s'", "cannot access data-member pointer"); return NULL; } 00240 qx::any a = this->getDataPtr(pOwner); 00241 try { T * t = qx::any_cast<T *>(a); if (bOk) { (* bOk) = (t != NULL); }; return t; } 00242 catch (const qx::bad_any_cast & err) { Q_UNUSED(err); qDebug("[QxOrm] qx::IxDataMember::getValuePtr<T>() : '%s'", "bad any cast exception"); return NULL; } 00243 catch (...) { qDebug("[QxOrm] qx::IxDataMember::getValuePtr<T>() : '%s'", "unknown cast exception"); return NULL; } 00244 } 00245 00246 template <typename T> 00247 T getValue(void * pOwner, bool * bOk = NULL) 00248 { 00249 if (! getAccessDataPointer()) { return qxCannotAccessDataPointer<T, 0>::getValue(this, pOwner, bOk); } 00250 T * t = this->getValuePtr<T>(pOwner, bOk); 00251 return (t ? (* t) : T()); 00252 } 00253 00254 template <typename T> 00255 bool setValue(void * pOwner, const T & val) 00256 { 00257 if (! getAccessDataPointer()) { return qxCannotAccessDataPointer<T, 0>::setValue(this, pOwner, val); } 00258 T * t = this->getValuePtr<T>(pOwner); 00259 if (t) { (* t) = val; } 00260 return (t != NULL); 00261 } 00262 00263 private: 00264 00265 template <typename T, int dummy> 00266 struct qxCannotAccessDataPointer 00267 { 00268 static T getValue(IxDataMember * pData, void * pOwner, bool * bOk) 00269 { Q_UNUSED(pData); Q_UNUSED(pOwner); qDebug("[QxOrm] qx::IxDataMember::qxCannotAccessDataPointer<T>::getValue() : '%s'", "type T not supported"); if (bOk) { (* bOk) = false; }; return T(); } 00270 static bool setValue(IxDataMember * pData, void * pOwner, const T & val) 00271 { Q_UNUSED(pData); Q_UNUSED(pOwner); Q_UNUSED(val); qDebug("[QxOrm] qx::IxDataMember::qxCannotAccessDataPointer<T>::setValue() : '%s'", "type T not supported"); return false; } 00272 }; 00273 00274 template <int dummy> 00275 struct qxCannotAccessDataPointer<QVariant, dummy> 00276 { 00277 static QVariant getValue(IxDataMember * pData, void * pOwner, bool * bOk) 00278 { if (bOk) { (* bOk) = (pData != NULL); }; return (pData ? pData->toVariant(pOwner, "") : QVariant()); } 00279 static bool setValue(IxDataMember * pData, void * pOwner, const QVariant & val) 00280 { return (pData ? pData->fromVariant(pOwner, val, "").getValue() : false); } 00281 }; 00282 00283 template <int dummy> 00284 struct qxCannotAccessDataPointer<QString, dummy> 00285 { 00286 static QString getValue(IxDataMember * pData, void * pOwner, bool * bOk) 00287 { if (bOk) { (* bOk) = (pData != NULL); }; return (pData ? pData->toVariant(pOwner, "").toString() : QString()); } 00288 static bool setValue(IxDataMember * pData, void * pOwner, const QString & val) 00289 { QVariant tmp(val); return (pData ? pData->fromVariant(pOwner, tmp, "").getValue() : false); } 00290 }; 00291 00292 public: 00293 00294 #ifdef _QX_ENABLE_BOOST_SERIALIZATION 00295 00296 #if _QX_SERIALIZE_POLYMORPHIC 00297 QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::polymorphic_iarchive, boost::archive::polymorphic_oarchive) 00298 #endif // _QX_SERIALIZE_POLYMORPHIC 00299 00300 #if _QX_SERIALIZE_BINARY 00301 QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::binary_iarchive, boost::archive::binary_oarchive) 00302 #endif // _QX_SERIALIZE_BINARY 00303 00304 #if _QX_SERIALIZE_TEXT 00305 QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::text_iarchive, boost::archive::text_oarchive) 00306 #endif // _QX_SERIALIZE_TEXT 00307 00308 #if _QX_SERIALIZE_XML 00309 QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::xml_iarchive, boost::archive::xml_oarchive) 00310 #endif // _QX_SERIALIZE_XML 00311 00312 #if _QX_SERIALIZE_PORTABLE_BINARY 00313 QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(eos::portable_iarchive, eos::portable_oarchive) 00314 #endif // _QX_SERIALIZE_PORTABLE_BINARY 00315 00316 #if _QX_SERIALIZE_WIDE_BINARY 00317 QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::binary_wiarchive, boost::archive::binary_woarchive) 00318 #endif // _QX_SERIALIZE_WIDE_BINARY 00319 00320 #if _QX_SERIALIZE_WIDE_TEXT 00321 QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::text_wiarchive, boost::archive::text_woarchive) 00322 #endif // _QX_SERIALIZE_WIDE_TEXT 00323 00324 #if _QX_SERIALIZE_WIDE_XML 00325 QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::xml_wiarchive, boost::archive::xml_woarchive) 00326 #endif // _QX_SERIALIZE_WIDE_XML 00327 00328 #endif // _QX_ENABLE_BOOST_SERIALIZATION 00329 00330 private: 00331 00332 #ifdef _QX_ENABLE_BOOST_SERIALIZATION 00333 template <class Archive> 00334 void serialize(Archive & ar, const unsigned int version); 00335 #endif // _QX_ENABLE_BOOST_SERIALIZATION 00336 00337 }; 00338 00339 typedef std::shared_ptr<IxDataMember> IxDataMember_ptr; 00340 00345 struct IxDataMemberSqlCallbackParams 00346 { 00347 00348 const IxDataMember * pDataMember; 00349 QString & sSQL; 00350 QString sTable; 00351 QString sSep; 00352 QString sCustomAlias; 00353 QString sSuffixAlias; 00354 QString sAppend; 00355 QString sOtherName; 00356 bool bClauseWhere; 00357 bool bCheckFKPartOfPK; 00358 int iIndexName; 00359 qx::dao::detail::IxDao_Helper * pDaoHelper; 00360 qx::IxSqlQueryBuilder * pSqlQueryBuilder; 00361 00362 IxDataMemberSqlCallbackParams(const IxDataMember * p, QString & sql); 00363 ~IxDataMemberSqlCallbackParams(); 00364 00365 }; 00366 00367 } // namespace qx 00368 00369 QX_DLL_EXPORT_INLINE_FCT bool operator< (const qx::IxDataMember & i1, const qx::IxDataMember & i2); 00370 QX_DLL_EXPORT_INLINE_FCT bool operator> (const qx::IxDataMember & i1, const qx::IxDataMember & i2); 00371 00372 #endif // _IX_DATA_MEMBER_H_