Merge pull request #69 from mishok13/nulls-first-last-in-order-by

NULL order specification support
This commit is contained in:
Michael Blume 2015-04-17 14:20:23 -07:00
commit 198115fb84
3 changed files with 17 additions and 10 deletions

View file

@ -232,7 +232,7 @@ Here's a big, complicated query. Note that Honey SQL makes no attempt to verify
[:between :f.e 10 20]]) [:between :f.e 10 20]])
(group :f.a) (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 [:f.a :nulls-first])
(limit 50) (limit 50)
(offset 10)) (offset 10))
=> {:select [:f.* :b.baz :c.quux [:b.bla "bla-bla"] => {:select [:f.* :b.baz :c.quux [:b.bla "bla-bla"]
@ -249,7 +249,7 @@ Here's a big, complicated query. Note that Honey SQL makes no attempt to verify
[:between :f.e 10 20]] [:between :f.e 10 20]]
:group-by [:f.a] :group-by [:f.a]
:having [:< 0 :f.e] :having [:< 0 :f.e]
:order-by [[:b.baz :desc] :c.quux] :order-by [[:b.baz :desc] :c.quux [:f.a :nulls-first]
:limit 50 :limit 50
:offset 10} :offset 10}
@ -265,7 +265,7 @@ Here's a big, complicated query. Note that Honey SQL makes no attempt to verify
OR f.e BETWEEN 10 AND 20) OR f.e BETWEEN 10 AND 20)
GROUP BY f.a GROUP BY f.a
HAVING 0 < f.e HAVING 0 < f.e
ORDER BY b.baz DESC, c.quux ORDER BY b.baz DESC, c.quux, f.a NULLS FIRST
LIMIT 50 LIMIT 50
OFFSET 10 " OFFSET 10 "
"bort" "gabba" 2] "bort" "gabba" 2]

View file

@ -415,9 +415,16 @@
(str "ORDER BY " (str "ORDER BY "
(comma-join (for [field fields] (comma-join (for [field fields]
(if (sequential? field) (if (sequential? field)
(let [[field order] field] (let [[field & modifiers] field]
(str (to-sql field) " " (if (= :desc order) (string/join " "
"DESC" "ASC"))) (cons (to-sql field)
(for [modifier modifiers]
(case modifier
:desc "DESC"
:asc "ASC"
:nulls-first "NULLS FIRST"
:nulls-last "NULLS LAST"
"")))))
(to-sql field)))))) (to-sql field))))))
(defmethod format-clause :limit [[_ limit] _] (defmethod format-clause :limit [[_ limit] _]

View file

@ -24,7 +24,7 @@
;;(merge-where [:not= nil :b.bla]) ;;(merge-where [:not= nil :b.bla])
(group :f.a) (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 [:f.a :nulls-first])
(limit 50) (limit 50)
(offset 10)) (offset 10))
m2 {:select [:f.* :b.baz :c.quux [:b.bla :bla-bla] m2 {:select [:f.* :b.baz :c.quux [:b.bla :bla-bla]
@ -44,7 +44,7 @@
;;:merge-where [:not= nil :b.bla] ;;:merge-where [:not= nil :b.bla]
:group-by :f.a :group-by :f.a
:having [:< 0 :f.e] :having [:< 0 :f.e]
:order-by [[:b.baz :desc] :c.quux] :order-by [[:b.baz :desc] :c.quux [:f.a :nulls-first]]
:limit 50 :limit 50
:offset 10} :offset 10}
m3 (sql/build m2) m3 (sql/build m2)
@ -52,14 +52,14 @@
(testing "Various construction methods are consistent" (testing "Various construction methods are consistent"
(is (= m1 m3 m4))) (is (= m1 m3 m4)))
(testing "SQL data formats correctly" (testing "SQL data formats correctly"
(is (= ["SELECT DISTINCT f.*, b.baz, c.quux, b.bla AS bla_bla, now(), @x := 10 FROM foo f, baz b INNER JOIN draq ON f.b = draq.x LEFT JOIN clod c ON f.a = c.d RIGHT JOIN bock ON bock.z = c.e FULL JOIN beck ON beck.x = c.y 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 " (is (= ["SELECT DISTINCT f.*, b.baz, c.quux, b.bla AS bla_bla, now(), @x := 10 FROM foo f, baz b INNER JOIN draq ON f.b = draq.x LEFT JOIN clod c ON f.a = c.d RIGHT JOIN bock ON bock.z = c.e FULL JOIN beck ON beck.x = c.y 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, f.a NULLS FIRST LIMIT 50 OFFSET 10 "
"bort" "gabba" 2] "bort" "gabba" 2]
(sql/format m1 {:param1 "gabba" :param2 2})))) (sql/format m1 {:param1 "gabba" :param2 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)))))
(testing "SQL data formats correctly with alternate param naming" (testing "SQL data formats correctly with alternate param naming"
(is (= (sql/format m1 :params {:param1 "gabba" :param2 2} :parameterizer :postgresql) (is (= (sql/format m1 :params {:param1 "gabba" :param2 2} :parameterizer :postgresql)
["SELECT DISTINCT f.*, b.baz, c.quux, b.bla AS bla_bla, now(), @x := 10 FROM foo f, baz b INNER JOIN draq ON f.b = draq.x LEFT JOIN clod c ON f.a = c.d RIGHT JOIN bock ON bock.z = c.e FULL JOIN beck ON beck.x = c.y WHERE ((f.a = $1 AND b.baz <> $2) OR (1 < 2 AND 2 < 3) OR (f.e in (1, $3, 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 f, baz b INNER JOIN draq ON f.b = draq.x LEFT JOIN clod c ON f.a = c.d RIGHT JOIN bock ON bock.z = c.e FULL JOIN beck ON beck.x = c.y WHERE ((f.a = $1 AND b.baz <> $2) OR (1 < 2 AND 2 < 3) OR (f.e in (1, $3, 3)) OR f.e BETWEEN 10 AND 20) GROUP BY f.a HAVING 0 < f.e ORDER BY b.baz DESC, c.quux, f.a NULLS FIRST LIMIT 50 OFFSET 10 "
"bort" "gabba" 2]))))) "bort" "gabba" 2])))))
(deftest test-cast (deftest test-cast