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