[Development] Fwd: Re: Proposing QSqlQuery syntax sugar

Andras Mantia andras.mantia at kdab.com
Wed Sep 19 11:52:20 CEST 2012


André Somers wrote:

> Op 19-9-2012 11:32, Andras Mantia schreef:
>> 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).
> Sure, but then you'd have to use
> int myvalue = query.value(0).value<int>();
> 
> The double value in that call is not so nice.

Ah, indeed, I misread the original request.

> I think the suggestion was to overload value in QSqlQuery to directly
> allow the template version:
> int myvalue = query.value<int>(0);
> or
> int myvalue = query.value<int>("MyColumnName"); //using the also
> proposed string version.
> 
> Just a bit of syntactic sugar, of course, but nice. I do think there are
> some problems with error checking though. What happens if "MyColumnName"
> doesn't contain an int but a string in the above call?

Yes, that is true, especially in case of basic types, where there is no 
notion of invalid value.
One could use exceptions in case of error, add a "bool hasValue<T>()" method 
or an option "bool &ok" argument to value, but I'm not 100% convinced this 
will make the code nicer (except maybe the exception approach).

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