[Development] Fwd: Re: Proposing QSqlQuery syntax sugar

Andras Mantia andras.mantia at kdab.com
Wed Sep 19 11:32:50 CEST 2012


Thiago A. Corrêa wrote:

> Hi,
> 
>       I'd like to propose a templated QSqlQuery::value in the same way
> QVariant::value works. This syntax suggar allows one to write:
> 
>       QSqlQuery query;
>       ...
>       int myvalue = query.value<int>(0);
> 
>       Instead of the current:
>       int myvalue = query.value(0).toInt();

QSqlQuery::value() returns a QVariant already, so anything that works for 
QVariant works here as well (the .toInt() there is a QVariant method).

> 
>       And also QSqlQuery::value( const QString& ) overload to obtain
> column values by name instead of column index.
>       Writting code that relies on column names is more robust to
> changes than indexes, yet in current API it's very clumbersome:
> 
>       int myvalue = query.record().value("MyColumn").toInt();

I'm all for this, as this would be easier for beginners and avoids some hard 
to find problems (you change the query by adding/reordering the columns, and 
suddenly all index based value() calls are wrong). You need to know though 
that this is still not perfect, as it doesn't protect against typos in the 
column name or changes in the column names in the database. The first is a 
common problem and something that is fixable, but not that easily. There are 
code around that does that with templates, but not generic enough yet to be 
submitted to Qt, although I hope one day we can do that.

As adding a new non-virtual function is binary compatible, there should be 
no problem to add a QSqlQuery::value(const QString&) method in 5.1.

Andras
-- 
András Manția | andras.mantia at kdab.com | Senior Software Engineer
Klarälvdalens Datakonsult AB, a KDAB Group company
Tel. Sweden (HQ) +46-563-540090, USA +1-866-777-KDAB(5322)
KDAB - Qt Experts - Platform-independent software solutions



More information about the Development mailing list