Mysql query result access by field name
kdevel
kdevel at vogtner.de
Mon May 21 14:59:51 UTC 2018
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;
}
More information about the Digitalmars-d-learn
mailing list