From 28e44b7972031ba36921ca9778bbd478446ea2b7 Mon Sep 17 00:00:00 2001 From: Buck Ryan Date: Sun, 7 Apr 2019 16:13:35 -0400 Subject: [PATCH 1/2] Add support for DISTINCT ON Resolves #31 This uses the new multimethod added to honeysql in https://github.com/jkk/honeysql/commit/509df1970247954d11baf52ff3897c9440acb394 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. --- deps.edn | 2 +- src/honeysql_postgres/format.cljc | 5 ++++- test/honeysql_postgres/postgres_test.cljc | 11 ++++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/deps.edn b/deps.edn index e6dbf82..3c2786e 100644 --- a/deps.edn +++ b/deps.edn @@ -1 +1 @@ -{:deps {honeysql {:mvn/version "0.9.4" :exclusions [org.clojure/clojurescript]}}} +{:deps {honeysql {:git/url "https://github.com/jkk/honeysql" :sha "be203e9086808c47ab7e42520950db0bfd9efcc1" :exclusions [org.clojure/clojurescript]}}} diff --git a/src/honeysql_postgres/format.cljc b/src/honeysql_postgres/format.cljc index 3a278fb..855136c 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])) (def ^:private custom-additions @@ -209,3 +209,6 @@ (str "INSERT INTO " (sqlf/to-sql table-name) " AS " (sqlf/to-sql table-alias))) (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 321a79b..9331cef 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]])) @@ -219,3 +220,11 @@ (from :products) (where [:not-ilike :name "%name%"]) sql/format))))) + +(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)))))) From d005fa2b0e2307fb56700cbc7d9384b0e06237dd Mon Sep 17 00:00:00 2001 From: Buck Ryan Date: Mon, 8 Apr 2019 17:18:06 -0400 Subject: [PATCH 2/2] namespace for myself --- project.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project.clj b/project.clj index e96ce32..c6c9cb1 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject nilenso/honeysql-postgres "0.2.5" +(defproject b-ryan/honeysql-postgres "0.2.6" :description "PostgreSQL extension for honeysql" :url "https://github.com/nilenso/honeysql-postgres" :license {:name "Eclipse Public License"