More helper docstrings
This commit is contained in:
parent
d5ab2a8d8c
commit
05360d10d6
1 changed files with 48 additions and 0 deletions
|
|
@ -476,16 +476,64 @@
|
||||||
|
|
||||||
;; helpers that produce non-clause expressions -- must be listed below:
|
;; helpers that produce non-clause expressions -- must be listed below:
|
||||||
(defn composite
|
(defn composite
|
||||||
|
"Accepts any number of SQL expressions and produces
|
||||||
|
a composite value from them:
|
||||||
|
|
||||||
|
(composite :a 42)
|
||||||
|
|
||||||
|
Produces: (a, ?)
|
||||||
|
Parameters: 42"
|
||||||
[& args]
|
[& args]
|
||||||
(into [:composite] args))
|
(into [:composite] args))
|
||||||
|
|
||||||
;; to make this easy to use in a select, wrap it so it becomes a function:
|
;; to make this easy to use in a select, wrap it so it becomes a function:
|
||||||
(defn over
|
(defn over
|
||||||
|
"Accepts any number of OVER clauses, each of which
|
||||||
|
is a pair of an aggregate function and a window function
|
||||||
|
or a triple of an aggregate function, a window function,
|
||||||
|
and an alias:
|
||||||
|
|
||||||
|
(select :id (over [[:avg :salary] (partition-by :department)]))
|
||||||
|
|
||||||
|
Produces: SELECT id, AVG(salary) OVER ()PARTITION BY department)"
|
||||||
[& args]
|
[& args]
|
||||||
[(into [:over] args)])
|
[(into [:over] args)])
|
||||||
|
|
||||||
;; this helper is intended to ease the migration from nilenso:
|
;; this helper is intended to ease the migration from nilenso:
|
||||||
(defn upsert
|
(defn upsert
|
||||||
|
"Provided purely to ease migration from nilenso/honeysql-postgres
|
||||||
|
this accepts a single clause, constructed from on-conflict,
|
||||||
|
do-nothing or do-update-set, and where. Any of those are optional.
|
||||||
|
|
||||||
|
This helper unpacks that clause and turns it into what HoneySQL
|
||||||
|
2.x expects, with any where clause being an argument to the
|
||||||
|
do-update-set helper, along with the `:fields`.
|
||||||
|
|
||||||
|
nilenso/honeysql-postgres:
|
||||||
|
|
||||||
|
(-> ...
|
||||||
|
(upsert (-> (on-conflict :col)
|
||||||
|
do-nothing)))
|
||||||
|
(-> ...
|
||||||
|
(upsert (-> (on-conflict :col)
|
||||||
|
(do-update-set :x)
|
||||||
|
(where [:<> :x nil]))))
|
||||||
|
|
||||||
|
HoneySQL 2.x:
|
||||||
|
|
||||||
|
(-> ...
|
||||||
|
(on-conflict :col)
|
||||||
|
do-nothing)
|
||||||
|
(-> ...
|
||||||
|
(on-conflict :col)
|
||||||
|
(do-update-set {:fields [:x]
|
||||||
|
:where [:<> :x nil]}))
|
||||||
|
|
||||||
|
Alternative structure for that second one:
|
||||||
|
|
||||||
|
(-> ...
|
||||||
|
(on-conflict :col)
|
||||||
|
(do-update-set :x {:where [:<> :x nil]}))"
|
||||||
([clause] (upsert {} clause))
|
([clause] (upsert {} clause))
|
||||||
([data clause]
|
([data clause]
|
||||||
(let [{:keys [on-conflict do-nothing do-update-set where]} clause]
|
(let [{:keys [on-conflict do-nothing do-update-set where]} clause]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue