123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865 |
- #ifndef EASYQTSQL_QUERYRESULT_H
- #define EASYQTSQL_QUERYRESULT_H
- #ifndef EASY_QT_SQL_MAIN
- #include <QtSql>
- #include "EasyQtSql_NonQueryResult.h"
- #endif
- class QueryResult
- {
- friend class Database;
- friend class Transaction;
- friend class PreparedQuery;
- public:
-
- QSqlQuery &unwrappedQuery()
- {
- return m_query;
- }
-
- bool next()
- {
- bool res = m_query.next();
- if ( ! m_firstRowFetched)
- {
- m_fieldNames.clear();
- QSqlRecord record = m_query.record();
- for (int i = 0; i < record.count(); ++i)
- {
- m_fieldNames.append(record.fieldName(i));
- }
- m_firstRowFetched = true;
- }
- return res;
- }
-
- bool previous()
- {
- return m_query.previous();
- }
-
- bool nextResult()
- {
- m_firstRowFetched = false;
- bool res = m_query.nextResult();
- return res;
- }
-
- bool first()
- {
- return m_query.first();
- }
-
- bool last()
- {
- return m_query.last();
- }
-
- bool seek(int index, bool relative = false)
- {
- return m_query.seek(index, relative);
- }
-
- int at() const
- {
- return m_query.at();
- }
-
- bool isActive() const
- {
- return m_query.isActive();
- }
-
- bool isValid() const
- {
- return m_query.isValid();
- }
-
- bool isForwardOnly() const
- {
- return m_query.isForwardOnly();
- }
-
- int size() const
- {
- return m_query.size();
- }
-
- int numRowsAffected() const
- {
- return m_query.numRowsAffected();
- }
-
- QSqlError lastError() const
- {
- return m_query.lastError();
- }
-
- QString lastQuery() const
- {
- return m_query.lastQuery();
- }
-
- QString executedQuery() const
- {
- return m_query.executedQuery();
- }
-
- QVariant value(int column) const
- {
- return m_query.value(column);
- }
-
- QVariant value(const QString &colName) const
- {
- return m_query.value(colName);
- }
-
- QVariant boundValue(int column) const
- {
- return m_query.boundValue(column);
- }
-
- QVariant boundValue(const QString &aliasName) const
- {
- const int index = m_bindValueAlias.value(aliasName.trimmed().toLower(), -1);
- QVariant res;
- if (index >= 0)
- {
- res = boundValue(index);
- }
- return res;
- }
-
- QVariantList boundValues() const
- {
- QList<QVariant> list = m_query.boundValues().values();
- return list;
- }
-
- QVariantMap toMap() const
- {
- QVariantMap res;
- fetchMap(res);
- return res;
- }
-
- QVariantList toList() const
- {
- QVariantList res;
- fetchList(res);
- return res;
- }
-
- QStringList toStringList() const
- {
- QStringList res;
- fetchStringList(res);
- return res;
- }
-
- QVector<QVariant> toVector() const
- {
- QVector<QVariant> res;
- fetchVector(res);
- return res;
- }
-
- template <typename T>
- QVector<T> toVector(bool skipNullValues = false) const
- {
- QVector<T> res;
- res.reserve(m_fieldNames.count());
- for (int i = 0; i < m_fieldNames.count(); ++i)
- {
- const QVariant &value = m_query.value(i);
- if (!value.canConvert<T>())
- continue;
- if (skipNullValues && value.isNull())
- continue;
- res.append(value.value<T>());
- }
- return res;
- }
-
- QVector<int> parseToIntVector(int base = 10, bool skipNullValues = false) const
- {
- QVector<int> res;
- res.reserve(m_fieldNames.count());
- for (int i = 0; i < m_fieldNames.count(); ++i)
- {
- const QVariant &value = m_query.value(i);
- if (!value.canConvert<int>())
- continue;
- if (skipNullValues && value.isNull())
- continue;
- bool ok = false;
- int iValue = 0;
- if ( ((value.type() == QVariant::String) || (value.type() == QVariant::ByteArray)))
- {
- const QString str = value.toString();
- iValue = str.toInt(&ok, base);
- }
- else
- {
- iValue = value.toInt(&ok);
- }
- if (ok)
- {
- res.append(iValue);
- }
- }
- return res;
- }
-
- template <typename T>
- T scalar() const
- {
- return m_query.value(0).value<T>();
- }
-
- QVariant scalar() const
- {
- return m_query.value(0);
- }
-
- void fetchVars(int &value) const
- {
- value = m_query.value(m_fetchIndex).toInt();
- m_fetchIndex = 0;
- }
-
- void fetchVars(double &value) const
- {
- value = m_query.value(m_fetchIndex).toInt();
- m_fetchIndex = 0;
- }
-
- void fetchVars(QString &value) const
- {
- value = m_query.value(m_fetchIndex).toString();
- m_fetchIndex = 0;
- }
-
- void fetchVars(bool &value) const
- {
- value = m_query.value(m_fetchIndex).toBool();
- m_fetchIndex = 0;
- }
-
- void fetchVars(QDate &value) const
- {
- value = m_query.value(m_fetchIndex).toDate();
- m_fetchIndex = 0;
- }
-
- void fetchVars(QDateTime &value) const
- {
- value = m_query.value(m_fetchIndex).toDateTime();
- m_fetchIndex = 0;
- }
-
- void fetchVars(QTime &value) const
- {
- value = m_query.value(m_fetchIndex).toTime();
- m_fetchIndex = 0;
- }
-
- void fetchVars(QByteArray &value) const
- {
- value = m_query.value(m_fetchIndex).toByteArray();
- m_fetchIndex = 0;
- }
-
- void fetchVars(QVariant &value) const
- {
- value = m_query.value(m_fetchIndex);
- m_fetchIndex = 0;
- }
- template <typename... Rest> void fetchVars(int &value, Rest&... rest) const
- {
- value = m_query.value(m_fetchIndex++).toInt();
- fetchVars(rest...);
- }
- template <typename... Rest> void fetchVars(double &value, Rest&... rest) const
- {
- value = m_query.value(m_fetchIndex++).toDouble();
- fetchVars(rest...);
- }
- template <typename... Rest> void fetchVars(QString &value, Rest&... rest) const
- {
- value = m_query.value(m_fetchIndex++).toString();
- fetchVars(rest...);
- }
- template <typename... Rest> void fetchVars(bool &value, Rest&... rest) const
- {
- value = m_query.value(m_fetchIndex++).toBool();
- fetchVars(rest...);
- }
- template <typename... Rest> void fetchVars(QDate &value, Rest&... rest) const
- {
- value = m_query.value(m_fetchIndex++).toDate();
- fetchVars(rest...);
- }
- template <typename... Rest> void fetchVars(QDateTime &value, Rest&... rest) const
- {
- value = m_query.value(m_fetchIndex++).toDateTime();
- fetchVars(rest...);
- }
- template <typename... Rest> void fetchVars(QTime &value, Rest&... rest) const
- {
- value = m_query.value(m_fetchIndex++).toTime();
- fetchVars(rest...);
- }
- template <typename... Rest> void fetchVars(QByteArray &value, Rest&... rest) const
- {
- value = m_query.value(m_fetchIndex++).toByteArray();
- fetchVars(rest...);
- }
- template <typename... Rest> void fetchVars(QVariant &value, Rest&... rest) const
- {
- value = m_query.value(m_fetchIndex++);
- fetchVars(rest...);
- }
-
- void fetchObject(QObject &object) const
- {
- const QMetaObject *metaobject = object.metaObject();
- const int count = metaobject->propertyCount();
- const QVariantMap map = toMap();
- for (int i = 0; i < count; ++i)
- {
- QMetaProperty metaproperty = metaobject->property(i);
- if (metaproperty.isWritable())
- {
- QLatin1String sName(metaproperty.name());
- if (map.contains(sName))
- {
- object.setProperty(sName.data(), map.value(sName));
- }
- }
- }
- }
-
- template<typename T>
- void fetchGadget(T &gadget) const
- {
- const QMetaObject &metaobject = gadget.staticMetaObject;
- const int count = metaobject.propertyCount();
- const QVariantMap map = toMap();
- for (int i = 0; i < count; ++i)
- {
- QMetaProperty metaproperty = metaobject.property(i);
- if (metaproperty.isWritable())
- {
- QLatin1String sName(metaproperty.name());
- if (map.contains(sName))
- {
- metaproperty.writeOnGadget(&gadget, map.value(sName));
- }
- }
- }
- }
-
- void fetchMap(QVariantMap &map) const
- {
- map.clear();
- for (int i = 0; i < m_fieldNames.count(); ++i)
- {
- map.insert(m_fieldNames.at(i), m_query.value(i));
- }
- }
-
- void fetchList(QVariantList &list) const
- {
- list.clear();
- for (int i = 0; i < m_fieldNames.count(); ++i)
- {
- list.append(m_query.value(i));
- }
- }
-
- void fetchVector(QVector<QVariant> &vector) const
- {
- vector.clear();
- vector.reserve(m_fieldNames.count());
- for (int i = 0; i < m_fieldNames.count(); ++i)
- {
- vector.append(m_query.value(i));
- }
- }
-
- void fetchStringList(QStringList &list) const
- {
- list.clear();
- for (int i = 0; i < m_fieldNames.count(); ++i)
- {
- list.append(m_query.value(i).toString());
- }
- }
- private:
- QueryResult()
- { }
- explicit QueryResult(const QSqlQuery &query)
- : m_query(query)
- { }
- QueryResult(const QSqlQuery &query, const QMap<QString, int> &bindValueAliasMap)
- : m_query(query)
- , m_bindValueAlias(bindValueAliasMap)
- { }
- private:
- QSqlQuery m_query;
- QStringList m_fieldNames;
- QMap<QString, int> m_bindValueAlias;
- mutable int m_fetchIndex = 0;
- bool m_firstRowFetched = false;
- };
- #endif
|