From 21b4cea2c29196e445826592f138edcdbc45110d Mon Sep 17 00:00:00 2001 From: Unnikrishnan Date: Mon, 4 Apr 2016 18:34:18 +0530 Subject: [PATCH 1/2] Add fix for select distinct on --- src/honeysql/format.clj | 10 +++++++++- test/honeysql/core_test.clj | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index 79efdc3..34b15ef 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -17,6 +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 +(defn construct-select [s] + (if (re-find #"DISTINCT ON" (first s)) + (str (first s) " " (comma-join (rest s))) + (comma-join s))) + (def ^:dynamic *clause* "During formatting, *clause* is bound to :select, :from, :where, etc." nil) @@ -405,7 +413,7 @@ (str (space-join (map (comp string/upper-case name) (:modifiers sql-map))) " ")) - (comma-join (map to-sql fields)))) + (construct-select (map to-sql fields)))) (defmethod format-clause :from [[_ tables] _] (str "FROM " (comma-join (map to-sql tables)))) diff --git a/test/honeysql/core_test.clj b/test/honeysql/core_test.clj index a59f59f..feb1813 100644 --- a/test/honeysql/core_test.clj +++ b/test/honeysql/core_test.clj @@ -173,3 +173,12 @@ (from :foo) (join :x [:= :foo.id :x.id] :y nil) sql/format))))) + +(deftest distinct-on + (testing "Distinct on" + (is (= ["SELECT DISTINCT ON (name) name, created_by"] + (-> (select (sql/call :distinct-on :name) :name :created_by) + sql/format))) + (is (= ["SELECT DISTINCT ON (name, created_by) name, created_by"] + (-> (select (sql/call :distinct-on :name :created_by) :name :created_by) + sql/format))))) From 36d13d32dc489782cfda28e19b7d2f1eaf2ece07 Mon Sep 17 00:00:00 2001 From: Unnikrishnan Date: Tue, 5 Apr 2016 09:26:27 +0530 Subject: [PATCH 2/2] minor refactoring --- src/honeysql/format.clj | 13 +++++++------ test/honeysql/core_test.clj | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) 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)))