adds support for JOIN ... USING (...) syntax (#188)

This commit is contained in:
Vincent 2018-01-16 03:13:14 +01:00
parent faba02f2b4
commit 8a6ecd94aa
2 changed files with 15 additions and 4 deletions

View file

@ -470,10 +470,13 @@
(str "WHERE " (format-predicate* pred)))
(defn format-join [type table pred]
(cond-> (str (when type
(str (string/upper-case (name type)) " "))
"JOIN " (to-sql table))
pred (str " ON " (format-predicate* pred))))
(str (when type
(str (string/upper-case (name type)) " "))
"JOIN " (to-sql table)
(when pred
(if (= :using (first pred))
(str " USING (" (->> pred rest (map quote-identifier) comma-join) ")")
(str " ON " (format-predicate* pred))))))
(defmethod format-clause :join [[_ join-groups] _]
(space-join (map #(apply format-join :inner %)

View file

@ -192,6 +192,14 @@
(join :x [:= :foo.id :x.id] :y nil)
sql/format)))))
(deftest join-using-test
(testing "nil join"
(is (= ["SELECT * FROM foo INNER JOIN x USING (id) INNER JOIN y USING (foo, bar)"]
(-> (select :*)
(from :foo)
(join :x [:using :id] :y [:using :foo :bar])
sql/format)))))
(deftest inline-test
(is (= ["SELECT * FROM foo WHERE id = 5"]
(-> (select :*)