Merge pull request #93 from jrdoane/master

Added support for preserving dashes in quoted names.
This commit is contained in:
Michael Blume 2015-10-13 23:36:08 -07:00
commit d1eb29b28f
2 changed files with 15 additions and 4 deletions

View file

@ -38,6 +38,8 @@
(def ^:dynamic *subquery?* false) (def ^:dynamic *subquery?* false)
(def ^:dynamic *allow-dashed-names?* false)
(def ^:private quote-fns (def ^:private quote-fns
{:ansi #(str \" % \") {:ansi #(str \" % \")
:mysql #(str \` % \`) :mysql #(str \` % \`)
@ -55,12 +57,13 @@
(string/replace s "-" "_")) (string/replace s "-" "_"))
(defn quote-identifier [x & {:keys [style split] :or {split true}}] (defn quote-identifier [x & {:keys [style split] :or {split true}}]
(let [qf (if style (let [name-transform-fn (if *allow-dashed-names?* identity undasherize)
qf (if style
(quote-fns style) (quote-fns style)
*quote-identifier-fn*) *quote-identifier-fn*)
s (cond s (cond
(or (keyword? x) (symbol? x)) (undasherize (name x)) (or (keyword? x) (symbol? x)) (name-transform-fn (name x))
(string? x) (if qf x (undasherize x)) (string? x) (if qf x (name-transform-fn x))
:else (str x))] :else (str x))]
(if-not qf (if-not qf
s s
@ -228,7 +231,8 @@
*param-names* (atom []) *param-names* (atom [])
*input-params* (atom params) *input-params* (atom params)
*quote-identifier-fn* (quote-fns (:quoting opts)) *quote-identifier-fn* (quote-fns (:quoting opts))
*parameterizer* (parameterizers (or (:parameterizer opts) :jdbc))] *parameterizer* (parameterizers (or (:parameterizer opts) :jdbc))
*allow-dashed-names?* (:allow-dashed-names? opts)]
(let [sql-str (to-sql sql-map)] (let [sql-str (to-sql sql-map)]
(if (seq @*params*) (if (seq @*params*)
(if (:return-param-names opts) (if (:return-param-names opts)

View file

@ -18,6 +18,13 @@
:foo-bar "foo_bar") :foo-bar "foo_bar")
(is (= (quote-identifier "*" :style :ansi) "*"))) (is (= (quote-identifier "*" :style :ansi) "*")))
(deftest test-dashed-quote
(binding [*allow-dashed-names?* true]
(is (= (quote-identifier :foo-bar) "foo-bar"))
(is (= (quote-identifier :foo-bar :style :ansi) "\"foo-bar\""))
(is (= (quote-identifier :foo-bar.moo-bar :style :ansi)
"\"foo-bar\".\"moo-bar\""))))
(deftest test-cte (deftest test-cte
(is (= (format-clause (is (= (format-clause
(first {:with [[:query {:select [:foo] :from [:bar]}]]}) nil) (first {:with [[:query {:select [:foo] :from [:bar]}]]}) nil)