From ad1b0f98807836c3dc9bf907d572c38ebaa29aa5 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Sat, 26 Aug 2023 16:45:39 -0700 Subject: [PATCH] address #496 by adding :overriding-value option --- CHANGELOG.md | 1 + src/honey/sql.cljc | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d82256a..c26edaf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Add `:select` with function call and alias example to README (PR [#502](https://github.com/seancorfield/honeysql/pull/502) [@markbastian](https://github.com/markbastian)). * Address [#501](https://github.com/seancorfield/honeysql/issues/501) by making `INSERT INTO` (and `REPLACE INTO`) use the `:columns` or `:values` clauses to produce column names (which are then omitted from those other clauses). * Address [#497](https://github.com/seancorfield/honeysql/issues/497) by adding `:alias` special syntax. + * Address [#496](https://github.com/seancorfield/honeysql/issues/496) by adding `:overriding-value` option to `:insert` clause. Documentation TBD. * Address [#407](https://github.com/seancorfield/honeysql/issues/407) by adding support for temporal queries (see `FROM` in [SQL Clause Reference](https://cljdoc.org/d/com.github.seancorfield/honeysql/CURRENT/doc/getting-started/sql-clause-reference#from)). * Address [#389](https://github.com/seancorfield/honeysql/issues/389) by adding examples of `[:only :table]` producing `ONLY(table)`. * Add `:create-or-replace-view` to support PostgreSQL's lack of `IF NOT EXISTS` for `CREATE VIEW`. diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 7f3d5e9..a3f3e18 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -749,7 +749,12 @@ (if-let [columns (clause-body :columns)] (cons columns (format-columns :force-columns columns)) (when-let [values (clause-body :values)] - (columns-from-values values false)))] + (columns-from-values values false))) + [opts table] (if (and (sequential? table) (map? (first table))) + ((juxt first rest) table) + [{} table]) + overriding (when-let [type (:overriding-value opts)] + (str " OVERRIDING " (sql-kw type) " VALUE"))] (if (sequential? table) (cond (map? (second table)) (let [[table statement] table @@ -768,6 +773,7 @@ ") ") (seq cols') (str cols-sql' " ")) + overriding sql)] (into t-params) (into c-params) @@ -780,20 +786,23 @@ (-> [(str (sql-kw k) " " t-sql " (" (str/join ", " c-sqls) - ")")] + ")" + overriding)] (into t-params) (into c-params))) :else (let [[sql & params] (format-entity-alias table)] (-> [(str (sql-kw k) " " sql (when (seq cols') - (str " " cols-sql')))] + (str " " cols-sql')) + overriding)] (into cols-params') (into params)))) (let [[sql & params] (format-entity-alias table)] (-> [(str (sql-kw k) " " sql (when (seq cols') - (str " " cols-sql')))] + (str " " cols-sql')) + overriding)] (into cols-params') (into params)))))) @@ -2245,4 +2254,9 @@ (sql/format {:select [:u.username] :from [[:user :u :for :system-time :from [:inline "2019-08-01 15:23:00"] :to [:inline "2019-08-01 15:24:00"]]] :where [:= :u.id 9]}) + ;; #496 -- overriding + (sql/register-clause! :overriding-system-value + (fn [_ _] ["OVERRIDING SYSTEM VALUE"]) + :values) + (sql/format {:insert-into :foo :values [{:id 1}] :overriding-system-value true}) )