fix #529 by changing how table name/expression is formatted in special join syntax

Signed-off-by: Sean Corfield <sean@corfield.org>
This commit is contained in:
Sean Corfield 2024-05-13 15:26:00 -07:00
parent 8764759323
commit 747383c847
No known key found for this signature in database
4 changed files with 28 additions and 2 deletions

View file

@ -1,6 +1,7 @@
# Changes # Changes
* 2.6.next in progress * 2.6.next in progress
* Address [#529](https://github.com/seancorfield/honeysql/issues/529) by fixing `:join` special syntax to support aliases and to handle expressions the same way `select` / `from` etc handle them (extra `[...]` nesting).
* Address [#527](https://github.com/seancorfield/honeysql/issues/527) by adding tests and more documentation for `:composite`. * Address [#527](https://github.com/seancorfield/honeysql/issues/527) by adding tests and more documentation for `:composite`.
* Add example of mixed `DO UPDATE SET` with `EXCLUDED` and regular SQL expressions. * Add example of mixed `DO UPDATE SET` with `EXCLUDED` and regular SQL expressions.
* Improve exception message when un-`lift`-ed JSON expressions are used in the DSL. * Improve exception message when un-`lift`-ed JSON expressions are used in the DSL.

View file

@ -369,6 +369,20 @@ a `JOIN` clause.
;;=> ["INNER JOIN (tbl1 LEFT JOIN tbl2 USING (id))"] ;;=> ["INNER JOIN (tbl1 LEFT JOIN tbl2 USING (id))"]
``` ```
An alias can be provided:
```clojure
(sql/format {:join [[[:join [:tbl1 :t] {:left-join [:tbl2 [:using :id]]}]]]})
;;=> ["INNER JOIN (tbl1 AS t LEFT JOIN tbl2 USING (id))"]
```
To provide an expression, an extra level of `[...]` is needed:
```clojure
(sql/format {:join [[[:join [[:make_thing 42] :t] {:left-join [:tbl2 [:using :id]]}]]]})
;;=> ["INNER JOIN (MAKE_THING(?) AS t LEFT JOIN tbl2 USING (id))" 42]
```
## lateral ## lateral
Accepts a single argument that can be a (`SELECT`) clause or Accepts a single argument that can be a (`SELECT`) clause or

View file

@ -1839,7 +1839,7 @@
:interval format-interval :interval format-interval
:join :join
(fn [_ [e & js]] (fn [_ [e & js]]
(let [[sqls params] (reduce-sql (cons (format-expr e) (let [[sqls params] (reduce-sql (cons (format-selectable-dsl e {:as true})
(map format-dsl js)))] (map format-dsl js)))]
(into [(str "(" (str/join " " sqls) ")")] params))) (into [(str "(" (str/join " " sqls) ")")] params)))
:lateral :lateral

View file

@ -1,4 +1,4 @@
;; copyright (c) 2020-2022 sean corfield, all rights reserved ;; copyright (c) 2020-2024 sean corfield, all rights reserved
(ns honey.sql.helpers-test (ns honey.sql.helpers-test
(:refer-clojure :exclude [filter for group-by partition-by set update]) (:refer-clojure :exclude [filter for group-by partition-by set update])
@ -981,3 +981,14 @@
(sql/format (create-index [:unique :my-column-idx :if-not-exists] [:my-table :my-column])))) (sql/format (create-index [:unique :my-column-idx :if-not-exists] [:my-table :my-column]))))
(is (= ["CREATE INDEX my_column_idx ON my_table (LOWER(my_column))"] (is (= ["CREATE INDEX my_column_idx ON my_table (LOWER(my_column))"]
(sql/format (create-index :my-column-idx [:my-table :%lower.my-column])))))) (sql/format (create-index :my-column-idx [:my-table :%lower.my-column]))))))
(deftest join-with-alias
(is (= ["SELECT * FROM foo LEFT JOIN (populatons AS pm INNER JOIN customers AS pc ON (pm.id = pc.id) AND (pm.other_id = pc.other_id)) ON foo.fk_id = pm.id"]
(sql/format {:select :*
:from :foo
:left-join [[[:join [:populatons :pm]
{:join [[:customers :pc]
[:and
[:= :pm/id :pc/id]
[:= :pm/other-id :pc/other-id]]]}]]
[:= :foo/fk-id :pm/id]]}))))