From 1ff93eb964fdae59068036d772c77a4c714ee256 Mon Sep 17 00:00:00 2001 From: "Andrii V. Mishkovskyi" Date: Wed, 15 Apr 2015 17:01:05 +0200 Subject: [PATCH 1/6] Adding stubs for future support of NULLS FIRST/LAST --- src/honeysql/format.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index b42b020..aec3d0c 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -412,7 +412,7 @@ (str "ORDER BY " (comma-join (for [field fields] (if (sequential? field) - (let [[field order] field] + (let [[field order & [nulls-order]] field] (str (to-sql field) " " (if (= :desc order) "DESC" "ASC"))) (to-sql field)))))) From 76303c2ffeb4ecb43e1716699dd42a6af0b5abf5 Mon Sep 17 00:00:00 2001 From: "Andrii V. Mishkovskyi" Date: Thu, 16 Apr 2015 20:21:21 +0200 Subject: [PATCH 2/6] Adding example of what I'm trying to achieve in the README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 758e66d..e9b429b 100644 --- a/README.md +++ b/README.md @@ -249,7 +249,7 @@ Here's a big, complicated query. Note that Honey SQL makes no attempt to verify [:between :f.e 10 20]] :group-by [:f.a] :having [:< 0 :f.e] - :order-by [[:b.baz :desc] :c.quux] + :order-by [[:b.baz :desc] :c.quux [:f.a :nulls-first] :limit 50 :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) GROUP BY f.a HAVING 0 < f.e - ORDER BY b.baz DESC, c.quux + ORDER BY b.baz DESC, c.quux, f.a NULLS FIRST LIMIT 50 OFFSET 10 " "bort" "gabba" 2] From 11d4ff37ec93da6a15835f7466bca8384771faf4 Mon Sep 17 00:00:00 2001 From: "Andrii V. Mishkovskyi" Date: Thu, 16 Apr 2015 21:23:19 +0200 Subject: [PATCH 3/6] Adding a comment for future reference --- src/honeysql/format.clj | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index aec3d0c..0f7d779 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -413,6 +413,12 @@ (comma-join (for [field fields] (if (sequential? field) (let [[field order & [nulls-order]] field] + ;; Correct way of handling this would be to + ;; expect a dictionary with either order, + ;; nulls order or both at the same + ;; time. However, so far I'm not sure how to + ;; achieve that, so first iteration will have + ;; to use dirty hacks. (str (to-sql field) " " (if (= :desc order) "DESC" "ASC"))) (to-sql field)))))) From 04f0447c9285d8b66b0dfca87445ff4fbc80bbec Mon Sep 17 00:00:00 2001 From: "Andrii V. Mishkovskyi" Date: Fri, 17 Apr 2015 12:09:50 +0200 Subject: [PATCH 4/6] Corrected README example --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e9b429b..a184be2 100644 --- a/README.md +++ b/README.md @@ -232,7 +232,7 @@ Here's a big, complicated query. Note that Honey SQL makes no attempt to verify [:between :f.e 10 20]]) (group :f.a) (having [:< 0 :f.e]) - (order-by [:b.baz :desc] :c.quux) + (order-by [:b.baz :desc] :c.quux [:f.a :nulls-first]) (limit 50) (offset 10)) => {:select [:f.* :b.baz :c.quux [:b.bla "bla-bla"] From 7160b4b1ad06a9a0cdd379e67203d233683540a9 Mon Sep 17 00:00:00 2001 From: "Andrii V. Mishkovskyi" Date: Fri, 17 Apr 2015 12:29:20 +0200 Subject: [PATCH 5/6] Adding tests for expected behavior --- test/honeysql/core_test.clj | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/honeysql/core_test.clj b/test/honeysql/core_test.clj index d7c5543..b5b4b2a 100644 --- a/test/honeysql/core_test.clj +++ b/test/honeysql/core_test.clj @@ -24,7 +24,7 @@ ;;(merge-where [:not= nil :b.bla]) (group :f.a) (having [:< 0 :f.e]) - (order-by [:b.baz :desc] :c.quux) + (order-by [:b.baz :desc] :c.quux [:f.a :nulls-first]) (limit 50) (offset 10)) m2 {:select [:f.* :b.baz :c.quux [:b.bla :bla-bla] @@ -44,7 +44,7 @@ ;;:merge-where [:not= nil :b.bla] :group-by :f.a :having [:< 0 :f.e] - :order-by [[:b.baz :desc] :c.quux] + :order-by [[:b.baz :desc] :c.quux [:f.a :nulls-first]] :limit 50 :offset 10} m3 (sql/build m2) @@ -52,14 +52,14 @@ (testing "Various construction methods are consistent" (is (= m1 m3 m4))) (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] (sql/format m1 {:param1 "gabba" :param2 2})))) (testing "SQL data prints and reads correctly" (is (= m1 (read-string (pr-str m1))))) (testing "SQL data formats correctly with alternate param naming" (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]))))) (deftest test-cast From 500b55775e0e43d11938733c9939808732890b43 Mon Sep 17 00:00:00 2001 From: "Andrii V. Mishkovskyi" Date: Fri, 17 Apr 2015 12:29:44 +0200 Subject: [PATCH 6/6] Handle :nulls-first and :nulls-last in order-by This commit implements NULLS (FIRST | LAST) in ORDER BY clause, as introduced by SQL:2003, link to grammar specification: http://savage.net.au/SQL/sql-2003-2.bnf.html#sort%20specification%20list Only PostgreSQL 8.4+ and Oracle 10+ support said feature as of currently. --- src/honeysql/format.clj | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index 0f7d779..18959e4 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -412,15 +412,16 @@ (str "ORDER BY " (comma-join (for [field fields] (if (sequential? field) - (let [[field order & [nulls-order]] field] - ;; Correct way of handling this would be to - ;; expect a dictionary with either order, - ;; nulls order or both at the same - ;; time. However, so far I'm not sure how to - ;; achieve that, so first iteration will have - ;; to use dirty hacks. - (str (to-sql field) " " (if (= :desc order) - "DESC" "ASC"))) + (let [[field & modifiers] field] + (string/join " " + (cons (to-sql field) + (for [modifier modifiers] + (case modifier + :desc "DESC" + :asc "ASC" + :nulls-first "NULLS FIRST" + :nulls-last "NULLS LAST" + ""))))) (to-sql field)))))) (defmethod format-clause :limit [[_ limit] _]