EasyQtSql
Easy SQL data access helper for QtSql
QueryResult Class Reference

QSqlQuery wrapper with handy data fetch methods. More...

#include <EasyQtSql_QueryResult.h>

Public Member Functions

QSqlQuery & unwrappedQuery ()
 Returns reference on wrapped QSqlQuery. More...
 
bool next ()
 Retrieves the next record in the result, if available, and positions the query on the retrieved record. More...
 
bool previous ()
 Retrieves the previous record in the result, if available, and positions the query on the retrieved record. More...
 
bool nextResult ()
 Discards the current result set and navigates to the next if available (if database is capable of returning multiple result sets) More...
 
bool first ()
 Retrieves the first record in the result, if available, and positions the query on the retrieved record. More...
 
bool last ()
 Retrieves the last record in the result, if available, and positions the query on the retrieved record. More...
 
bool seek (int index, bool relative=false)
 Retrieves the record at position index, if available, and positions the query on the retrieved record. More...
 
int at () const
 Returns the current internal position of the query. The first record is at position zero. More...
 
bool isActive () const
 Returns true if the query is active. An active QSqlQuery is one that has been exec()'d successfully but not yet finished with. More...
 
bool isValid () const
 Returns true if the query is currently positioned on a valid record; otherwise returns false. More...
 
bool isForwardOnly () const
 Returns true if you can only scroll forward through a result set; otherwise returns false. More...
 
int size () const
 Returns the size of the result (number of rows returned), or -1 if the size cannot be determined or if the database does not support reporting information about query sizes. More...
 
int numRowsAffected () const
 Returns the number of rows affected by the result's SQL statement, or -1 if it cannot be determined. More...
 
QSqlError lastError () const
 Returns error information about the last error (if any) that occurred with this query. More...
 
QString lastQuery () const
 Returns the text of the current query being used, or an empty string if there is no current query text. More...
 
QString executedQuery () const
 Returns the last query that was successfully executed. More...
 
QVariant value (int column) const
 Returns the value of field index in the current record. More...
 
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 invalid variant is returned. More...
 
QVariant boundValue (int column) const
 Returns the value for the placeholder at position column. More...
 
QVariant boundValue (const QString &aliasName) const
 Returns the value for the placeholder with aliasName. More...
 
QVariantList boundValues () const
 Returns list of the bound values (with positional binding) More...
 
QVariantMap toMap () const
 Returns QVariantMap filled with values fetched from current result row. Key is QString (result column name) and value is QVariant value. More...
 
QVariantList toList () const
 Returns QVariantList filled with values fetched from current result row. More...
 
QStringList toStringList () const
 Returns QStringList filled with values fetched from current result row. SQL values converted to QString with QVariant::toString() method. More...
 
QVector< QVariant > toVector () const
 Returns QVector<QVariant> filled with values fetched from current result row. SQL values not converted: QSqlQuery::value() return value used. More...
 
template<typename T >
QVector< T > toVector (bool skipNullValues=false) const
 Returns QVector<T> filled with values fetched from current result row. SQL values converted with QVariant::value<T>(). QVector::canConvert<T>() method used for convertability check. Non-convertable values skipped. More...
 
QVector< int > parseToIntVector (int base=10, bool skipNullValues=false) const
 Returns vector of (optionally parsed) integer values from currently active result row. More...
 
template<typename T >
scalar () const
 Returns scalar value converted to type T (the value of the first column of the current row) More...
 
QVariant scalar () const
 Returns scalar value (the value of the first column of the current row) More...
 
void fetchVars (int &value) const
 Assigns a variable the value from the current record. More...
 
void fetchVars (double &value) const
 Assigns a variable the value from the current record. More...
 
void fetchVars (QString &value) const
 Assigns a variable the value from the current record. More...
 
void fetchVars (bool &value) const
 Assigns a variable the value from the current record. More...
 
void fetchVars (QDate &value) const
 Assigns a variable the value from the current record. More...
 
void fetchVars (QDateTime &value) const
 Assigns a variable the value from the current record. More...
 
void fetchVars (QTime &value) const
 Assigns a variable the value from the current record. More...
 
void fetchVars (QByteArray &value) const
 Assigns a variable the value from the current record. More...
 
void fetchVars (QVariant &value) const
 Assigns a variable the value from the current record. More...
 
template<typename... Rest>
void fetchVars (int &value, Rest &... rest) const
 
template<typename... Rest>
void fetchVars (double &value, Rest &... rest) const
 
template<typename... Rest>
void fetchVars (QString &value, Rest &... rest) const
 
template<typename... Rest>
void fetchVars (bool &value, Rest &... rest) const
 
template<typename... Rest>
void fetchVars (QDate &value, Rest &... rest) const
 
template<typename... Rest>
void fetchVars (QDateTime &value, Rest &... rest) const
 
template<typename... Rest>
void fetchVars (QTime &value, Rest &... rest) const
 
template<typename... Rest>
void fetchVars (QByteArray &value, Rest &... rest) const
 
template<typename... Rest>
void fetchVars (QVariant &value, Rest &... rest) const
 
void fetchObject (QObject &object) const
 Fills Q_OBJECT object properties with data fetched from current result row. More...
 
template<typename T >
void fetchGadget (T &gadget) const
 Fills Q_GADGET object properties with data fetched from current result row. More...
 
void fetchMap (QVariantMap &map) const
 Fills QVariantMap with values fetched from current result row. Key is QString (result column name) and value is QVariant value. More...
 
void fetchList (QVariantList &list) const
 Fills QVariantList with values fetched from current result row. SQL values not converted: QSqlQuery::value() return value used. More...
 
void fetchVector (QVector< QVariant > &vector) const
 Fills QVector<QVariant> with values fetched from current result row. SQL values not converted: QSqlQuery::value() return value used. More...
 
void fetchStringList (QStringList &list) const
 Fills QStringList with values fetched from current result row. SQL values converted to QString with QVariant::toString() method. More...
 

Friends

class Database
 
class Transaction
 
class PreparedQuery
 

Detailed Description

QSqlQuery wrapper with handy data fetch methods.

Member Function Documentation

◆ at()

int QueryResult::at ( ) const
inline

Returns the current internal position of the query. The first record is at position zero.

Returns
If the position is invalid, the function returns QSql::BeforeFirstRow or QSql::AfterLastRow, which are special negative values.

Wrapper over QSqlQuery::at()

◆ boundValue() [1/2]

QVariant QueryResult::boundValue ( int  column) const
inline

Returns the value for the placeholder at position column.

Wrapper over QSqlQuery::boundValue()

◆ boundValue() [2/2]

QVariant QueryResult::boundValue ( const QString &  aliasName) const
inline

Returns the value for the placeholder with aliasName.

Use subclasses of ParamDirectionWrapper class (In, Out, InOut) for aliased/directional parameter binding during PreparedQuery::exec execution

Use Out and InOut wrappers if you need to read Stored Procedures return values (output parameters).

See also
In, Out, InOut, PreparedQuery::exec

◆ boundValues()

QVariantList QueryResult::boundValues ( ) const
inline

Returns list of the bound values (with positional binding)

Wrapper over QSqlQuery::boundValues()

◆ executedQuery()

QString QueryResult::executedQuery ( ) const
inline

Returns the last query that was successfully executed.

Wrapper over QSqlQuery::executedQuery()

◆ fetchGadget()

template<typename T >
void QueryResult::fetchGadget ( T &  gadget) const
inline

Fills Q_GADGET object properties with data fetched from current result row.

Qt Meta-Object System used. Only writable properties can be filled.

C++ struct fields will be mapped on SQL result table by Q_PROPERTY member names. For example, if Q_GADGET struct contains Q_PROPERTY named "e" but SELECT query defines no "e" column, object property "e" not affected by the method.

struct Row
{
int a;
int b;
int c;
QString d;
int e;
private:
Q_GADGET
//property "a" will be filled with "a" column of the Select SQL query result
Q_PROPERTY(int a MEMBER a)
Q_PROPERTY(int b MEMBER b)
Q_PROPERTY(int c MEMBER c)
Q_PROPERTY(QString d MEMBER d)
//property e will be ignored in the example because there is no e field in the Select SQL query result
Q_PROPERTY(int e MEMBER e)
};
void test()
{
QueryResult res = t.execQuery("SELECT a, b, c, d FROM table");
while (res.next())
{
//Row instance (Q_GADGET structure)
Row testRow;
//fetch testRow from current result row, properties a, b, c, d will be filled here:
res.fetchGadget(testRow);
}
}
Parameters
gadgetQ_GADGET reference

◆ fetchList()

void QueryResult::fetchList ( QVariantList &  list) const
inline

Fills QVariantList with values fetched from current result row. SQL values not converted: QSqlQuery::value() return value used.

Parameters
[out]listQVariantList
See also
QueryResult::toList

◆ fetchMap()

void QueryResult::fetchMap ( QVariantMap &  map) const
inline

Fills QVariantMap with values fetched from current result row. Key is QString (result column name) and value is QVariant value.

Parameters
[out]mapQVariantMap
See also
QueryResult::toMap

◆ fetchObject()

void QueryResult::fetchObject ( QObject &  object) const
inline

Fills Q_OBJECT object properties with data fetched from current result row.

Qt Meta-Object System used. Only writable properties can be filled.

C++ class fields (properties) will be mapped on SQL result table by Q_PROPERTY member names. For example, if Q_OBJECT class contains Q_PROPERTY named "e" but SELECT query defines no "e" column, object property "e" not affected by the method.

class TestObject : public QObject
{
Q_OBJECT
//property "a" will be filled with "a" column of the Select SQL query result
Q_PROPERTY(int a MEMBER a)
Q_PROPERTY(int b MEMBER b)
Q_PROPERTY(int c MEMBER c)
Q_PROPERTY(QString d MEMBER d)
//property e will be ignored in the example because there is no e field in the Select SQL query result
Q_PROPERTY(int e MEMBER e)
private:
int a;
int b;
int c;
QString d;
int e;
};
void test()
{
QueryResult res = t.execQuery("SELECT a, b, c, d FROM table");
while (res.next())
{
//TestObject instance (Q_OBJECT class)
TestObject testRow;
res.fetchObject(testRow);
}
}
Parameters
objectQ_OBJECT object reference

◆ fetchStringList()

void QueryResult::fetchStringList ( QStringList &  list) const
inline

Fills QStringList with values fetched from current result row. SQL values converted to QString with QVariant::toString() method.

Parameters
[out]listQStringList
See also
QueryResult::toStringList

◆ fetchVars() [1/18]

void QueryResult::fetchVars ( int &  value) const
inline

Assigns a variable the value from the current record.

The method has a variable number of parameters, which allows filling the list of variables with values from the current record in single call.

Overloaded EasyQtSql::fetchVars methods performs the corresponding type conversion with QVariant::to*() methods.

Supported parameter types: int, double, bool, QString, QDate, QDateTime, QTime, QByteArray, QVariant.

Variables of different types can be mixed.

QueryResult res = t.execQuery("SELECT a, b, c, d FROM table");
while (res.next())
{
int a;
bool b;
QString c;
QDateTime d;
res.fetchVars(a, b, c, d);
}
See also
QueryResult::fetchVars(int&) const , QueryResult::fetchVars(double&) const , QueryResult::fetchVars(QString&) const , QueryResult::fetchVars(bool&) const , QueryResult::fetchVars(QDate&) const , QueryResult::fetchVars(QDateTime&) const , QueryResult::fetchVars(QTime&) const , QueryResult::fetchVars(QByteArray&) const , QueryResult::fetchVars(QVariant&) const

◆ fetchVars() [2/18]

void QueryResult::fetchVars ( double &  value) const
inline

Assigns a variable the value from the current record.

See also
QueryResult::fetchVars(int&) const

◆ fetchVars() [3/18]

void QueryResult::fetchVars ( QString &  value) const
inline

Assigns a variable the value from the current record.

See also
QueryResult::fetchVars(int&) const

◆ fetchVars() [4/18]

void QueryResult::fetchVars ( bool &  value) const
inline

Assigns a variable the value from the current record.

See also
QueryResult::fetchVars(int&) const

◆ fetchVars() [5/18]

void QueryResult::fetchVars ( QDate &  value) const
inline

Assigns a variable the value from the current record.

See also
QueryResult::fetchVars(int&) const

◆ fetchVars() [6/18]

void QueryResult::fetchVars ( QDateTime &  value) const
inline

Assigns a variable the value from the current record.

See also
QueryResult::fetchVars(int&) const

◆ fetchVars() [7/18]

void QueryResult::fetchVars ( QTime &  value) const
inline

Assigns a variable the value from the current record.

See also
QueryResult::fetchVars(int&) const

◆ fetchVars() [8/18]

void QueryResult::fetchVars ( QByteArray &  value) const
inline

Assigns a variable the value from the current record.

See also
QueryResult::fetchVars(int&) const

◆ fetchVars() [9/18]

void QueryResult::fetchVars ( QVariant &  value) const
inline

Assigns a variable the value from the current record.

See also
QueryResult::fetchVars(int&) const

◆ fetchVars() [10/18]

template<typename... Rest>
void QueryResult::fetchVars ( int &  value,
Rest &...  rest 
) const
inline

◆ fetchVars() [11/18]

template<typename... Rest>
void QueryResult::fetchVars ( double &  value,
Rest &...  rest 
) const
inline

◆ fetchVars() [12/18]

template<typename... Rest>
void QueryResult::fetchVars ( QString &  value,
Rest &...  rest 
) const
inline

◆ fetchVars() [13/18]

template<typename... Rest>
void QueryResult::fetchVars ( bool &  value,
Rest &...  rest 
) const
inline

◆ fetchVars() [14/18]

template<typename... Rest>
void QueryResult::fetchVars ( QDate &  value,
Rest &...  rest 
) const
inline

◆ fetchVars() [15/18]

template<typename... Rest>
void QueryResult::fetchVars ( QDateTime &  value,
Rest &...  rest 
) const
inline

◆ fetchVars() [16/18]

template<typename... Rest>
void QueryResult::fetchVars ( QTime &  value,
Rest &...  rest 
) const
inline

◆ fetchVars() [17/18]

template<typename... Rest>
void QueryResult::fetchVars ( QByteArray &  value,
Rest &...  rest 
) const
inline

◆ fetchVars() [18/18]

template<typename... Rest>
void QueryResult::fetchVars ( QVariant &  value,
Rest &...  rest 
) const
inline

◆ fetchVector()

void QueryResult::fetchVector ( QVector< QVariant > &  vector) const
inline

Fills QVector<QVariant> with values fetched from current result row. SQL values not converted: QSqlQuery::value() return value used.

Parameters
[out]vectorQVector<QVariant>
See also
QueryResult::toVector

◆ first()

bool QueryResult::first ( )
inline

Retrieves the first record in the result, if available, and positions the query on the retrieved record.

Wrapper over QSqlQuery::first()

◆ isActive()

bool QueryResult::isActive ( ) const
inline

Returns true if the query is active. An active QSqlQuery is one that has been exec()'d successfully but not yet finished with.

Wrapper over QSqlQuery::isActive()

◆ isForwardOnly()

bool QueryResult::isForwardOnly ( ) const
inline

Returns true if you can only scroll forward through a result set; otherwise returns false.

Wrapper over QSqlQuery::isForwardOnly()

◆ isValid()

bool QueryResult::isValid ( ) const
inline

Returns true if the query is currently positioned on a valid record; otherwise returns false.

Wrapper over QSqlQuery::isValid()

◆ last()

bool QueryResult::last ( )
inline

Retrieves the last record in the result, if available, and positions the query on the retrieved record.

Wrapper over QSqlQuery::last()

◆ lastError()

QSqlError QueryResult::lastError ( ) const
inline

Returns error information about the last error (if any) that occurred with this query.

Wrapper over QSqlQuery::lastError()

◆ lastQuery()

QString QueryResult::lastQuery ( ) const
inline

Returns the text of the current query being used, or an empty string if there is no current query text.

Wrapper over QSqlQuery::lastQuery()

◆ next()

bool QueryResult::next ( )
inline

Retrieves the next record in the result, if available, and positions the query on the retrieved record.

Wrapper over QSqlQuery::next()

◆ nextResult()

bool QueryResult::nextResult ( )
inline

Discards the current result set and navigates to the next if available (if database is capable of returning multiple result sets)

Wrapper over QSqlQuery::nextResult()

◆ numRowsAffected()

int QueryResult::numRowsAffected ( ) const
inline

Returns the number of rows affected by the result's SQL statement, or -1 if it cannot be determined.

Wrapper over QSqlQuery::numRowsAffected()

◆ parseToIntVector()

QVector<int> QueryResult::parseToIntVector ( int  base = 10,
bool  skipNullValues = false 
) const
inline

Returns vector of (optionally parsed) integer values from currently active result row.

Parsing with specified base will be performed if type of selected database field is String (varchar/text). Not successfully parsed string values are ignored.

Parameters
baseNumber base to parse int values from SQL string values.
skipNullValuesSkip SQL NULL values. Resulting int value is 0 if skipNullValues = false.

◆ previous()

bool QueryResult::previous ( )
inline

Retrieves the previous record in the result, if available, and positions the query on the retrieved record.

Wrapper over QSqlQuery::previous()

◆ scalar() [1/2]

template<typename T >
T QueryResult::scalar ( ) const
inline

Returns scalar value converted to type T (the value of the first column of the current row)

◆ scalar() [2/2]

QVariant QueryResult::scalar ( ) const
inline

Returns scalar value (the value of the first column of the current row)

◆ seek()

bool QueryResult::seek ( int  index,
bool  relative = false 
)
inline

Retrieves the record at position index, if available, and positions the query on the retrieved record.

Wrapper over QSqlQuery::seek()

◆ size()

int QueryResult::size ( ) const
inline

Returns the size of the result (number of rows returned), or -1 if the size cannot be determined or if the database does not support reporting information about query sizes.

Wrapper over QSqlQuery::size()

◆ toList()

QVariantList QueryResult::toList ( ) const
inline

Returns QVariantList filled with values fetched from current result row.

See also
QueryResult::fetchList

◆ toMap()

QVariantMap QueryResult::toMap ( ) const
inline

Returns QVariantMap filled with values fetched from current result row. Key is QString (result column name) and value is QVariant value.

See also
QueryResult::fetchMap

◆ toStringList()

QStringList QueryResult::toStringList ( ) const
inline

Returns QStringList filled with values fetched from current result row. SQL values converted to QString with QVariant::toString() method.

See also
QueryResult::fetchStringList()

◆ toVector() [1/2]

QVector<QVariant> QueryResult::toVector ( ) const
inline

Returns QVector<QVariant> filled with values fetched from current result row. SQL values not converted: QSqlQuery::value() return value used.

See also
QueryResult::fetchVector

◆ toVector() [2/2]

template<typename T >
QVector<T> QueryResult::toVector ( bool  skipNullValues = false) const
inline

Returns QVector<T> filled with values fetched from current result row. SQL values converted with QVariant::value<T>(). QVector::canConvert<T>() method used for convertability check. Non-convertable values skipped.

Parameters
skipNullValuesSkip SQL NULL values. Resulting value is default value if skipNullValues = false.
See also
QueryResult::toVector

◆ unwrappedQuery()

QSqlQuery& QueryResult::unwrappedQuery ( )
inline

Returns reference on wrapped QSqlQuery.

◆ value() [1/2]

QVariant QueryResult::value ( int  column) const
inline

Returns the value of field index in the current record.

Wrapper over QSqlQuery::value()

◆ value() [2/2]

QVariant QueryResult::value ( const QString &  colName) const
inline

Returns the value of the field called name in the current record. If field name does not exist an invalid variant is returned.

Wrapper over QSqlQuery::value()

Friends And Related Function Documentation

◆ Database

friend class Database
friend

◆ PreparedQuery

friend class PreparedQuery
friend

◆ Transaction

friend class Transaction
friend

The documentation for this class was generated from the following file: