bitemyapp / esqueleto

New home of Esqueleto, please file issues so we can get things caught up!
BSD 3-Clause "New" or "Revised" License
372 stars 107 forks source link

[Experimental] More powerful queries #215

Closed belevy closed 3 years ago

belevy commented 3 years ago

Before submitting your PR, check that you've:

After submitting your PR:

This subsumes #213 and #212. It is the merger of the two. Like i mentioned over in #212 I am not particularly happy with the need to maintain so much machinery to support LATERAL but without the newtype wrapper I rather like the syntax here.

Experimental.from $ Table @Lord 
`InnerJoin` (\lords -> do
    ...)
`on` (\(l :& r) -> ...)

Further I am still unhappy with the fact that union_ and unionAll_ feel out of place in the withRecursive syntax compared to the SqlSetOperation syntax.

This PR: fixes #207 fixes #124 fixes #121

belevy commented 3 years ago

Only 150 more lines of indirection and the two kinds of unions are unified. I am still unhappy with how verbose the code is and it still needs comments. In addition this change obviates the need to ever use SelectQuery. union_ et al can take SqlQuery a directly. Likewise SubQuery is also not needed as SqlQuery a in the join tree will be treated as a sub query and a -> SqlQuery b as a lateral query. Both with and withRecursive return a SqlQuery (From a). The logic is much the same as a memo function where you are memoizing a sub query and so it is effectful, if you call it twice you will add two cte's to the query even if they are identical. The resulting from can be used anywhere in a Join Tree and follows the semantics of CTEs in raw sql, namely that you must select from the CTE to get any data. While regular CTE's are useful from a compositional standpoint in raw sql they lose that value in esqueleto as you could just use a function instead.

@parsonsmatt I think I am done with anything that I can do along this path, the only way forward from here is to remove stuff. I am really not super excited to maintain this implementation and it feels like there must be a simpler way.