christianparpart / Lightweight

thin and lightweight, fast, ODBC API wrapper for modern C++ uses
4 stars 0 forks source link

Template arguments for associations #45

Open Yaraslaut opened 3 weeks ago

Yaraslaut commented 3 weeks ago

https://godbolt.org/z/KPYaTcEds

I think it might be good to have following syntax for complex associations

class A{};
class B{};

class Something{
    HasManyThrough<A,Through<B>> records;
};

And HasManyThrough<A,B> records; will fail to compile

christianparpart commented 3 weeks ago

Oh nice, i'm absolutely open to this. But I am pretty sure I had these cass and they compiled, hmm... :thinking:

edit: wouldn't it make more sense to name it something like HasMany<A,Through<B>> then?

Yaraslaut commented 3 weeks ago

Yes, indeed, associations will be much more clear in a such way, and look similar to ruby's

Yaraslaut commented 3 weeks ago

I tried to implement HasMany<A,Through<B>> and find out that c++ does not allow mixing types and non-types template argument inside variadic templates, the closest I could get is to wrap them inside structure to have

    HasMany<Record<A, "a">, Record<Through<B>, "b">> records;
    HasMany<Record<A, "a">> as;

https://godbolt.org/z/5MvGW3boK

Yaraslaut commented 2 weeks ago

Ok, so here is how to deduce variadic with types/literals using reflection https://godbolt.org/z/145M3143G

christianparpart commented 3 days ago

NB: With the draft-PR on static reflection, we have to adapt the way of how associations work anyways. It seems like we're moving away from the Active Record model towards Data Mapper model in order to satisfy some requirements of C++20, to get static reflection in early (i.e. the Record type must be an aggregate, i.e., we can't use inheritance nor private membmers, just plain structs without any constructor. Plus side is: modeling the SQL tables into structs becomes way more natural)

EDIT: We should still persure something like HasManyThrough<A,Through<B>> with that. :)