| QxOrm 1.2.4 C++ Object Relational Mapping library

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

- File List
- File Members

QxSqlQuery.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 _QX_SQL_QUERY_H_
    00027 #define _QX_SQL_QUERY_H_
    00028
    00029 #ifdef _MSC_VER
    00030 #pragma once
    00031 #endif
    00032
    00040 #include <boost/tuple/tuple.hpp>
    00041 #include <boost/tuple/tuple_comparison.hpp>
    00042 #include <boost/tuple/tuple_io.hpp>
    00043
    00044 #include <QtSql/qsqlquery.h>
    00045
    00046 #include <QxCollection/QxCollection.h>
    00047
    00048 #include <QxDao/QxSqlElement/QxSqlElement.h>
    00049
    00050 namespace qx {
    00051
    00178 class QX_DLL_EXPORT QxSqlQuery
    00179 {
    00180
    00181 protected:
    00182
    00183    struct QxSqlResult
    00184    { QHash<QString, int> positionByKey; QVector< QVector<QVariant> > values; };
    00185
    00186    typedef boost::tuple<QVariant, QSql::ParamType> type_bind_value;
    00187
    00188    QString                                   m_sQuery;
    00189    QxCollection<QString, type_bind_value>    m_lstValue;
    00190    qx::dao::detail::IxSqlElement_ptr         m_pSqlElementTemp;
    00191    QList<qx::dao::detail::IxSqlElement_ptr>  m_lstSqlElement;
    00192    int                                       m_iSqlElementIndex;
    00193    int                                       m_iParenthesisCount;
    00194    bool                                      m_bDistinct;
    00195    boost::shared_ptr<QxSqlResult>            m_pSqlResult;
    00196
    00197 public:
    00198
    00199    QxSqlQuery() : m_iSqlElementIndex(0), m_iParenthesisCount(0), m_bDistinct(false) { ; }
    00200    QxSqlQuery(const QString & sQuery) : m_sQuery(sQuery), m_iSqlElementIndex(0), m_iParenthesisCount(0), m_bDistinct(false) { ; }
    00201    QxSqlQuery(const char * sQuery) : m_sQuery(sQuery), m_iSqlElementIndex(0), m_iParenthesisCount(0), m_bDistinct(false) { ; }
    00202    virtual ~QxSqlQuery() { ; }
    00203
    00204    QString query();
    00205    bool isEmpty() const;
    00206    bool isDistinct() const;
    00207    void clear();
    00208    void resolve(QSqlQuery & query) const;
    00209    void resolveOutput(QSqlQuery & query, bool bFetchSqlResult);
    00210    void postProcess(QString & sql) const;
    00211
    00212    QxSqlQuery & query(const QString & sQuery);
    00213    QxSqlQuery & bind(const QVariant & vValue, QSql::ParamType paramType = QSql::In);
    00214    QxSqlQuery & bind(const QString & sKey, const QVariant & vValue, QSql::ParamType paramType = QSql::In);
    00215
    00216    QVariant boundValue(const QString & sKey) const;
    00217    QVariant boundValue(int iPosition) const;
    00218
    00219    long getSqlResultRowCount() const;
    00220    long getSqlResultColumnCount() const;
    00221    QVariant getSqlResultAt(long row, long column) const;
    00222    QVariant getSqlResultAt(long row, const QString & column) const;
    00223    QVector<QVariant> getSqlResultAt(long row) const;
    00224    QVector<QString> getSqlResultAllColumns() const;
    00225    void dumpSqlResult();
    00226
    00227 private:
    00228
    00229 #ifndef NDEBUG
    00230    void verifyQuery() const;
    00231 #else
    00232    inline void verifyQuery() const { ; }
    00233 #endif // NDEBUG
    00234
    00235    void fetchSqlResult(QSqlQuery & query);
    00236
    00237 public:
    00238
    00239    /* -- All methods to build SQL query using C++ syntax -- */
    00240
    00241    QxSqlQuery & distinct();
    00242
    00243    QxSqlQuery & where(const QString & column);
    00244    QxSqlQuery & where_OpenParenthesis(const QString & column);
    00245    QxSqlQuery & and_(const QString & column);
    00246    QxSqlQuery & and_OpenParenthesis(const QString & column);
    00247    QxSqlQuery & or_(const QString & column);
    00248    QxSqlQuery & or_OpenParenthesis(const QString & column);
    00249
    00250    QxSqlQuery & openParenthesis();
    00251    QxSqlQuery & closeParenthesis();
    00252
    00253    QxSqlQuery & orderAsc(const QStringList & columns);
    00254    QxSqlQuery & orderAsc(const QString & col1);
    00255    QxSqlQuery & orderAsc(const QString & col1, const QString & col2);
    00256    QxSqlQuery & orderAsc(const QString & col1, const QString & col2, const QString & col3);
    00257    QxSqlQuery & orderAsc(const QString & col1, const QString & col2, const QString & col3, const QString & col4);
    00258    QxSqlQuery & orderAsc(const QString & col1, const QString & col2, const QString & col3, const QString & col4, const QString & col5);
    00259    QxSqlQuery & orderAsc(const QString & col1, const QString & col2, const QString & col3, const QString & col4, const QString & col5, const QString & col6);
    00260    QxSqlQuery & orderAsc(const QString & col1, const QString & col2, const QString & col3, const QString & col4, const QString & col5, const QString & col6, const QString & col7);
    00261    QxSqlQuery & orderAsc(const QString & col1, const QString & col2, const QString & col3, const QString & col4, const QString & col5, const QString & col6, const QString & col7, const QString & col8);
    00262    QxSqlQuery & orderAsc(const QString & col1, const QString & col2, const QString & col3, const QString & col4, const QString & col5, const QString & col6, const QString & col7, const QString & col8, const QString & col9);
    00263
    00264    QxSqlQuery & orderDesc(const QStringList & columns);
    00265    QxSqlQuery & orderDesc(const QString & col1);
    00266    QxSqlQuery & orderDesc(const QString & col1, const QString & col2);
    00267    QxSqlQuery & orderDesc(const QString & col1, const QString & col2, const QString & col3);
    00268    QxSqlQuery & orderDesc(const QString & col1, const QString & col2, const QString & col3, const QString & col4);
    00269    QxSqlQuery & orderDesc(const QString & col1, const QString & col2, const QString & col3, const QString & col4, const QString & col5);
    00270    QxSqlQuery & orderDesc(const QString & col1, const QString & col2, const QString & col3, const QString & col4, const QString & col5, const QString & col6);
    00271    QxSqlQuery & orderDesc(const QString & col1, const QString & col2, const QString & col3, const QString & col4, const QString & col5, const QString & col6, const QString & col7);
    00272    QxSqlQuery & orderDesc(const QString & col1, const QString & col2, const QString & col3, const QString & col4, const QString & col5, const QString & col6, const QString & col7, const QString & col8);
    00273    QxSqlQuery & orderDesc(const QString & col1, const QString & col2, const QString & col3, const QString & col4, const QString & col5, const QString & col6, const QString & col7, const QString & col8, const QString & col9);
    00274
    00275    QxSqlQuery & groupBy(const QStringList & columns);
    00276    QxSqlQuery & groupBy(const QString & col1);
    00277    QxSqlQuery & groupBy(const QString & col1, const QString & col2);
    00278    QxSqlQuery & groupBy(const QString & col1, const QString & col2, const QString & col3);
    00279    QxSqlQuery & groupBy(const QString & col1, const QString & col2, const QString & col3, const QString & col4);
    00280    QxSqlQuery & groupBy(const QString & col1, const QString & col2, const QString & col3, const QString & col4, const QString & col5);
    00281    QxSqlQuery & groupBy(const QString & col1, const QString & col2, const QString & col3, const QString & col4, const QString & col5, const QString & col6);
    00282    QxSqlQuery & groupBy(const QString & col1, const QString & col2, const QString & col3, const QString & col4, const QString & col5, const QString & col6, const QString & col7);
    00283    QxSqlQuery & groupBy(const QString & col1, const QString & col2, const QString & col3, const QString & col4, const QString & col5, const QString & col6, const QString & col7, const QString & col8);
    00284    QxSqlQuery & groupBy(const QString & col1, const QString & col2, const QString & col3, const QString & col4, const QString & col5, const QString & col6, const QString & col7, const QString & col8, const QString & col9);
    00285
    00286    QxSqlQuery & limit(int rowsCount, int startRow = 0);
    00287
    00288    QxSqlQuery & like(const QString & val);
    00289    QxSqlQuery & notLike(const QString & val);
    00290    QxSqlQuery & startsWith(const QString & val);
    00291    QxSqlQuery & endsWith(const QString & val);
    00292    QxSqlQuery & containsString(const QString & val);
    00293
    00294    QxSqlQuery & isEqualTo(const QVariant & val);
    00295    QxSqlQuery & isNotEqualTo(const QVariant & val);
    00296    QxSqlQuery & isGreaterThan(const QVariant & val);
    00297    QxSqlQuery & isGreaterThanOrEqualTo(const QVariant & val);
    00298    QxSqlQuery & isLessThan(const QVariant & val);
    00299    QxSqlQuery & isLessThanOrEqualTo(const QVariant & val);
    00300
    00301    QxSqlQuery & in(const QVariantList & values);
    00302    QxSqlQuery & in(const QVariant & val1);
    00303    QxSqlQuery & in(const QVariant & val1, const QVariant & val2);
    00304    QxSqlQuery & in(const QVariant & val1, const QVariant & val2, const QVariant & val3);
    00305    QxSqlQuery & in(const QVariant & val1, const QVariant & val2, const QVariant & val3, const QVariant & val4);
    00306    QxSqlQuery & in(const QVariant & val1, const QVariant & val2, const QVariant & val3, const QVariant & val4, const QVariant & val5);
    00307    QxSqlQuery & in(const QVariant & val1, const QVariant & val2, const QVariant & val3, const QVariant & val4, const QVariant & val5, const QVariant & val6);
    00308    QxSqlQuery & in(const QVariant & val1, const QVariant & val2, const QVariant & val3, const QVariant & val4, const QVariant & val5, const QVariant & val6, const QVariant & val7);
    00309    QxSqlQuery & in(const QVariant & val1, const QVariant & val2, const QVariant & val3, const QVariant & val4, const QVariant & val5, const QVariant & val6, const QVariant & val7, const QVariant & val8);
    00310    QxSqlQuery & in(const QVariant & val1, const QVariant & val2, const QVariant & val3, const QVariant & val4, const QVariant & val5, const QVariant & val6, const QVariant & val7, const QVariant & val8, const QVariant & val9);
    00311
    00312    QxSqlQuery & notIn(const QVariantList & values);
    00313    QxSqlQuery & notIn(const QVariant & val1);
    00314    QxSqlQuery & notIn(const QVariant & val1, const QVariant & val2);
    00315    QxSqlQuery & notIn(const QVariant & val1, const QVariant & val2, const QVariant & val3);
    00316    QxSqlQuery & notIn(const QVariant & val1, const QVariant & val2, const QVariant & val3, const QVariant & val4);
    00317    QxSqlQuery & notIn(const QVariant & val1, const QVariant & val2, const QVariant & val3, const QVariant & val4, const QVariant & val5);
    00318    QxSqlQuery & notIn(const QVariant & val1, const QVariant & val2, const QVariant & val3, const QVariant & val4, const QVariant & val5, const QVariant & val6);
    00319    QxSqlQuery & notIn(const QVariant & val1, const QVariant & val2, const QVariant & val3, const QVariant & val4, const QVariant & val5, const QVariant & val6, const QVariant & val7);
    00320    QxSqlQuery & notIn(const QVariant & val1, const QVariant & val2, const QVariant & val3, const QVariant & val4, const QVariant & val5, const QVariant & val6, const QVariant & val7, const QVariant & val8);
    00321    QxSqlQuery & notIn(const QVariant & val1, const QVariant & val2, const QVariant & val3, const QVariant & val4, const QVariant & val5, const QVariant & val6, const QVariant & val7, const QVariant & val8, const QVariant & val9);
    00322
    00323    QxSqlQuery & in_Select(const QString & sql);
    00324    QxSqlQuery & notIn_Select(const QString & sql);
    00325
    00326    QxSqlQuery & isNull();
    00327    QxSqlQuery & isNotNull();
    00328
    00329    QxSqlQuery & isBetween(const QVariant & val1, const QVariant & val2);
    00330    QxSqlQuery & isNotBetween(const QVariant & val1, const QVariant & val2);
    00331
    00332    QxSqlQuery & freeText(const QString & text);
    00333
    00334 private:
    00335
    00336    QxSqlQuery & addSqlExpression(const QString & column, qx::dao::detail::QxSqlExpression::type type);
    00337    QxSqlQuery & addSqlCompare(const QVariant & val, qx::dao::detail::QxSqlCompare::type type);
    00338    QxSqlQuery & addSqlSort(const QStringList & columns, qx::dao::detail::QxSqlSort::type type);
    00339    QxSqlQuery & addSqlIn(const QVariantList & values, qx::dao::detail::QxSqlIn::type type);
    00340    QxSqlQuery & addSqlIsNull(qx::dao::detail::QxSqlIsNull::type type);
    00341    QxSqlQuery & addSqlIsBetween(const QVariant & val1, const QVariant & val2, qx::dao::detail::QxSqlIsBetween::type type);
    00342
    00343 };
    00344
    00345 } // namespace qx
    00346
    00347 typedef qx::QxSqlQuery qx_query;
    00348
    00349 namespace qx {
    00350 namespace dao {
    00351
    00375 QX_DLL_EXPORT QSqlError call_query(qx::QxSqlQuery & query, QSqlDatabase * pDatabase = NULL);
    00376
    00377 } // namespace dao
    00378 } // namespace qx
    00379
    00380 #endif // _QX_SQL_QUERY_H_