You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
mysql-native's toStruct is quite limited currently (e.g. when compared to mysql-lited or vibe.data serialization). A better implementation is blocked mainly by the fact, that the Row does currently not know the column names. Only indices are available (but these depend on the sql query string, so it's also not easily possible to recover field names from these ids).
This PR simply exposes the names of the columns. A basic toStruct mapper could then look like this:
T parse(T)(const Row row)
{
T result;
row.parse(result);
return result;
}
voidparse(T)(const Row row, ref T result)
{
foreach (size_t i, name; row.names)
{
result.setField(name, row[i]);
}
}
voidsetField(T)(ref T result, string name, Variant value)
{
importstd.traits;
foreach (idx, member; result.tupleof)
{
enum mname = T.tupleof[idx].stringof;
if (name == mname)
{
alias FieldType = typeof(__traits(getMember, result, mname));
convertField!FieldType(__traits(getMember, result, mname), value);
}
}
}
voidconvertField(T)(ref T field, Variant value)
{
importstd.traits, std.datetime;
aliasUT = Unqual!T;
// Convert between varian of possily typeof(null) and Nullable!Tstaticif (is(typeof(UT.nullify)))
{
if (value.type ==typeid(typeof(null)))
field.nullify();
else
field = value.coerce!(Unqual!(ReturnType!(UT.get)));
}
elsestaticif (is(UT==DateTime))
{
field = value.get!UT;
}
// All other typeselse
{
field = value.coerce!UT;
}
}
The text was updated successfully, but these errors were encountered:
From @jpf91 over on PR #188:
The text was updated successfully, but these errors were encountered: