| QxOrm 1.3.1 C++ Object Relational Mapping library

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

IxDataMember.h
Go to the documentation of this file.

    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 <boost/any.hpp>
    00052
    00053 #include <QxCommon/QxBool.h>
    00054 #include <QxCommon/QxPropertyBag.h>
    00055
    00056 #include <QxSerialize/boost/QxSerializeInclude.h>
    00057
    00058 #include <QxDao/IxSqlRelation.h>
    00059
    00060 #ifdef _MSC_VER
    00061 #pragma warning(pop)
    00062 #endif // _MSC_VER
    00063
    00064 #define QX_CONSTRUCT_IX_DATA_MEMBER() \
    00065 m_iPrecision(6), m_iMinLength(-1), m_iMaxLength(-1), m_bRequired(false), \
    00066 m_bReadOnly(false), m_bAutoIncrement(false), m_bNotNull(false), \
    00067 m_bIsPrimaryKey(false), m_bAccessDataPointer(false), m_bIndex(false), \
    00068 m_bUnique(false), m_pName(NULL), m_pParent(NULL)
    00069
    00070 #define QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(ArchiveInput, ArchiveOutput) \
    00071 virtual void toArchive(const void * pOwner, ArchiveOutput & ar) const = 0; \
    00072 virtual void fromArchive(void * pOwner, ArchiveInput & ar) = 0;
    00073
    00074 namespace qx {
    00075
    00076 class IxDataMemberX;
    00077
    00082 class QX_DLL_EXPORT IxDataMember : public qx::QxPropertyBag
    00083 {
    00084
    00085 protected:
    00086
    00087    QString     m_sKey;
    00088    QString     m_sName;
    00089    QString     m_sNameParent;
    00090    QString     m_sDescription;
    00091    QString     m_sFormat;
    00092    QString     m_sSqlType;
    00093    QString     m_sSqlAlias;
    00094    long        m_lVersion;
    00095    bool        m_bSerialize;
    00096    bool        m_bDao;
    00097    QVariant    m_vDefaultValue;
    00098    QVariant    m_vMinValue;
    00099    QVariant    m_vMaxValue;
    00100    int         m_iPrecision;
    00101    int         m_iMinLength;
    00102    int         m_iMaxLength;
    00103    bool        m_bRequired;
    00104    bool        m_bReadOnly;
    00105    bool        m_bAutoIncrement;
    00106    bool        m_bNotNull;
    00107    bool        m_bIsPrimaryKey;
    00108    bool        m_bAccessDataPointer;
    00109    bool        m_bIndex;
    00110    bool        m_bUnique;
    00111
    00112    QByteArray m_byteName;
    00113    const char * m_pName;
    00114    QStringList m_lstNames;
    00115
    00116    boost::scoped_ptr<IxSqlRelation> m_pSqlRelation;
    00117    IxDataMemberX * m_pParent;
    00118
    00119 public:
    00120
    00121    IxDataMember(const QString & sKey) : qx::QxPropertyBag(), m_sKey(sKey), m_lVersion(-1), m_bSerialize(true), m_bDao(true), QX_CONSTRUCT_IX_DATA_MEMBER() { qAssert(! m_sKey.isEmpty()); updateNamePtr(); }
    00122    IxDataMember(const QString & sKey, long lVersion, bool bSerialize, bool bDao) : qx::QxPropertyBag(), m_sKey(sKey), m_lVersion(lVersion), m_bSerialize(bSerialize), m_bDao(bDao), QX_CONSTRUCT_IX_DATA_MEMBER() { qAssert(! m_sKey.isEmpty()); updateNamePtr(); }
    00123    virtual ~IxDataMember() = 0;
    00124
    00125    inline QString getKey() const                   { return m_sKey; }
    00126    inline QString getName() const                  { return (m_sName.isEmpty() ? m_sKey : m_sName); }
    00127    inline int getNameCount() const                 { return m_lstNames.count(); }
    00128    inline QString getNameParent() const            { return m_sNameParent; }
    00129    inline const char * getNamePtr() const          { return m_pName; }
    00130    inline QString getDescription() const           { return m_sDescription; }
    00131    inline QString getFormat() const                { return m_sFormat; }
    00132    inline long getVersion() const                  { return m_lVersion; }
    00133    inline bool getSerialize() const                { return m_bSerialize; }
    00134    inline bool getDao() const                      { return m_bDao; }
    00135    inline QVariant getDefaultValue() const         { return m_vDefaultValue; }
    00136    inline QVariant getMinValue() const             { return m_vMinValue; }
    00137    inline QVariant getMaxValue() const             { return m_vMaxValue; }
    00138    inline int getPrecision() const                 { return m_iPrecision; }
    00139    inline int getMinLength() const                 { return m_iMinLength; }
    00140    inline int getMaxLength() const                 { return m_iMaxLength; }
    00141    inline bool getRequired() const                 { return m_bRequired; }
    00142    inline bool getReadOnly() const                 { return m_bReadOnly; }
    00143    inline bool getAutoIncrement() const            { return m_bAutoIncrement; }
    00144    inline bool getNotNull() const                  { return m_bNotNull; }
    00145    inline bool getIsPrimaryKey() const             { return m_bIsPrimaryKey; }
    00146    inline bool getIsIndex() const                  { return m_bIndex; }
    00147    inline bool getIsUnique() const                 { return m_bUnique; }
    00148    inline IxDataMemberX * getParent() const        { return m_pParent; }
    00149    inline IxSqlRelation * getSqlRelation() const   { return m_pSqlRelation.get(); }
    00150    inline bool hasSqlRelation() const              { return (m_pSqlRelation.get() != NULL); }
    00151
    00152    inline void setName(const QString & sName)                  { m_sName = sName; updateNamePtr(); }
    00153    inline void setNameParent(const QString & sName)            { m_sNameParent = sName; }
    00154    inline void setDescription(const QString & sDesc)           { m_sDescription = sDesc; }
    00155    inline void setFormat(const QString & sFormat)              { m_sFormat = sFormat; }
    00156    inline void setSqlType(const QString & sSqlType)            { m_sSqlType = sSqlType; }
    00157    inline void setSqlAlias(const QString & sSqlAlias)          { m_sSqlAlias = sSqlAlias; }
    00158    inline void setVersion(long lVersion)                       { m_lVersion = lVersion; }
    00159    inline void setSerialize(bool bSerialize)                   { m_bSerialize = bSerialize; }
    00160    inline void setDao(bool bDao)                               { m_bDao = bDao; }
    00161    inline void setDefaultValue(const QVariant & var)           { m_vDefaultValue = var; }
    00162    inline void setPrecision(int iPrecision)                    { m_iPrecision = iPrecision; }
    00163    inline void setRequired(bool bRequired)                     { m_bRequired = bRequired; }
    00164    inline void setReadOnly(bool bReadOnly)                     { m_bReadOnly = bReadOnly; }
    00165    inline void setAutoIncrement(bool bAutoIncrement)           { m_bAutoIncrement = bAutoIncrement; }
    00166    inline void setIsPrimaryKey(bool bIsPrimaryKey)             { m_bIsPrimaryKey = bIsPrimaryKey; }
    00167    inline void setIsIndex(bool bIsIndex)                       { m_bIndex = bIsIndex; }
    00168    inline void setIsUnique(bool bIsUnique)                     { m_bUnique = bIsUnique; }
    00169    inline void setParent(IxDataMemberX * pParent)              { m_pParent = pParent; }
    00170    inline void setSqlRelation(IxSqlRelation * pSqlRelation)    { m_pSqlRelation.reset(pSqlRelation); }
    00171
    00172    void setMinValue(long lMinValue, const QString & sMessage = QString());
    00173    void setMinValue(double dMinValue, const QString & sMessage = QString());
    00174    void setMaxValue(long lMaxValue, const QString & sMessage = QString());
    00175    void setMaxValue(double dMaxValue, const QString & sMessage = QString());
    00176    void setMinLength(int iMinLength, const QString & sMessage = QString());
    00177    void setMaxLength(int iMaxLength, const QString & sMessage = QString());
    00178    void setNotNull(bool bNotNull, const QString & sMessage = QString());
    00179
    00180    QString getName(int iIndex, const QString & sOtherName = QString()) const;
    00181    QString getSqlAlias(const QString & sTable = QString(), bool bClauseWhere = false, int iIndexName = 0) const;
    00182    QString getSqlType(int iIndexName = -1) const;
    00183    QString getSqlTypeAndParams(int iIndexName = -1) const;
    00184    QString getSqlPlaceHolder(const QString & sAppend = QString(), int iIndexName = 0, const QString & sSep = QString(", "), const QString & sOtherName = QString()) const;
    00185    void setSqlPlaceHolder(QSqlQuery & query, void * pOwner, const QString & sAppend = QString(), const QString & sOtherName = QString()) const;
    00186    QString getSqlAliasEqualToPlaceHolder(const QString & sTable = QString(), bool bClauseWhere = false, const QString & sAppend = QString(), const QString & sSep = QString(" AND ")) const;
    00187    QString getSqlNameEqualToPlaceHolder(const QString & sAppend = QString(), const QString & sSep = QString(" AND ")) const;
    00188    QString getSqlTablePointNameAsAlias(const QString & sTable, const QString & sSep = QString(", "), const QString & sSuffixAlias = QString()) const;
    00189    QString getSqlName(const QString & sSep = QString(", "), const QString & sOtherName = QString()) const;
    00190    QString getSqlNameAndTypeAndParams(const QString & sSep = QString(", "), const QString & sOtherName = QString()) const;
    00191
    00192    static QString getSqlFromTable(const QString & sTable);
    00193
    00194    virtual bool isEqual(const void * pOwner1, const void * pOwner2) const = 0;
    00195    virtual QString toString(const void * pOwner, const QString & sFormat, int iIndexName = -1) const = 0;
    00196    virtual qx_bool fromString(void * pOwner, const QString & s, const QString & sFormat, int iIndexName = -1) = 0;
    00197    virtual QVariant toVariant(const void * pOwner, const QString & sFormat, int iIndexName = -1) const = 0;
    00198    virtual qx_bool fromVariant(void * pOwner, const QVariant & v, const QString & sFormat, int iIndexName = -1) = 0;
    00199
    00200    inline QString toString(const void * pOwner, int iIndexName = -1) const             { return this->toString(pOwner, m_sFormat, iIndexName); }
    00201    inline qx_bool fromString(void * pOwner, const QString & s, int iIndexName = -1)    { return this->fromString(pOwner, s, m_sFormat, iIndexName); }
    00202    inline QVariant toVariant(const void * pOwner, int iIndexName = -1) const           { return this->toVariant(pOwner, m_sFormat, iIndexName); }
    00203    inline qx_bool fromVariant(void * pOwner, const QVariant & v, int iIndexName = -1)  { return this->fromVariant(pOwner, v, m_sFormat, iIndexName); }
    00204
    00205 protected:
    00206
    00207    virtual boost::any getDataPtr(const void * pOwner) const = 0;
    00208    virtual boost::any getDataPtr(void * pOwner) = 0;
    00209    virtual void * getDataVoidPtr(const void * pOwner) const = 0;
    00210    virtual void * getDataVoidPtr(void * pOwner) = 0;
    00211
    00212 public:
    00213
    00214    inline boost::any getValueAnyPtr(const void * pOwner) const    { return this->getDataPtr(pOwner); }
    00215    inline boost::any getValueAnyPtr(void * pOwner)                { return this->getDataPtr(pOwner); }
    00216    inline void * getValueVoidPtr(const void * pOwner) const       { return this->getDataVoidPtr(pOwner); }
    00217    inline void * getValueVoidPtr(void * pOwner)                   { return this->getDataVoidPtr(pOwner); }
    00218
    00219    template <typename T>
    00220    T * getValuePtr(void * pOwner, bool * bOk = NULL)
    00221    {
    00222       if (bOk) { (* bOk) = false; }
    00223       if (! m_bAccessDataPointer) { qDebug("[QxOrm] qx::IxDataMember::getValuePtr<T>() : '%s'", "cannot access data-member pointer"); return NULL; }
    00224       boost::any a = this->getDataPtr(pOwner);
    00225       try { T * t = boost::any_cast<T *>(a); if (bOk) { (* bOk) = (t != NULL); }; return t; }
    00226       catch (const boost::bad_any_cast & err) { Q_UNUSED(err); qDebug("[QxOrm] qx::IxDataMember::getValuePtr<T>() : '%s'", "bad any cast exception"); return NULL; }
    00227    }
    00228
    00229    template <typename T>
    00230    T getValue(void * pOwner, bool * bOk = NULL)
    00231    {
    00232       if (! m_bAccessDataPointer) { return qxCannotAccessDataPointer<T, 0>::getValue(this, pOwner, bOk); }
    00233       T * t = this->getValuePtr<T>(pOwner, bOk);
    00234       return (t ? (* t) : T());
    00235    }
    00236
    00237    template <typename T>
    00238    bool setValue(void * pOwner, const T & val)
    00239    {
    00240       if (! m_bAccessDataPointer) { return qxCannotAccessDataPointer<T, 0>::setValue(this, pOwner, val); }
    00241       T * t = this->getValuePtr<T>(pOwner);
    00242       if (t) { (* t) = val; }
    00243       return (t != NULL);
    00244    }
    00245
    00246 private:
    00247
    00248    template <typename T, int dummy>
    00249    struct qxCannotAccessDataPointer
    00250    {
    00251       static inline T getValue(IxDataMember * pData, void * pOwner, bool * bOk)
    00252       { Q_UNUSED(pData); Q_UNUSED(pOwner); qDebug("[QxOrm] qx::IxDataMember::qxCannotAccessDataPointer<T>::getValue() : '%s'", "type T not supported"); if (bOk) { (* bOk) = false; }; return T(); }
    00253       static inline bool setValue(IxDataMember * pData, void * pOwner, const T & val)
    00254       { Q_UNUSED(pData); Q_UNUSED(pOwner); Q_UNUSED(val); qDebug("[QxOrm] qx::IxDataMember::qxCannotAccessDataPointer<T>::setValue() : '%s'", "type T not supported"); return false; }
    00255    };
    00256
    00257    template <int dummy>
    00258    struct qxCannotAccessDataPointer<QVariant, dummy>
    00259    {
    00260       static inline QVariant getValue(IxDataMember * pData, void * pOwner, bool * bOk)
    00261       { if (bOk) { (* bOk) = (pData != NULL); }; return (pData ? pData->toVariant(pOwner, "") : QVariant()); }
    00262       static inline bool setValue(IxDataMember * pData, void * pOwner, const QVariant & val)
    00263       { return (pData ? pData->fromVariant(pOwner, val, "").getValue() : false); }
    00264    };
    00265
    00266    template <int dummy>
    00267    struct qxCannotAccessDataPointer<QString, dummy>
    00268    {
    00269       static inline QString getValue(IxDataMember * pData, void * pOwner, bool * bOk)
    00270       { if (bOk) { (* bOk) = (pData != NULL); }; return (pData ? pData->toString(pOwner, "") : QString()); }
    00271       static inline bool setValue(IxDataMember * pData, void * pOwner, const QString & val)
    00272       { return (pData ? pData->fromString(pOwner, val, "").getValue() : false); }
    00273    };
    00274
    00275 public:
    00276
    00277 #if _QX_SERIALIZE_POLYMORPHIC
    00278    QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::polymorphic_iarchive, boost::archive::polymorphic_oarchive)
    00279 #endif // _QX_SERIALIZE_POLYMORPHIC
    00280
    00281 #if _QX_SERIALIZE_BINARY
    00282    QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::binary_iarchive, boost::archive::binary_oarchive)
    00283 #endif // _QX_SERIALIZE_BINARY
    00284
    00285 #if _QX_SERIALIZE_TEXT
    00286    QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::text_iarchive, boost::archive::text_oarchive)
    00287 #endif // _QX_SERIALIZE_TEXT
    00288
    00289 #if _QX_SERIALIZE_XML
    00290    QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::xml_iarchive, boost::archive::xml_oarchive)
    00291 #endif // _QX_SERIALIZE_XML
    00292
    00293 #if _QX_SERIALIZE_PORTABLE_BINARY
    00294    QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(eos::portable_iarchive, eos::portable_oarchive)
    00295 #endif // _QX_SERIALIZE_PORTABLE_BINARY
    00296
    00297 #if _QX_SERIALIZE_WIDE_BINARY
    00298    QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::binary_wiarchive, boost::archive::binary_woarchive)
    00299 #endif // _QX_SERIALIZE_WIDE_BINARY
    00300
    00301 #if _QX_SERIALIZE_WIDE_TEXT
    00302    QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::text_wiarchive, boost::archive::text_woarchive)
    00303 #endif // _QX_SERIALIZE_WIDE_TEXT
    00304
    00305 #if _QX_SERIALIZE_WIDE_XML
    00306    QX_IX_DATA_MEMBER_PURE_VIRTUAL_ARCHIVE(boost::archive::xml_wiarchive, boost::archive::xml_woarchive)
    00307 #endif // _QX_SERIALIZE_WIDE_XML
    00308
    00309 private:
    00310
    00311    inline void updateNamePtr()
    00312    {
    00313       QString sNamePtr = (m_sName.isEmpty() ? m_sKey : m_sName);
    00314       sNamePtr.replace("|", "-"); // valid xml tag
    00315       m_byteName = sNamePtr.toLatin1();
    00316       m_pName = m_byteName.constData();
    00317       m_lstNames = (m_sName.isEmpty() ? m_sKey.split("|") : m_sName.split("|"));
    00318    }
    00319
    00320    template <class Archive>
    00321    void serialize(Archive & ar, const unsigned int version);
    00322
    00323 };
    00324
    00325 typedef boost::shared_ptr<IxDataMember> IxDataMember_ptr;
    00326
    00327 } // namespace qx
    00328
    00329 QX_DLL_EXPORT_INLINE_FCT inline bool operator< (const qx::IxDataMember & i1, const qx::IxDataMember & i2);
    00330 QX_DLL_EXPORT_INLINE_FCT inline bool operator> (const qx::IxDataMember & i1, const qx::IxDataMember & i2);
    00331
    00332 #endif // _IX_DATA_MEMBER_H_