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