Support register customized parameterizer
This commit is contained in:
parent
1e76bc00ca
commit
2abe128008
2 changed files with 26 additions and 1 deletions
|
|
@ -56,6 +56,17 @@
|
||||||
:jdbc (constantly "?")
|
:jdbc (constantly "?")
|
||||||
:none #(str (last @*params*))})
|
:none #(str (last @*params*))})
|
||||||
|
|
||||||
|
(defn register-parameterizer
|
||||||
|
"Register f as a customized parameterizer.
|
||||||
|
E.g.:
|
||||||
|
(register-parameterizer :single-quote #(str \"'\" % \"'\"))
|
||||||
|
(format sql-map :parameterizer :single-quote)"
|
||||||
|
[k f]
|
||||||
|
(alter-var-root
|
||||||
|
#'parameterizers
|
||||||
|
(fn [m]
|
||||||
|
(assoc m k #(f (last @*params*))))))
|
||||||
|
|
||||||
(def ^:dynamic *quote-identifier-fn* nil)
|
(def ^:dynamic *quote-identifier-fn* nil)
|
||||||
(def ^:dynamic *parameterizer* nil)
|
(def ^:dynamic *parameterizer* nil)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,8 @@
|
||||||
:cljs [cljs.test :refer-macros]) [deftest testing is are]]
|
:cljs [cljs.test :refer-macros]) [deftest testing is are]]
|
||||||
[honeysql.types :as sql]
|
[honeysql.types :as sql]
|
||||||
[honeysql.format :refer
|
[honeysql.format :refer
|
||||||
[*allow-dashed-names?* quote-identifier format-clause format]]))
|
[*allow-dashed-names?* quote-identifier format-clause format
|
||||||
|
register-parameterizer]]))
|
||||||
|
|
||||||
(deftest test-quote
|
(deftest test-quote
|
||||||
(are
|
(are
|
||||||
|
|
@ -174,3 +175,16 @@
|
||||||
(is (= (format {:where [:and [:= :foo "foo"] [:= :bar "bar"] nil]}
|
(is (= (format {:where [:and [:= :foo "foo"] [:= :bar "bar"] nil]}
|
||||||
:parameterizer :postgresql)
|
:parameterizer :postgresql)
|
||||||
["WHERE (foo = $1 AND bar = $2)" "foo" "bar"]))))
|
["WHERE (foo = $1 AND bar = $2)" "foo" "bar"]))))
|
||||||
|
|
||||||
|
(register-parameterizer :single-quote #(str \' % \'))
|
||||||
|
(register-parameterizer :mysql-fill (constantly "?"))
|
||||||
|
|
||||||
|
(deftest customized-parameterizer
|
||||||
|
(testing "should fill param with single quote"
|
||||||
|
(is (= (format {:where [:and [:= :foo "foo"] [:= :bar "bar"] nil]}
|
||||||
|
:parameterizer :single-quote)
|
||||||
|
["WHERE (foo = 'foo' AND bar = 'bar')" "foo" "bar"])))
|
||||||
|
(testing "should fill param with ?"
|
||||||
|
(is (= (format {:where [:and [:= :foo "foo"] [:= :bar "bar"] nil]}
|
||||||
|
:parameterizer :mysql-fill)
|
||||||
|
["WHERE (foo = ? AND bar = ?)" "foo" "bar"]))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue