diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index c879fa7..8e2dc18 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -38,6 +38,8 @@ (def ^:dynamic *subquery?* false) +(def ^:dynamic *allow-dashed-names?* false) + (def ^:private quote-fns {:ansi #(str \" % \") :mysql #(str \` % \`) @@ -55,12 +57,13 @@ (string/replace s "-" "_")) (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-identifier-fn*) s (cond - (or (keyword? x) (symbol? x)) (undasherize (name x)) - (string? x) (if qf x (undasherize x)) + (or (keyword? x) (symbol? x)) (name-transform-fn (name x)) + (string? x) (if qf x (name-transform-fn x)) :else (str x))] (if-not qf s @@ -228,7 +231,8 @@ *param-names* (atom []) *input-params* (atom params) *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)] (if (seq @*params*) (if (:return-param-names opts) diff --git a/test/honeysql/format_test.clj b/test/honeysql/format_test.clj index 26f90bc..2980260 100644 --- a/test/honeysql/format_test.clj +++ b/test/honeysql/format_test.clj @@ -18,6 +18,13 @@ :foo-bar "foo_bar") (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 (is (= (format-clause (first {:with [[:query {:select [:foo] :from [:bar]}]]}) nil)