1 #ifndef EASYQTSQL_QUERYRESULT_H 2 #define EASYQTSQL_QUERYRESULT_H 29 #ifndef EASY_QT_SQL_MAIN 62 bool res = m_query.next();
64 if ( ! m_firstRowFetched)
68 QSqlRecord record = m_query.record();
70 for (
int i = 0; i < record.count(); ++i)
72 m_fieldNames.append(record.fieldName(i));
75 m_firstRowFetched =
true;
88 return m_query.previous();
98 m_firstRowFetched =
false;
100 bool res = m_query.nextResult();
112 return m_query.first();
122 return m_query.last();
130 bool seek(
int index,
bool relative =
false)
132 return m_query.seek(index, relative);
153 return m_query.isActive();
163 return m_query.isValid();
173 return m_query.isForwardOnly();
183 return m_query.size();
193 return m_query.numRowsAffected();
203 return m_query.lastError();
213 return m_query.lastQuery();
223 return m_query.executedQuery();
233 return m_query.value(column);
241 QVariant
value(
const QString &colName)
const 243 return m_query.value(colName);
253 return m_query.boundValue(column);
267 const int index = m_bindValueAlias.value(aliasName.trimmed().toLower(), -1);
286 QList<QVariant> list = m_query.boundValues().values();
336 QVector<QVariant> res;
349 template <
typename T>
350 QVector<T>
toVector(
bool skipNullValues =
false)
const 354 res.reserve(m_fieldNames.count());
356 for (
int i = 0; i < m_fieldNames.count(); ++i)
358 const QVariant &
value = m_query.value(i);
360 if (!
value.canConvert<T>())
363 if (skipNullValues &&
value.isNull())
366 res.append(
value.value<T>());
385 res.reserve(m_fieldNames.count());
387 for (
int i = 0; i < m_fieldNames.count(); ++i)
389 const QVariant &
value = m_query.value(i);
391 if (!
value.canConvert<
int>())
394 if (skipNullValues &&
value.isNull())
400 if ( ((
value.type() == QVariant::String) || (
value.type() == QVariant::ByteArray)))
402 const QString str =
value.toString();
404 iValue = str.toInt(&ok, base);
408 iValue =
value.toInt(&ok);
423 template <
typename T>
426 return m_query.value(0).value<T>();
434 return m_query.value(0);
475 value = m_query.value(m_fetchIndex).toInt();
487 value = m_query.value(m_fetchIndex).toInt();
499 value = m_query.value(m_fetchIndex).toString();
511 value = m_query.value(m_fetchIndex).toBool();
523 value = m_query.value(m_fetchIndex).toDate();
535 value = m_query.value(m_fetchIndex).toDateTime();
547 value = m_query.value(m_fetchIndex).toTime();
559 value = m_query.value(m_fetchIndex).toByteArray();
571 value = m_query.value(m_fetchIndex);
578 value = m_query.value(m_fetchIndex++).toInt();
585 value = m_query.value(m_fetchIndex++).toDouble();
592 value = m_query.value(m_fetchIndex++).toString();
599 value = m_query.value(m_fetchIndex++).toBool();
606 value = m_query.value(m_fetchIndex++).toDate();
611 template <
typename... Rest>
void fetchVars(QDateTime &
value, Rest&... rest)
const 613 value = m_query.value(m_fetchIndex++).toDateTime();
620 value = m_query.value(m_fetchIndex++).toTime();
625 template <
typename... Rest>
void fetchVars(QByteArray &
value, Rest&... rest)
const 627 value = m_query.value(m_fetchIndex++).toByteArray();
634 value = m_query.value(m_fetchIndex++);
688 const QMetaObject *metaobject =
object.metaObject();
689 const int count = metaobject->propertyCount();
691 const QVariantMap map =
toMap();
693 for (
int i = 0; i < count; ++i)
695 QMetaProperty metaproperty = metaobject->property(i);
697 if (metaproperty.isWritable())
699 QLatin1String sName(metaproperty.name());
701 if (map.contains(sName))
703 object.setProperty(sName.data(), map.value(sName));
760 const QMetaObject &metaobject = gadget.staticMetaObject;
762 const int count = metaobject.propertyCount();
764 const QVariantMap map =
toMap();
766 for (
int i = 0; i < count; ++i)
768 QMetaProperty metaproperty = metaobject.property(i);
770 if (metaproperty.isWritable())
772 QLatin1String sName(metaproperty.name());
774 if (map.contains(sName))
776 metaproperty.writeOnGadget(&gadget, map.value(sName));
791 for (
int i = 0; i < m_fieldNames.count(); ++i)
793 map.insert(m_fieldNames.at(i), m_query.value(i));
806 for (
int i = 0; i < m_fieldNames.count(); ++i)
808 list.append(m_query.value(i));
820 vector.reserve(m_fieldNames.count());
822 for (
int i = 0; i < m_fieldNames.count(); ++i)
824 vector.append(m_query.value(i));
837 for (
int i = 0; i < m_fieldNames.count(); ++i)
839 list.append(m_query.value(i).toString());
852 QueryResult(
const QSqlQuery &query,
const QMap<QString, int> &bindValueAliasMap)
854 , m_bindValueAlias(bindValueAliasMap)
859 QStringList m_fieldNames;
860 QMap<QString, int> m_bindValueAlias;
861 mutable int m_fetchIndex = 0;
862 bool m_firstRowFetched =
false;
865 #endif // EASYQTSQL_QUERYRESULT_H bool previous()
Retrieves the previous record in the result, if available, and positions the query on the retrieved r...
Definition: EasyQtSql_QueryResult.h:86
void fetchVars(double &value) const
Assigns a variable the value from the current record.
Definition: EasyQtSql_QueryResult.h:485
QSqlError lastError() const
Returns error information about the last error (if any) that occurred with this query.
Definition: EasyQtSql_QueryResult.h:201
QVector< QVariant > toVector() const
Returns QVector<QVariant> filled with values fetched from current result row. SQL values not converte...
Definition: EasyQtSql_QueryResult.h:334
void fetchVector(QVector< QVariant > &vector) const
Fills QVector<QVariant> with values fetched from current result row. SQL values not converted: QSqlQu...
Definition: EasyQtSql_QueryResult.h:817
Class for query preparation and execution.
Definition: EasyQtSql_PreparedQuery.h:63
QVariant scalar() const
Returns scalar value (the value of the first column of the current row)
Definition: EasyQtSql_QueryResult.h:432
void fetchVars(QVariant &value) const
Assigns a variable the value from the current record.
Definition: EasyQtSql_QueryResult.h:569
bool isValid() const
Returns true if the query is currently positioned on a valid record; otherwise returns false...
Definition: EasyQtSql_QueryResult.h:161
T scalar() const
Returns scalar value converted to type T (the value of the first column of the current row) ...
Definition: EasyQtSql_QueryResult.h:424
QVector< int > parseToIntVector(int base=10, bool skipNullValues=false) const
Returns vector of (optionally parsed) integer values from currently active result row...
Definition: EasyQtSql_QueryResult.h:381
QVariant boundValue(const QString &aliasName) const
Returns the value for the placeholder with aliasName.
Definition: EasyQtSql_QueryResult.h:265
QSqlQuery & unwrappedQuery()
Returns reference on wrapped QSqlQuery.
Definition: EasyQtSql_QueryResult.h:50
void fetchVars(QByteArray &value) const
Assigns a variable the value from the current record.
Definition: EasyQtSql_QueryResult.h:557
void fetchVars(QDateTime &value) const
Assigns a variable the value from the current record.
Definition: EasyQtSql_QueryResult.h:533
void fetchGadget(T &gadget) const
Fills Q_GADGET object properties with data fetched from current result row.
Definition: EasyQtSql_QueryResult.h:758
QVariant value(const QString &colName) const
Returns the value of the field called name in the current record. If field name does not exist an inv...
Definition: EasyQtSql_QueryResult.h:241
void fetchVars(bool &value, Rest &... rest) const
Definition: EasyQtSql_QueryResult.h:597
void fetchVars(QByteArray &value, Rest &... rest) const
Definition: EasyQtSql_QueryResult.h:625
void fetchVars(int &value) const
Assigns a variable the value from the current record.
Definition: EasyQtSql_QueryResult.h:473
QVariant boundValue(int column) const
Returns the value for the placeholder at position column.
Definition: EasyQtSql_QueryResult.h:251
void fetchVars(QDate &value, Rest &... rest) const
Definition: EasyQtSql_QueryResult.h:604
bool isForwardOnly() const
Returns true if you can only scroll forward through a result set; otherwise returns false...
Definition: EasyQtSql_QueryResult.h:171
void fetchVars(QString &value, Rest &... rest) const
Definition: EasyQtSql_QueryResult.h:590
int at() const
Returns the current internal position of the query. The first record is at position zero...
Definition: EasyQtSql_QueryResult.h:141
int size() const
Returns the size of the result (number of rows returned), or -1 if the size cannot be determined or i...
Definition: EasyQtSql_QueryResult.h:181
void fetchMap(QVariantMap &map) const
Fills QVariantMap with values fetched from current result row. Key is QString (result column name) an...
Definition: EasyQtSql_QueryResult.h:787
QVariantList toList() const
Returns QVariantList filled with values fetched from current result row.
Definition: EasyQtSql_QueryResult.h:308
void fetchVars(QDateTime &value, Rest &... rest) const
Definition: EasyQtSql_QueryResult.h:611
QVector< T > toVector(bool skipNullValues=false) const
Returns QVector<T> filled with values fetched from current result row. SQL values converted with QVar...
Definition: EasyQtSql_QueryResult.h:350
void fetchVars(QTime &value) const
Assigns a variable the value from the current record.
Definition: EasyQtSql_QueryResult.h:545
bool last()
Retrieves the last record in the result, if available, and positions the query on the retrieved recor...
Definition: EasyQtSql_QueryResult.h:120
QVariantList boundValues() const
Returns list of the bound values (with positional binding)
Definition: EasyQtSql_QueryResult.h:284
QSqlDatabase wrapper.
Definition: EasyQtSql_Transaction.h:67
bool isActive() const
Returns true if the query is active. An active QSqlQuery is one that has been exec()'d successfully b...
Definition: EasyQtSql_QueryResult.h:151
void fetchVars(QDate &value) const
Assigns a variable the value from the current record.
Definition: EasyQtSql_QueryResult.h:521
void fetchVars(QVariant &value, Rest &... rest) const
Definition: EasyQtSql_QueryResult.h:632
int numRowsAffected() const
Returns the number of rows affected by the result's SQL statement, or -1 if it cannot be determined...
Definition: EasyQtSql_QueryResult.h:191
QVariant value(int column) const
Returns the value of field index in the current record.
Definition: EasyQtSql_QueryResult.h:231
void fetchVars(double &value, Rest &... rest) const
Definition: EasyQtSql_QueryResult.h:583
QVariantMap toMap() const
Returns QVariantMap filled with values fetched from current result row. Key is QString (result column...
Definition: EasyQtSql_QueryResult.h:295
void fetchVars(bool &value) const
Assigns a variable the value from the current record.
Definition: EasyQtSql_QueryResult.h:509
bool nextResult()
Discards the current result set and navigates to the next if available (if database is capable of ret...
Definition: EasyQtSql_QueryResult.h:96
bool seek(int index, bool relative=false)
Retrieves the record at position index, if available, and positions the query on the retrieved record...
Definition: EasyQtSql_QueryResult.h:130
void fetchObject(QObject &object) const
Fills Q_OBJECT object properties with data fetched from current result row.
Definition: EasyQtSql_QueryResult.h:686
QSqlQuery wrapper with handy data fetch methods.
Definition: EasyQtSql_QueryResult.h:39
QSqlDatabase transaction wrapper.
Definition: EasyQtSql_Transaction.h:398
void fetchStringList(QStringList &list) const
Fills QStringList with values fetched from current result row. SQL values converted to QString with Q...
Definition: EasyQtSql_QueryResult.h:833
bool next()
Retrieves the next record in the result, if available, and positions the query on the retrieved recor...
Definition: EasyQtSql_QueryResult.h:60
void fetchVars(QString &value) const
Assigns a variable the value from the current record.
Definition: EasyQtSql_QueryResult.h:497
QStringList toStringList() const
Returns QStringList filled with values fetched from current result row. SQL values converted to QStri...
Definition: EasyQtSql_QueryResult.h:321
bool first()
Retrieves the first record in the result, if available, and positions the query on the retrieved reco...
Definition: EasyQtSql_QueryResult.h:110
void fetchVars(QTime &value, Rest &... rest) const
Definition: EasyQtSql_QueryResult.h:618
QString lastQuery() const
Returns the text of the current query being used, or an empty string if there is no current query tex...
Definition: EasyQtSql_QueryResult.h:211
void fetchList(QVariantList &list) const
Fills QVariantList with values fetched from current result row. SQL values not converted: QSqlQuery::...
Definition: EasyQtSql_QueryResult.h:802
QString executedQuery() const
Returns the last query that was successfully executed.
Definition: EasyQtSql_QueryResult.h:221
void fetchVars(int &value, Rest &... rest) const
Definition: EasyQtSql_QueryResult.h:576