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