-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Need Row.opIndex(string) #168
Comments
Just because it was asked about today, |
I think what I'd had in mind was to just use an AA internally and ditch asAA (or just leave it for backwards compatibility), but now that I think about it, that would mean behind-the-scenes heap allocations even when name-based access isn't used. (Damn...) Good point about adding asAA to Row, though. |
OTOH, I would think that in the vast majority of cases, the "n" in O(n) would be small enough to be negligible (even when combined with the total number of O(n) lookups also being "n"). Combine that with a note in the docs about it being O(n) ("so therefore don't use it if you're returning a huge number of columns"), and it might not be so bad. Albeit, not ideal. Maybe there's some clever way to use templating to make the cost of the "internal AA" solution be pay-as-you-go? I just find it such a pain, and horrible UI, to be accessing columns by index instead of name. There's got to be a good solution for this... |
One "clever" way to do it may be to use an accessor for the map, which can be O(n). So something like: auto row = conn.queryRow(...);
auto val = row[row.col("id")]; // in-line fetching of column index O(n)
auto idcol = row.col("id"); // cache it O(n) lookup
val = row[colid]; // O(1) lookup Honestly, I hate dealing with Variant (or even Algebraic), and much rather would deal with a serialized type. My internal library consists mainly of a foreach(MyType t; conn.query(...).byItem!MyType)
{
auto val = t.id; // typed correctly, easy access.
} Behind the scenes, I might publish this some day, but it's pretty undocumented and probably not ready for public consumption. If you want to do it like all other libraries with an opIndex on the name, the best way is likely a relatively static AA mapping generated once when the sequence is fetched. Since you are already generating GC data (you are already creating an array of strings for the column names). So maybe this isn't so bad. The only thing that sucks is that the builtin AA allocates a lot of little things (one per item), whereas we only really need a static and internal lookup table. I wonder if there is something already out there. At the very least we can kind of duplicate what AA's do, but make it into a simple array instead. |
I threw this together. What do you think? https://github.com/schveiguy/lookuptable |
I'm going to look into including lookuptable for this. I think the functionality would allow for a nice medium ground (have been using lookuptable in my project for some time). |
Been itching for this one for years. Need to finally get it in.
The information needed to lookup columns by name should already be there (in
ResultRange
). Just need tostash the column into into(UPDATE: Already done as of #188 and v2.3.0) and the rest should be trivial.Row
The text was updated successfully, but these errors were encountered: