From bb73dcbda7292457b7ea3a3a13869b68da2ac91e Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Sat, 7 Sep 2019 15:56:06 -0700 Subject: [PATCH] Fixes #162 by adding composite/:composite Relies on paren-wrapping within subqueries which is the context for values. --- CHANGES.md | 1 + README.md | 15 +++++++++++++++ src/honeysql/format.cljc | 4 ++++ src/honeysql/helpers.cljc | 6 ++++++ 4 files changed, 26 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 974ebf6..80df94a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,6 +3,7 @@ * Fix #248 by treating alias as "not a subquery" when generating SQL for it. (@seancorfield) * Fix #247 by reverting #132 / #131 so the default behavior is friendlier for namespace-qualified keywords used for table and column names, but adds `honeysql.format/*allow-namespaced-names?*` to restore the previous behavior. A `:allow-namespaced-names?` option has been adding to `format` to set this more easily. * Fix #235 by adding `set0` (`:set0`) and `set1` (`:set1`) variants of `sset` (`:set`) to support different placements of `SET` (before `FROM` or after `JOIN` respectively) that different databases require. See also #200. (@seancorfield) +* Fix #162 by adding `composite`/`:composite` constructor for values. (@seancorfield) * Fix #139 by checking arguments to `columns`/`merge-columns` and throwing an exception if a single collection is supplied (instead of varargs). (@seancorfield) * Fix #128 by adding `truncate` support. (@seancorfield) * Fix #99 by adding a note to the first use of `select` in the README that column names can be keywords or symbols but not strings. (@seancorfield) diff --git a/README.md b/README.md index dfe651e..c5a982c 100644 --- a/README.md +++ b/README.md @@ -186,6 +186,21 @@ The column values do not have to be literals, they can be nested queries: "user"] ``` +Composite types are supported: + +```clojure +(-> (insert-into :comp_table) + (columns :name :comp_column) + (values + [["small" (composite 1 "inch")] + ["large" (composite 10 "feet")]]) + sql/format) +=> [#sql/regularize + "INSERT INTO comp_table (name, comp_column) + VALUES (?, (?, ?)), (?, (?, ?))" + "small" 1 "inch" "large" 10 "feet"] +``` + Updates are possible too (note the double S in `sset` to avoid clashing with `clojure.core/set`): diff --git a/src/honeysql/format.cljc b/src/honeysql/format.cljc index 9880068..6554245 100644 --- a/src/honeysql/format.cljc +++ b/src/honeysql/format.cljc @@ -226,6 +226,7 @@ :delete-from 80 :truncate 85 :columns 90 + :composite 95 :set0 100 ; low-priority set clause :from 110 :join 120 @@ -612,6 +613,9 @@ (defmethod format-clause :columns [[_ fields] _] (str "(" (comma-join (map to-sql fields)) ")")) +(defmethod format-clause :composite [[_ fields] _] + (comma-join (map to-sql fields))) + (defmethod format-clause :values [[_ values] _] (if (sequential? (first values)) (str "VALUES " (comma-join (for [x values] diff --git a/src/honeysql/helpers.cljc b/src/honeysql/helpers.cljc index 1f0b206..d6757cd 100644 --- a/src/honeysql/helpers.cljc +++ b/src/honeysql/helpers.cljc @@ -250,6 +250,12 @@ (check-varargs :merge-columns fields) (build-clause :merge-columns m fields))) +(macros/usetime + (defhelper composite [m ms] + (if (nil? ms) + m + (assoc m :composite (collify ms))))) + (defmethod build-clause :values [_ m vs] (assoc m :values vs))