NyxCode / ormx

bringing orm-like features to sqlx
MIT License
287 stars 32 forks source link

request: add conditional_query! macro? #3

Closed kilpatty closed 2 years ago

kilpatty commented 3 years ago

Would it be possible to add a conditional_query! macro? I use query! from sqlx much more than query_as! so I'd love to be able to replicate some of the possibilities with filtering and pattern matching that you have enabled, but with the other macro.

NyxCode commented 3 years ago

i will look into it, but it will require even more hackery, if possible at all. the problem is that i can't actually name the output type of query! since it's anonymous.

conditional_query_as! creates an enum, with each variant containing a "branch" of the conditional query. If you use 4 branches in your query (using (pattern) = (expression) => { ... }), then this the macro will expand to ConditionalMap16, since there are then 2^4 possible paths your query can take.

I'm not sure i can even construct such an enum when i don't know the output type. So it will be tricky to get this working with query!, but i'll try.

kilpatty commented 3 years ago

Makes sense - no worries honestly, I have to say I'm absolutely in love with what this library enables me to do (I have a ton of filtering in my application, so simplifying my queries to 1 macro call without having to ditch the macros or have a ton of match/if/else statements is amazing.

I'm hoping that sqlx will eventually merge in support for FromRow to be used in query_as so that I can have more "custom" structs but still use them with the query_as macro (which is what has prevent me from using it before).

If this is to complicated of an ask please don't sweat it!

NyxCode commented 2 years ago

I'll close this for now. Such a macro would, I believe, need to be implemented using a procedural macro.

I plan on porting conditional_query_as! to procedural macros in the future anyway.