Search code examples
mysqldvariant

How to make array from MySQL ROW type?


I am using mysql-native driver. My code:

ResultRange MySQLTablesRange = mysqlconnection.query(`SELECT historysensor FROM TableName`);
auto historysensor = MySQLTablesRange.array.filter!(a=>a[0].coerce!string.canFind("historysensor"));

But on send string I am getting to historysensor not an array, but structure like: Row([historysensor_10774], [false]). So every time to get value I need to do a lot of casting like:

foreach(sensor;historysensor)
{
    writeln(sensor[0].coerce!string.split("_")[1]);
}

How I can make historysensor as simple array, to be able to work without [0].coerce!string?


Solution

  • You can just map it before doing the other stuff

    auto historysensor = MySQLTablesRange.array.
       map!(a => a[0].coerce!string). // this is new
       filter!(a.canFind("historysensor"). // no need for coerce here now
       array; // convert to plain array of strings
    

    BTW using filter here is probably a mistake too, make that part of your query so the database does it, that would be likely more efficient and easier to read.