improve identifier quoting

This commit is contained in:
Justin Kramer 2013-08-07 21:56:31 -04:00
parent 9d46b48366
commit 03fe1650ee

View file

@ -29,23 +29,28 @@
(def ^:dynamic *subquery?* false) (def ^:dynamic *subquery?* false)
(def ^:private quote-fns (def ^:private quote-fns
{:none identity {:ansi #(str \" % \")
:ansi #(str \" % \")
:mysql #(str \` % \`) :mysql #(str \` % \`)
:sqlserver #(str \[ % \])}) :sqlserver #(str \[ % \])})
(def ^:dynamic *quote-identifier-fn* nil) (def ^:dynamic *quote-identifier-fn* nil)
(defn- undasherize [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 [qf (if style
(quote-fns style) (quote-fns style)
*quote-identifier-fn*)] *quote-identifier-fn*)
s (cond
(or (keyword? x) (symbol? x)) (undasherize (name x))
(string? x) (if qf x (undasherize x))
:else (str x))]
(if-not qf (if-not qf
x s
(let [qf* #(if (= "*" %) % (qf %))] (let [qf* #(if (= "*" %) % (qf %))]
(if-not split (if-not split
(qf* (name x)) (qf* s)
(let [parts (string/split (name x) #"\.")] (let [parts (string/split s #"\.")]
(string/join "." (map qf* parts)))))))) (string/join "." (map qf* parts))))))))
(def infix-fns (def infix-fns
@ -195,9 +200,9 @@
\% (let [call-args (string/split (subs s 1) #"\." 2)] \% (let [call-args (string/split (subs s 1) #"\." 2)]
(to-sql (apply call (map keyword call-args)))) (to-sql (apply call (map keyword call-args))))
\? (to-sql (param (keyword (subs s 1)))) \? (to-sql (param (keyword (subs s 1))))
(-> s (string/replace "-" "_") quote-identifier)))) (quote-identifier x))))
clojure.lang.Symbol clojure.lang.Symbol
(-to-sql [x] (-> x name (string/replace "-" "_"))) (-to-sql [x] (quote-identifier x))
java.lang.Number java.lang.Number
(-to-sql [x] (str x)) (-to-sql [x] (str x))
java.lang.Boolean java.lang.Boolean