diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index 34b15ef..3c52787 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -17,13 +17,14 @@ (defn paren-wrap [x] (str "(" x ")")) -;; Checks if the first is a `DISTINCT ON` statement, if yes then return a string -;; with an appended space at the end of the first and then comma joining the rest -;; else simply return a comma joined string +;; Checks if the first elemnet of the list is a `DISTINCT ON` statement, if yes +;; then return a string with an appended space at the end of the first and then +;; comma joining the rest else simply return a comma joined string (defn construct-select [s] - (if (re-find #"DISTINCT ON" (first s)) - (str (first s) " " (comma-join (rest s))) - (comma-join s))) + (let [[fst & rst] s] + (if (re-find #"DISTINCT ON" fst) + (str fst " " (comma-join rst)) + (comma-join s)))) (def ^:dynamic *clause* "During formatting, *clause* is bound to :select, :from, :where, etc." diff --git a/test/honeysql/core_test.clj b/test/honeysql/core_test.clj index feb1813..14d1d14 100644 --- a/test/honeysql/core_test.clj +++ b/test/honeysql/core_test.clj @@ -175,7 +175,7 @@ sql/format))))) (deftest distinct-on - (testing "Distinct on" + (testing "Distinct on query generation" (is (= ["SELECT DISTINCT ON (name) name, created_by"] (-> (select (sql/call :distinct-on :name) :name :created_by) sql/format)))