QSqlQuery::value string overload
authorThiago A. Correa <thiago.correa@gmail.com>
Thu, 4 Oct 2012 01:52:59 +0000 (22:52 -0300)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Mon, 8 Oct 2012 06:55:28 +0000 (08:55 +0200)
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>
src/sql/kernel/qsqlquery.cpp
src/sql/kernel/qsqlquery.h
tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp

index eff211c..07b23f7 100644 (file)
@@ -406,6 +406,24 @@ QVariant QSqlQuery::value(int index) const
 }
 
 /*!
+    \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
     function returns QSql::BeforeFirstRow or
index 9b4f35c..9d660e0 100644 (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;
index 2030e2c..26768f2 100644 (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 ) ) );