address #496 by adding :overriding-value option

This commit is contained in:
Sean Corfield 2023-08-26 16:45:39 -07:00
parent 44ffd340f5
commit ad1b0f9880
2 changed files with 19 additions and 4 deletions

View file

@ -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`.

View file

@ -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})
)