Mysql query result access by field name
Steven Schveighoffer
schveiguy at yahoo.com
Mon May 21 15:22:47 UTC 2018
On 5/21/18 10:59 AM, kdevel wrote:
> On Monday, 21 May 2018 at 14:17:23 UTC, Steven Schveighoffer wrote:
>>> Data f;
>>> allrows[0].toStruct (f);
>>>
>>> I haven't checked this.
>>
>> This only works if your struct has exactly the same layout as the fields.
>>
>> So if, for instance, your rows are selected "title", "name",
>> "surname", but your data type orders them name, surname, title, you
>> won't be happy with the result.
>
> Haven't seen this. Then there is no more field-safety than in the OP's
> "assembler" code.
>
> In the other post you wrote
>
> | 1. Use ResultRange instead of the Row interface. This provides
> | a couple of ways to use column names, .asAA to get all the data
> | in a nice AA format (they are still variants),
>
> The AA format can than be used to fill the struct automatically
> (detecting missing and excess fields) like in this code:
>
> T toStructX(T) (string[string] a)
> {
> T t;
> bool[string] bookkeep;
> foreach (i, m; t.tupleof) {
> string key = T.tupleof[i].stringof;
> if (key !in a) {
> stderr.writefln ("missing key <%s>", key);
> continue;
> }
> t.tupleof[i] = a[key].to!(typeof (m));
> bookkeep[key] = true;
> }
> foreach (x, y; a)
> if (x !in bookkeep)
> stderr.writefln ("excess key-value pair <%s>:<%s>", x, y);
> return t;
> }
Yes, this is very similar to what I do in my serialization library,
except I don't use the AA, I keep a map of indexes based on the index of
the field in the tuple to avoid allocation associated with an AA.
Note that your incoming AA is going to be a Variant[string].
-Steve
More information about the Digitalmars-d-learn
mailing list