![]() |
QxOrm 1.2.1
C++ Object Relational Mapping library
|
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 <QxCollection/QxCollection.h> 00057 00058 #include <QxDataMember/IxDataMemberX.h> 00059 00060 namespace qx { 00061 namespace dao { 00062 namespace detail { 00063 00068 class QX_DLL_EXPORT IxDao_Helper 00069 { 00070 00071 protected: 00072 00073 QTime m_time; 00074 QSqlDatabase m_database; 00075 QSqlQuery m_query; 00076 QSqlError m_error; 00077 QString m_context; 00078 long m_lDataCount; 00079 bool m_bTransaction; 00080 bool m_bQuiet; 00081 bool m_bTraceQuery; 00082 bool m_bTraceRecord; 00083 bool m_bCartesianProduct; 00084 QStringList m_lstColumns; 00085 00086 qx::IxSqlQueryBuilder_ptr m_pQueryBuilder; 00087 qx::IxDataMemberX * m_pDataMemberX; 00088 qx::IxDataMember * m_pDataId; 00089 qx::QxSqlQuery m_qxQuery; 00090 00091 typedef qx::QxCollection<QString, qx::IxSqlRelation *> type_lst_relation; 00092 typedef boost::scoped_ptr< type_lst_relation > type_lst_relation_ptr; 00093 type_lst_relation_ptr m_pSqlRelationX; 00094 00095 protected: 00096 00097 IxDao_Helper(); 00098 virtual ~IxDao_Helper(); 00099 00100 public: 00101 00102 bool isValid() const; 00103 bool hasFeature(QSqlDriver::DriverFeature ft) const; 00104 00105 QSqlDatabase & database(); 00106 const QSqlDatabase & database() const; 00107 QSqlQuery & query(); 00108 const QSqlQuery & query() const; 00109 QSqlError & error(); 00110 const QSqlError & error() const; 00111 qx::QxSqlQuery & qxQuery(); 00112 const qx::QxSqlQuery & qxQuery() const; 00113 qx::IxSqlQueryBuilder & builder(); 00114 const qx::IxSqlQueryBuilder & builder() const; 00115 qx::IxDataMemberX * getDataMemberX() const; 00116 long getDataCount() const; 00117 qx::IxDataMember * getDataId() const; 00118 qx::IxDataMember * nextData(long & l) const; 00119 QString sql() const; 00120 type_lst_relation * getSqlRelationX() const; 00121 bool getCartesianProduct() const; 00122 QStringList getSqlColumns() const; 00123 void setSqlColumns(const QStringList & lst); 00124 00125 QSqlError errFailed(); 00126 QSqlError errEmpty(); 00127 QSqlError errNoData(); 00128 QSqlError errInvalidId(); 00129 QSqlError errInvalidRelation(); 00130 00131 bool transaction(); 00132 bool nextRecord(); 00133 void updateError(const QSqlError & error); 00134 void quiet(); 00135 bool exec(); 00136 00137 bool updateSqlRelationX(const QStringList & relation); 00138 void dumpRecord() const; 00139 void addQuery(const qx::QxSqlQuery & query, bool bResolve); 00140 00141 template <class U> 00142 inline bool isValidPrimaryKey(const U & u) 00143 { return (m_pDataId && qx::trait::is_valid_primary_key(m_pDataId->toVariant(& u))); } 00144 00145 template <class U> 00146 inline void updateLastInsertId(U & u) 00147 { 00148 if (m_pDataId && m_pDataId->getAutoIncrement() && this->hasFeature(QSqlDriver::LastInsertId)) 00149 { m_pDataId->fromVariant((& u), m_query.lastInsertId()); } 00150 } 00151 00152 protected: 00153 00154 virtual void updateQueryBuilder(); 00155 00156 QSqlError updateError(const QString & sError); 00157 void init(QSqlDatabase * pDatabase, const QString & sContext); 00158 void terminate(); 00159 00160 }; 00161 00162 } // namespace detail 00163 } // namespace dao 00164 } // namespace qx 00165 00166 #endif // _IX_DAO_HELPER_H_