QSqlQuery::value string overload

Introduce value overload to take field name as a parameter.

This allows for terser application code that avoids explicit
calls to QSqlRecord::value().

Change-Id: I02b6712cd5ec41633b902714315b5716c17d1a9b
Reviewed-by: Mark Brand <mabrand@mabrand.nl>
This commit is contained in:
Thiago A. Correa 2012-10-03 22:52:59 -03:00 committed by The Qt Project
parent 179437bdba
commit 7e6e141234
3 changed files with 20 additions and 0 deletions

View File

@ -405,6 +405,24 @@ QVariant QSqlQuery::value(int index) const
return QVariant();
}
/*!
\overload
Returns the value of the field called \a name in the current record.
If field \a name does not exist an invalid variant is returned.
This overload is less efficient than \l{QSqlQuery::}{value()}
*/
QVariant QSqlQuery::value(const QString& name) const
{
int index = d->sqlResult->record().indexOf(name);
if (index > -1)
return value(index);
qWarning("QSqlQuery::value: unknown field name '%s'", qPrintable(name));
return QVariant();
}
/*!
Returns the current internal position of the query. The first
record is at position zero. If the position is invalid, the

View File

@ -86,6 +86,7 @@ public:
void setForwardOnly(bool forward);
bool exec(const QString& query);
QVariant value(int i) const;
QVariant value(const QString& name) const;
void setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy);
QSql::NumericalPrecisionPolicy numericalPrecisionPolicy() const;

View File

@ -903,6 +903,7 @@ void tst_QSqlQuery::value()
while ( q.next() ) {
QCOMPARE( q.value( 0 ).toInt(), i );
QCOMPARE( q.value( "id" ).toInt(), i );
if ( db.driverName().startsWith( "QIBASE" ) )
QVERIFY( q.value( 1 ).toString().startsWith( "VarChar" + QString::number( i ) ) );