Skip to content

Commit feb22bf

Browse files
authored
Merge pull request #33 from b-ryan/distinct-on
Add support for DISTINCT ON
2 parents 29e6012 + 25e508b commit feb22bf

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 {:mvn/version "0.9.5" :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
[clojure.string :as string]))
66

@@ -220,3 +220,6 @@
220220
(string/join " EXCEPT ALL " (map sqlf/to-sql maps))))
221221

222222
(override-default-clause-priority)
223+
224+
(defmethod format-modifiers :distinct-on [[_ & fields]]
225+
(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

@@ -254,3 +255,11 @@
254255
{:except-all
255256
[{:select [:ip]}
256257
{:select [:ip] :from [:ip_location]}]})))))
258+
259+
(deftest select-distinct-on
260+
(testing "select distinct on"
261+
(is (= ["SELECT DISTINCT ON(\"a\", \"b\") \"c\" FROM \"products\" "]
262+
(-> (select :c)
263+
(from :products)
264+
(modifiers :distinct-on :a :b)
265+
(sql/format :quoting :ansi))))))

0 commit comments

Comments
 (0)