-
-
Notifications
You must be signed in to change notification settings - Fork 171
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
Nesting JOINs #483
Comments
I've never seen SQL syntax like -- can you link to database vendor documentation that shows that? You don't show what HoneySQL DSL you've actually tried, but I suspect |
Please see https://www.postgresql.org/docs/15/sql-select.html with the following under
I am playing around in the web editor at john.shaffe.rs and managed to get parantheses with
with |
I think my basic conceptual problem with understanding honeysql join syntax is that honeysql treats "joins" as individual clauses, whereas I would rather see them as operators within the "from" clause. |
I'll have to give this some thought. I don't think, right now, this particular syntax is achievable in HoneySQL. It supports a sequence of |
Thanks! When considering, note that with Postgres it is even possible to give an alias to a parenthesized sub-join after the closing parenthesis (which makes referencing the individual tables from outside the parentheses illegal, though). |
I think the easiest solution to this is to provide function-like "special syntax" that allows expressions to be Here's a proposed syntax (overloading the user=> (sql/format {:left-join [[[:join :bar {:outer-join [:foo [:using :id]]}]] [:and [:= :tbl1.col2 :bar.col2] [:= :tbl1.col3 :foo.col3]]]})
["LEFT JOIN (bar OUTER JOIN foo USING (id)) ON (tbl1.col2 = bar.col2) AND (tbl1.col3 = foo.col3)"]
user=> (sql/format {:left-join [[[:join :bar {:outer-join [:foo [:using :id]]}] :wibble] [:and [:= :tbl1.col2 :bar.col2] [:= :tbl1.col3 :foo.col3]]]})
["LEFT JOIN (bar OUTER JOIN foo USING (id)) AS wibble ON (tbl1.col2 = bar.col2) AND (tbl1.col3 = foo.col3)"] That second example is just to show how an alias would factor in here (which is already supported syntax in HoneySQL, even tho' it makes the overall result illegal SQL). Thoughts? |
How would that Would the 'function-like' |
Making it varargs and following that order of joins sounds sensible.
|
develop has been updated with a variadic version of this new syntax, if you want to try it out. I need to add tests and documentation but would like some feedback on it first. |
If you don't want to use git deps to test this (or can't), there's an updated 2.4.9999-SNAPSHOT version on Clojars too. |
Thanks for the quick preview!
So, albeit a tad cumbersome (I don't have a better idea, though), it works for me, and I see that I will be able to produce this map structure from my own data structures recursively in order to produce SQL via honeysql, which is great. |
(I'm rather new to Clojure altogether and to honeysql in particular, so please bear with me if I missed something obvious.)
In advanced SQL usage, there are times when I want to nest
JOIN
s, for example:Is there a way to do this? I have started using
:join-by
as it seems it's the only way to control the "order" of JOINs, but inside the list I hand to:join-by
, it won't accept a nested:join-by
nor a:nest
as join-type, and inside a join's "arguments" it won't recognize these as being another join rather than a table called "nest" or "join" etc.The text was updated successfully, but these errors were encountered: