QxOrm  1.2.6
C++ Object Relational Mapping library
IxDao_Helper.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_DAO_HELPER_H_
00033 #define _IX_DAO_HELPER_H_
00034 
00035 #ifdef _MSC_VER
00036 #pragma once
00037 #endif
00038 
00046 #include <QtSql/qsqldatabase.h>
00047 #include <QtSql/qsqlquery.h>
00048 #include <QtSql/qsqlerror.h>
00049 #include <QtSql/qsqldriver.h>
00050 #include <QtSql/qsqlrecord.h>
00051 
00052 #include <QxTraits/get_primary_key.h>
00053 #include <QxTraits/is_valid_primary_key.h>
00054 
00055 #include <QxDao/QxSqlDatabase.h>
00056 #include <QxDao/IxSqlQueryBuilder.h>
00057 #include <QxDao/QxSqlQuery.h>
00058 #include <QxDao/IxSqlRelation.h>
00059 #include <QxDao/QxSqlRelationLinked.h>
00060 
00061 #include <QxDao/QxSqlGenerator/IxSqlGenerator.h>
00062 
00063 #include <QxCollection/QxCollection.h>
00064 
00065 #include <QxDataMember/IxDataMemberX.h>
00066 
00067 #include <QxValidator/QxInvalidValueX.h>
00068 #include <QxValidator/QxValidatorError.h>
00069 
00070 namespace qx {
00071 template <class T>
00072 QxInvalidValueX validate(T & t, const QString & group);
00073 } // namespace qx
00074 
00075 namespace qx {
00076 namespace dao {
00077 namespace detail {
00078 
00083 class QX_DLL_EXPORT IxDao_Helper
00084 {
00085 
00086 protected:
00087 
00088    QTime          m_time;                 
00089    QSqlDatabase   m_database;             
00090    QSqlQuery      m_query;                
00091    QSqlError      m_error;                
00092    QString        m_context;              
00093    long           m_lDataCount;           
00094    bool           m_bTransaction;         
00095    bool           m_bQuiet;               
00096    bool           m_bTraceQuery;          
00097    bool           m_bTraceRecord;         
00098    bool           m_bCartesianProduct;    
00099    bool           m_bValidatorThrowable;  
00100    QStringList    m_lstColumns;           
00101 
00102    qx::IxSqlQueryBuilder_ptr     m_pQueryBuilder;        
00103    qx::IxDataMemberX *           m_pDataMemberX;         
00104    qx::IxDataMember *            m_pDataId;              
00105    qx::QxSqlQuery                m_qxQuery;              
00106    IxSqlGenerator *              m_pSqlGenerator;        
00107    qx::QxInvalidValueX           m_lstInvalidValues;     
00108    qx::QxSqlRelationLinked_ptr   m_pSqlRelationLinked;   
00109 
00110 protected:
00111 
00112    IxDao_Helper();
00113    virtual ~IxDao_Helper();
00114 
00115 public:
00116 
00117    bool isValid() const;
00118    bool hasFeature(QSqlDriver::DriverFeature ft) const;
00119 
00120    QSqlDatabase & database();
00121    const QSqlDatabase & database() const;
00122    QSqlQuery & query();
00123    const QSqlQuery & query() const;
00124    QSqlError & error();
00125    const QSqlError & error() const;
00126    qx::QxSqlQuery & qxQuery();
00127    const qx::QxSqlQuery & qxQuery() const;
00128    qx::IxSqlQueryBuilder & builder();
00129    const qx::IxSqlQueryBuilder & builder() const;
00130    qx::IxDataMemberX * getDataMemberX() const;
00131    long getDataCount() const;
00132    qx::IxDataMember * getDataId() const;
00133    qx::IxDataMember * nextData(long & l) const;
00134    QString sql() const;
00135    qx::QxSqlRelationLinked * getSqlRelationLinked() const;
00136    bool getCartesianProduct() const;
00137    QStringList getSqlColumns() const;
00138    void setSqlColumns(const QStringList & lst);
00139    IxSqlGenerator * getSqlGenerator() const;
00140    void addInvalidValues(const qx::QxInvalidValueX & lst);
00141    bool getAddAutoIncrementIdToUpdateQuery() const;
00142    bool isReadOnly() const;
00143 
00144    QSqlError errFailed();
00145    QSqlError errEmpty();
00146    QSqlError errNoData();
00147    QSqlError errInvalidId();
00148    QSqlError errInvalidRelation();
00149    QSqlError errReadOnly();
00150 
00151    bool transaction();
00152    bool nextRecord();
00153    void updateError(const QSqlError & error);
00154    void quiet();
00155    bool exec();
00156 
00157    bool updateSqlRelationX(const QStringList & relation);
00158    void dumpRecord() const;
00159    void addQuery(const qx::QxSqlQuery & query, bool bResolve);
00160 
00161    template <class U>
00162    inline bool isValidPrimaryKey(const U & u)
00163    { return (m_pDataId && qx::trait::is_valid_primary_key(m_pDataId->toVariant(& u))); }
00164 
00165    template <class U>
00166    inline void updateLastInsertId(U & u)
00167    {
00168       if (m_pDataId && m_pDataId->getAutoIncrement() && this->hasFeature(QSqlDriver::LastInsertId))
00169       { m_pDataId->fromVariant((& u), m_query.lastInsertId()); }
00170    }
00171 
00172    template <class U>
00173    inline bool validateInstance(U & u)
00174    {
00175       qx::QxInvalidValueX invalidValues = qx::validate(u, "");
00176       this->addInvalidValues(invalidValues);
00177       return (invalidValues.count() <= 0);
00178    }
00179 
00180 protected:
00181 
00182    virtual void updateQueryBuilder();
00183 
00184    QSqlError updateError(const QString & sError);
00185    void init(QSqlDatabase * pDatabase, const QString & sContext);
00186    void terminate();
00187 
00188 };
00189 
00190 } // namespace detail
00191 } // namespace dao
00192 } // namespace qx
00193 
00194 #endif // _IX_DAO_HELPER_H_