Skip to content

Commit 28e44b7

Browse files
committed
Add support for DISTINCT ON
Resolves #31 This uses the new multimethod added to honeysql in seancorfield/honeysql@509df19 to add DISTINCT ON support. Tests are included which cover the behavior. This commit updates deps.edn to point to the github repo for honeysql which will obviously have to be changed once honeysql is released.
1 parent f49efa4 commit 28e44b7

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

deps.edn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{:deps {honeysql {:mvn/version "0.9.4" :exclusions [org.clojure/clojurescript]}}}
1+
{:deps {honeysql {:git/url "https://github.com/jkk/honeysql" :sha "be203e9086808c47ab7e42520950db0bfd9efcc1" :exclusions [org.clojure/clojurescript]}}}

src/honeysql_postgres/format.cljc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(ns ^{:doc "Extension of the honeysql format functions specifically for postgreSQL"}
22
honeysql-postgres.format
3-
(:require [honeysql.format :as sqlf :refer [fn-handler format-clause]] ;; multi-methods
3+
(:require [honeysql.format :as sqlf :refer [fn-handler format-clause format-modifiers]] ;; multi-methods
44
[honeysql-postgres.util :as util]))
55

66
(def ^:private custom-additions
@@ -209,3 +209,6 @@
209209
(str "INSERT INTO " (sqlf/to-sql table-name) " AS " (sqlf/to-sql table-alias)))
210210

211211
(override-default-clause-priority)
212+
213+
(defmethod format-modifiers :distinct-on [[_ & fields]]
214+
(str "DISTINCT ON(" (sqlf/comma-join (map sqlf/to-sql fields)) ")"))

test/honeysql_postgres/postgres_test.cljc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
create-table rename-table drop-table
99
window create-view over with-columns]]
1010
[honeysql.helpers :as sqlh :refer [insert-into values where select columns
11-
from order-by update sset query-values]]
11+
from order-by update sset query-values
12+
modifiers]]
1213
[honeysql.core :as sql]
1314
[clojure.test :as test :refer [deftest is testing]]))
1415

@@ -219,3 +220,11 @@
219220
(from :products)
220221
(where [:not-ilike :name "%name%"])
221222
sql/format)))))
223+
224+
(deftest select-distinct-on
225+
(testing "select distinct on"
226+
(is (= ["SELECT DISTINCT ON(\"a\", \"b\") \"c\" FROM \"products\" "]
227+
(-> (select :c)
228+
(from :products)
229+
(modifiers :distinct-on :a :b)
230+
(sql/format :quoting :ansi))))))

0 commit comments

Comments
 (0)