QxOrm  1.2.4
C++ Object Relational Mapping library
IxDao_Helper.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** http://www.qxorm.com/
00004 ** http://sourceforge.net/projects/qxorm/
00005 ** Original file by Lionel Marty
00006 **
00007 ** This file is part of the QxOrm library
00008 **
00009 ** This software is provided 'as-is', without any express or implied
00010 ** warranty. In no event will the authors be held liable for any
00011 ** damages arising from the use of this software.
00012 **
00013 ** GNU Lesser General Public License Usage
00014 ** This file must be used under the terms of the GNU Lesser
00015 ** General Public License version 2.1 as published by the Free Software
00016 ** Foundation and appearing in the file 'license.lgpl.txt' included in the
00017 ** packaging of this file.  Please review the following information to
00018 ** ensure the GNU Lesser General Public License version 2.1 requirements
00019 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00020 **
00021 ** If you have questions regarding the use of this file, please contact :
00022 ** contact@qxorm.com
00023 **
00024 ****************************************************************************/
00025 
00026 #ifndef _IX_DAO_HELPER_H_
00027 #define _IX_DAO_HELPER_H_
00028 
00029 #ifdef _MSC_VER
00030 #pragma once
00031 #endif
00032 
00040 #include <QtSql/qsqldatabase.h>
00041 #include <QtSql/qsqlquery.h>
00042 #include <QtSql/qsqlerror.h>
00043 #include <QtSql/qsqldriver.h>
00044 
00045 #include <QxTraits/get_primary_key.h>
00046 #include <QxTraits/is_valid_primary_key.h>
00047 
00048 #include <QxDao/QxSqlDatabase.h>
00049 #include <QxDao/QxSqlQueryBuilder.h>
00050 #include <QxDao/QxSqlQueryHelper.h>
00051 #include <QxDao/QxSqlQuery.h>
00052 #include <QxDao/IxSqlRelation.h>
00053 #include <QxDao/QxSqlRelationLinked.h>
00054 
00055 #include <QxDao/QxSqlGenerator/IxSqlGenerator.h>
00056 
00057 #include <QxCollection/QxCollection.h>
00058 
00059 #include <QxDataMember/IxDataMemberX.h>
00060 
00061 #include <QxValidator/QxInvalidValueX.h>
00062 #include <QxValidator/QxValidatorError.h>
00063 
00064 namespace qx {
00065 template <class T>
00066 QxInvalidValueX validate(T & t, const QString & group);
00067 } // namespace qx
00068 
00069 namespace qx {
00070 namespace dao {
00071 namespace detail {
00072 
00077 class QX_DLL_EXPORT IxDao_Helper
00078 {
00079 
00080 protected:
00081 
00082    QTime          m_time;                 
00083    QSqlDatabase   m_database;             
00084    QSqlQuery      m_query;                
00085    QSqlError      m_error;                
00086    QString        m_context;              
00087    long           m_lDataCount;           
00088    bool           m_bTransaction;         
00089    bool           m_bQuiet;               
00090    bool           m_bTraceQuery;          
00091    bool           m_bTraceRecord;         
00092    bool           m_bCartesianProduct;    
00093    bool           m_bValidatorThrowable;  
00094    QStringList    m_lstColumns;           
00095 
00096    qx::IxSqlQueryBuilder_ptr     m_pQueryBuilder;        
00097    qx::IxDataMemberX *           m_pDataMemberX;         
00098    qx::IxDataMember *            m_pDataId;              
00099    qx::QxSqlQuery                m_qxQuery;              
00100    IxSqlGenerator *              m_pSqlGenerator;        
00101    qx::QxInvalidValueX           m_lstInvalidValues;     
00102    qx::QxSqlRelationLinked_ptr   m_pSqlRelationLinked;   
00103 
00104 protected:
00105 
00106    IxDao_Helper();
00107    virtual ~IxDao_Helper();
00108 
00109 public:
00110 
00111    bool isValid() const;
00112    bool hasFeature(QSqlDriver::DriverFeature ft) const;
00113 
00114    QSqlDatabase & database();
00115    const QSqlDatabase & database() const;
00116    QSqlQuery & query();
00117    const QSqlQuery & query() const;
00118    QSqlError & error();
00119    const QSqlError & error() const;
00120    qx::QxSqlQuery & qxQuery();
00121    const qx::QxSqlQuery & qxQuery() const;
00122    qx::IxSqlQueryBuilder & builder();
00123    const qx::IxSqlQueryBuilder & builder() const;
00124    qx::IxDataMemberX * getDataMemberX() const;
00125    long getDataCount() const;
00126    qx::IxDataMember * getDataId() const;
00127    qx::IxDataMember * nextData(long & l) const;
00128    QString sql() const;
00129    qx::QxSqlRelationLinked * getSqlRelationLinked() const;
00130    bool getCartesianProduct() const;
00131    QStringList getSqlColumns() const;
00132    void setSqlColumns(const QStringList & lst);
00133    IxSqlGenerator * getSqlGenerator() const;
00134    void addInvalidValues(const qx::QxInvalidValueX & lst);
00135    bool isReadOnly() const;
00136 
00137    QSqlError errFailed();
00138    QSqlError errEmpty();
00139    QSqlError errNoData();
00140    QSqlError errInvalidId();
00141    QSqlError errInvalidRelation();
00142    QSqlError errReadOnly();
00143 
00144    bool transaction();
00145    bool nextRecord();
00146    void updateError(const QSqlError & error);
00147    void quiet();
00148    bool exec();
00149 
00150    bool updateSqlRelationX(const QStringList & relation);
00151    void dumpRecord() const;
00152    void addQuery(const qx::QxSqlQuery & query, bool bResolve);
00153 
00154    template <class U>
00155    inline bool isValidPrimaryKey(const U & u)
00156    { return (m_pDataId && qx::trait::is_valid_primary_key(m_pDataId->toVariant(& u))); }
00157 
00158    template <class U>
00159    inline void updateLastInsertId(U & u)
00160    {
00161       if (m_pDataId && m_pDataId->getAutoIncrement() && this->hasFeature(QSqlDriver::LastInsertId))
00162       { m_pDataId->fromVariant((& u), m_query.lastInsertId()); }
00163    }
00164 
00165    template <class U>
00166    inline bool validateInstance(U & u)
00167    {
00168       qx::QxInvalidValueX invalidValues = qx::validate(u, "");
00169       this->addInvalidValues(invalidValues);
00170       return (invalidValues.count() <= 0);
00171    }
00172 
00173 protected:
00174 
00175    virtual void updateQueryBuilder();
00176 
00177    QSqlError updateError(const QString & sError);
00178    void init(QSqlDatabase * pDatabase, const QString & sContext);
00179    void terminate();
00180 
00181 };
00182 
00183 } // namespace detail
00184 } // namespace dao
00185 } // namespace qx
00186 
00187 #endif // _IX_DAO_HELPER_H_