:left-join & :right-join clauses; simplifies :join syntax

This commit is contained in:
Justin Kramer 2012-10-19 12:41:26 -04:00
parent bea39e3527
commit 76d6ccbcdb
5 changed files with 80 additions and 22 deletions

View file

@ -2,8 +2,6 @@
Turn Clojure data structures into SQL. Turn Clojure data structures into SQL.
**Work in progress**
## Leiningen Coordinate ## Leiningen Coordinate
```clj ```clj
@ -107,26 +105,60 @@ calls, raw SQL fragments, and named input parameters:
Here's a big, complicated query. Note that Honey SQL makes no attempt to verify that your queries make any sense. It merely renders surface syntax. Here's a big, complicated query. Note that Honey SQL makes no attempt to verify that your queries make any sense. It merely renders surface syntax.
```clj ```clj
(-> (select :f.* :b.baz :c.quux (sql/call :now) (sql/raw "@x := 10")) (-> (select :f.* :b.baz :c.quux [:b.bla "bla-bla"]
(sql/call :now) (sql/raw "@x := 10"))
(modifiers :distinct) (modifiers :distinct)
(from [:foo :f] [:baz :b]) (from [:foo :f] [:baz :b])
(join [[:clod :c] [:= :f.a :c.d] :left] (join :draq [:= :f.b :draq.x])
[:draq [:= :f.b :draq.x]]) (left-join [:clod :c] [:= :f.a :c.d])
(right-join :bock [:= :bock.z :c.e])
(where [:or (where [:or
[:and [:= :f.a "bort"] [:not= :b.baz "gabba"]] [:and [:= :f.a "bort"] [:not= :b.baz (sql/param :param1)]]
[:in :f.e [1 2 3]] [:< 1 2 3]
[:between :f.e 10 20]]) [:in :f.e [1 (sql/param :param2) 3]]
(group :f.a) ;note the name change [:between :f.e 10 20]])
(group :f.a)
(having [:< 0 :f.e]) (having [:< 0 :f.e])
(order-by [:b.baz :desc] :c.quux) (order-by [:b.baz :desc] :c.quux)
(limit 50) (limit 50)
(offset 10) (offset 10))
sql/format) => {:select [:f.* :b.baz :c.quux [:b.bla "bla-bla"]
=> ["SELECT DISTINCT f.*, b.baz, c.quux, NOW(), @x := 10 FROM foo AS f, baz AS b LEFT JOIN clod AS c ON f.a = c.d JOIN draq ON f.b = draq.x WHERE ((f.a = ? AND b.baz <> ?) OR (f.e IN (1, 2, 3)) OR f.e BETWEEN 10 AND 20) GROUP BY f.a HAVING 0 < f.e ORDER BY b.baz DESC, c.quux LIMIT 50 OFFSET 10" (sql/call :now) (sql/raw "@x := 10")]
"bort" "gabba"] :modifiers [:distinct]
:from [[:foo :f] [:baz :b]]
:join [:draq [:= :f.b :draq.x]]
:left-join [[:clod :c] [:= :f.a :c.d]]
:right-join [:bock [:= :bock.z :c.e]]
:where [:or
[:and [:= :f.a "bort"] [:not= :b.baz (sql/param :param1)]]
[:< 1 2 3]
[:in :f.e [1 (sql/param :param2) 3]]
[:between :f.e 10 20]]
:group-by [:f.a]
:having [:< 0 :f.e]
:order-by [[:b.baz :desc] :c.quux]
:limit 50
:offset 10}
(sql/format *1 {:param1 "gabba" :param2 2})
=> ["SELECT DISTINCT f.*, b.baz, c.quux, b.bla AS \"bla-bla\", NOW(), @x := 10
FROM foo AS f, baz AS b
INNER JOIN draq ON f.b = draq.x
LEFT JOIN clod AS c ON f.a = c.d
RIGHT JOIN bock ON bock.z = c.e
WHERE ((f.a = ? AND b.baz <> ?)
OR (1 < 2 AND 2 < 3)
OR (f.e IN (1, ?, 3))
OR f.e BETWEEN 10 AND 20)
GROUP BY f.a
HAVING 0 < f.e
ORDER BY b.baz DESC, c.quux
LIMIT 50
OFFSET 10 "
"bort" "gabba" 2]
;; Printable and readable ;; Printable and readable
(= *1 (read-string (pr-str *1))) (= *2 (read-string (pr-str *2)))
=> true => true
``` ```

View file

@ -34,6 +34,8 @@
:select, :merge-select, :un-select :select, :merge-select, :un-select
:from, :merge-from :from, :merge-from
:join, :merge-join :join, :merge-join
:left-join, :merge-left-join
:right-join, :merge-right-join
:where, :merge-where :where, :merge-where
:group-by, :merge-group-by :group-by, :merge-group-by
:having, :merge-having :having, :merge-having

View file

@ -117,7 +117,8 @@
(def clause-order (def clause-order
"Determines the order that clauses will be placed within generated SQL" "Determines the order that clauses will be placed within generated SQL"
[:select :from :join :where :group-by :having :order-by :limit :offset]) [:select :from :join :left-join :right-join :where :group-by :having
:order-by :limit :offset])
(def known-clauses (set clause-order)) (def known-clauses (set clause-order))
@ -242,14 +243,23 @@
(defmethod format-clause :where [[_ pred] _] (defmethod format-clause :where [[_ pred] _]
(str "WHERE " (format-predicate* pred))) (str "WHERE " (format-predicate* pred)))
(defn format-join [table pred & [type]] (defn format-join [type table pred]
(str (when type (str (when type
(str (string/upper-case (name type)) " ")) (str (string/upper-case (name type)) " "))
"JOIN " (to-sql table) "JOIN " (to-sql table)
" ON " (format-predicate* pred))) " ON " (format-predicate* pred)))
(defmethod format-clause :join [[_ join-groups] _] (defmethod format-clause :join [[_ join-groups] _]
(space-join (map #(apply format-join %) join-groups))) (space-join (map #(apply format-join :inner %)
(partition 2 join-groups))))
(defmethod format-clause :left-join [[_ join-groups] _]
(space-join (map #(apply format-join :left %)
(partition 2 join-groups))))
(defmethod format-clause :right-join [[_ join-groups] _]
(space-join (map #(apply format-join :right %)
(partition 2 join-groups))))
(defmethod format-clause :group-by [[_ fields] _] (defmethod format-clause :group-by [[_ fields] _]
(str "GROUP BY " (comma-join (map to-sql fields)))) (str "GROUP BY " (comma-join (map to-sql fields))))

View file

@ -78,6 +78,18 @@
(defhelper merge-join [m clauses] (defhelper merge-join [m clauses]
(update-in m [:join] concat clauses)) (update-in m [:join] concat clauses))
(defhelper left-join [m clauses]
(assoc m :left-join clauses))
(defhelper merge-left-join [m clauses]
(update-in m [:left-join] concat clauses))
(defhelper right-join [m clauses]
(assoc m :right-join clauses))
(defhelper merge-right-join [m clauses]
(update-in m [:right-join] concat clauses))
(defmethod build-clause :group-by [_ m fields] (defmethod build-clause :group-by [_ m fields]
(assoc m :group-by (collify fields))) (assoc m :group-by (collify fields)))

View file

@ -12,8 +12,9 @@
;;(un-select :c.quux) ;;(un-select :c.quux)
(modifiers :distinct) (modifiers :distinct)
(from [:foo :f] [:baz :b]) (from [:foo :f] [:baz :b])
(join [[:clod :c] [:= :f.a :c.d] :left] (join :draq [:= :f.b :draq.x])
[:draq [:= :f.b :draq.x]]) (left-join [:clod :c] [:= :f.a :c.d])
(right-join :bock [:= :bock.z :c.e])
(where [:or (where [:or
[:and [:= :f.a "bort"] [:not= :b.baz (sql/param :param1)]] [:and [:= :f.a "bort"] [:not= :b.baz (sql/param :param1)]]
[:< 1 2 3] [:< 1 2 3]
@ -30,8 +31,9 @@
;;:un-select :c.quux ;;:un-select :c.quux
:modifiers :distinct :modifiers :distinct
:from [[:foo :f] [:baz :b]] :from [[:foo :f] [:baz :b]]
:join [[[:clod :c] [:= :f.a :c.d] :left] :join [:draq [:= :f.b :draq.x]]
[:draq [:= :f.b :draq.x]]] :left-join [[:clod :c] [:= :f.a :c.d]]
:right-join [:bock [:= :bock.z :c.e]]
:where [:or :where [:or
[:and [:= :f.a "bort"] [:not= :b.baz (sql/param :param1)]] [:and [:= :f.a "bort"] [:not= :b.baz (sql/param :param1)]]
[:< 1 2 3] [:< 1 2 3]
@ -49,7 +51,7 @@
(is (= m1 m3))) (is (= m1 m3)))
(testing "SQL data formats correctly" (testing "SQL data formats correctly"
(is (= (sql/format m1 {:param1 "gabba" :param2 2}) (is (= (sql/format m1 {:param1 "gabba" :param2 2})
["SELECT DISTINCT f.*, b.baz, c.quux, b.bla AS \"bla-bla\", NOW(), @x := 10 FROM foo AS f, baz AS b LEFT JOIN clod AS c ON f.a = c.d JOIN draq ON f.b = draq.x WHERE ((f.a = ? AND b.baz <> ?) OR (1 < 2 AND 2 < 3) OR (f.e IN (1, ?, 3)) OR f.e BETWEEN 10 AND 20) GROUP BY f.a HAVING 0 < f.e ORDER BY b.baz DESC, c.quux LIMIT 50 OFFSET 10 " ["SELECT DISTINCT f.*, b.baz, c.quux, b.bla AS \"bla-bla\", NOW(), @x := 10 FROM foo AS f, baz AS b INNER JOIN draq ON f.b = draq.x LEFT JOIN clod AS c ON f.a = c.d RIGHT JOIN bock ON bock.z = c.e WHERE ((f.a = ? AND b.baz <> ?) OR (1 < 2 AND 2 < 3) OR (f.e IN (1, ?, 3)) OR f.e BETWEEN 10 AND 20) GROUP BY f.a HAVING 0 < f.e ORDER BY b.baz DESC, c.quux LIMIT 50 OFFSET 10 "
"bort" "gabba" 2]))) "bort" "gabba" 2])))
(testing "SQL data prints and reads correctly" (testing "SQL data prints and reads correctly"
(is (= m1 (read-string (pr-str m1))))))) (is (= m1 (read-string (pr-str m1)))))))