diff --git a/deps.edn b/deps.edn index e6dbf82..a9e0b0b 100644 --- a/deps.edn +++ b/deps.edn @@ -1 +1 @@ -{:deps {honeysql {:mvn/version "0.9.4" :exclusions [org.clojure/clojurescript]}}} +{:deps {honeysql {:mvn/version "0.9.5" :exclusions [org.clojure/clojurescript]}}} diff --git a/src/honeysql_postgres/format.cljc b/src/honeysql_postgres/format.cljc index 5949bac..3bc73fe 100644 --- a/src/honeysql_postgres/format.cljc +++ b/src/honeysql_postgres/format.cljc @@ -1,6 +1,6 @@ (ns ^{:doc "Extension of the honeysql format functions specifically for postgreSQL"} honeysql-postgres.format - (:require [honeysql.format :as sqlf :refer [fn-handler format-clause]] ;; multi-methods + (:require [honeysql.format :as sqlf :refer [fn-handler format-clause format-modifiers]] ;; multi-methods [honeysql-postgres.util :as util] [clojure.string :as string])) @@ -220,3 +220,6 @@ (string/join " EXCEPT ALL " (map sqlf/to-sql maps)))) (override-default-clause-priority) + +(defmethod format-modifiers :distinct-on [[_ & fields]] + (str "DISTINCT ON(" (sqlf/comma-join (map sqlf/to-sql fields)) ")")) diff --git a/test/honeysql_postgres/postgres_test.cljc b/test/honeysql_postgres/postgres_test.cljc index 7f201d3..7c389a7 100644 --- a/test/honeysql_postgres/postgres_test.cljc +++ b/test/honeysql_postgres/postgres_test.cljc @@ -8,7 +8,8 @@ create-table rename-table drop-table window create-view over with-columns]] [honeysql.helpers :as sqlh :refer [insert-into values where select columns - from order-by update sset query-values]] + from order-by update sset query-values + modifiers]] [honeysql.core :as sql] [clojure.test :as test :refer [deftest is testing]])) @@ -254,3 +255,11 @@ {:except-all [{:select [:ip]} {:select [:ip] :from [:ip_location]}]}))))) + +(deftest select-distinct-on + (testing "select distinct on" + (is (= ["SELECT DISTINCT ON(\"a\", \"b\") \"c\" FROM \"products\" "] + (-> (select :c) + (from :products) + (modifiers :distinct-on :a :b) + (sql/format :quoting :ansi))))))