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