From 500b55775e0e43d11938733c9939808732890b43 Mon Sep 17 00:00:00 2001 From: "Andrii V. Mishkovskyi" Date: Fri, 17 Apr 2015 12:29:44 +0200 Subject: [PATCH] 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] _]