From a51cfe5a2ea10464961d26433285750c97598d34 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Sat, 12 Jun 2021 18:15:59 -0700 Subject: [PATCH] Fixes #328 by adding :distinct special syntax --- CHANGELOG.md | 1 + doc/special-syntax.md | 9 +++++++++ src/honey/sql.cljc | 4 ++++ 3 files changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68c9a7d..0df5758 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changes * 2.0.next in progress + * Fix #328 by adding `:distinct` as special syntax, affecting an expression. * Support PostgreSQL's `&&` array operator. * Clarify how to `SELECT` a function expression (in **Getting Started**). diff --git a/doc/special-syntax.md b/doc/special-syntax.md index 04ab495..97b2681 100644 --- a/doc/special-syntax.md +++ b/doc/special-syntax.md @@ -60,6 +60,15 @@ expression (comma-separated, wrapped in parentheses): ;;=> ["(a, b, ?, x + ?)" "red" 1] ``` +## distinct + +Accepts a single expression and prefixes it with `DISTINCT `: + +```clojure +(sql/format {:select [ [[:count [:distinct :status]] :n] ] :from :table}) +;;=> ["SELECT COUNT(DISTINCT status) AS n FROM table"] +``` + ## entity Accepts a single keyword or symbol argument and produces a diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 73fb85c..3684085 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -1074,6 +1074,10 @@ (fn [_ [& args]] (let [[sqls params] (format-expr-list args)] (into [(str "(" (str/join ", " sqls) ")")] params))) + :distinct + (fn [_ [x]] + (let [[sql & params] (format-expr x {:nested true})] + (into [(str "DISTINCT " sql)] params))) :escape (fn [_ [pattern escape-chars]] (let [[sql-p & params-p] (format-expr pattern)