This commit is contained in:
Sean Corfield 2021-09-25 17:25:44 -07:00
parent dae09ff601
commit 92e0a04a84
3 changed files with 27 additions and 8 deletions

View file

@ -2,7 +2,9 @@
* 2.0.next in progress * 2.0.next in progress
* Address #364 by recommending how to handle PostgreSQL operators that contain `@`. * Address #364 by recommending how to handle PostgreSQL operators that contain `@`.
* Fix #363 by aligning more closely the semantics of `:inline` syntax with the `:inline true` option. A side effect of this is that `[:inline [:param :foo]]` will now (correctly) inline the value of the parameter `:foo` whereas it previously produced `PARAMS SOURCE`. In addition, inlining has been extended to vector values, so `[:inline ["a" "b" "c"]]` will now produce `('a', 'b', 'c')` and `[:inline [:lift ["a" "b" "c"]]]` will now produce `['a', 'b', 'c']` which is what people seemed to expect (the behavior was previously unspecified). * Fix #363 and #362 by aligning more closely the semantics of `:inline` syntax with the `:inline true` option. A side effect of this is that `[:inline [:param :foo]]` will now (correctly) inline the value of the parameter `:foo` whereas it previously produced `PARAMS SOURCE`. In addition, inlining has been extended to vector values, so `[:inline ["a" "b" "c"]]` will now produce `('a', 'b', 'c')` and `[:inline [:lift ["a" "b" "c"]]]` will now produce `['a', 'b', 'c']` which is what people seemed to expect (the behavior was previously unspecified).
* Fix #353 by correcting handling of strings used as SQL entities (such as table names); this was a regression introduced by a recent enhancement to `:create-table`.
* Fix #349 by adding an optional `:quoted` argument to `set-dialect!`.
* Support `AS` aliasing in `DELETE FROM`. * Support `AS` aliasing in `DELETE FROM`.
* Switch from `readme` to `test-doc-blocks` so all documentation is tested! * Switch from `readme` to `test-doc-blocks` so all documentation is tested!
* Clean up build/update deps. * Clean up build/update deps.

View file

@ -325,7 +325,15 @@ specify a dialect in the `format` call, you can specify
;;=> nil ;;=> nil
(sql/format '{select (id) from (table)} {:quoted true}) (sql/format '{select (id) from (table)} {:quoted true})
;;=> ["SELECT [id] FROM [table]"] ;;=> ["SELECT [id] FROM [table]"]
;; and to the default of :ansi ;; you can also choose to enable quoting globally
;; when you set a dialect:
(sql/set-dialect! :mysql :quoted true)
(sql/format '{select (id) from (table)})
;;=> ["SELECT `id` FROM `table`"]
;; and opt out for a specific call:
(sql/format '{select (id) from (table)} {:quoted false})
;;=> ["SELECT id FROM table"]
;; and reset back to the default of :ansi
(sql/set-dialect! :ansi) (sql/set-dialect! :ansi)
;;=> nil ;;=> nil
(sql/format '{select (id) from (table)} {:quoted true}) (sql/format '{select (id) from (table)} {:quoted true})

View file

@ -23,7 +23,8 @@
* `register-fn!` -- register a new function call (or special syntax) * `register-fn!` -- register a new function call (or special syntax)
formatter. formatter.
* `register-op!` -- register a new operator formatter. * `register-op!` -- register a new operator formatter.
* `set-dialect!` -- set the default dialect to be used for formatting. * `set-dialect!` -- set the default dialect to be used for formatting,
and optionally set a global `:quoted` option.
* `sql-kw` -- turns a Clojure keyword (or symbol) into SQL code (makes * `sql-kw` -- turns a Clojure keyword (or symbol) into SQL code (makes
it uppercase and replaces - with space). " it uppercase and replaces - with space). "
(:refer-clojure :exclude [format]) (:refer-clojure :exclude [format])
@ -97,6 +98,7 @@
; should become defonce ; should become defonce
(def ^:private default-dialect (atom (:ansi dialects))) (def ^:private default-dialect (atom (:ansi dialects)))
(def ^:private default-quoted (atom nil))
(def ^:private ^:dynamic *dialect* nil) (def ^:private ^:dynamic *dialect* nil)
;; nil would be a better default but that makes testing individual ;; nil would be a better default but that makes testing individual
@ -1323,9 +1325,12 @@
@current-clause-order) @current-clause-order)
*inline* (when (contains? opts :inline) *inline* (when (contains? opts :inline)
(:inline opts)) (:inline opts))
*quoted* (if (contains? opts :quoted) *quoted* (cond (contains? opts :quoted)
(:quoted opts) (:quoted opts)
dialect?) dialect?
true
:else
@default-quoted)
*quoted-snake* (when (contains? opts :quoted-snake) *quoted-snake* (when (contains? opts :quoted-snake)
(:quoted-snake opts)) (:quoted-snake opts))
*params* (:params opts)] *params* (:params opts)]
@ -1337,11 +1342,15 @@
Can be: `:ansi` (the default), `:mysql`, `:oracle`, or `:sqlserver`. Can be: `:ansi` (the default), `:mysql`, `:oracle`, or `:sqlserver`.
Can optionally accept `:quoted true` (or `:quoted false`) to set the
default global quoting strategy.
Dialects are always applied to the base order to create the current order." Dialects are always applied to the base order to create the current order."
[dialect] [dialect & {:keys [quoted]}]
(reset! default-dialect (get dialects (check-dialect dialect))) (reset! default-dialect (get dialects (check-dialect dialect)))
(when-let [f (:clause-order-fn @default-dialect)] (when-let [f (:clause-order-fn @default-dialect)]
(reset! current-clause-order (f @base-clause-order)))) (reset! current-clause-order (f @base-clause-order)))
(reset! default-quoted quoted))
(defn clause-order (defn clause-order
"Return the current order that known clauses will be applied when "Return the current order that known clauses will be applied when