From 95e208485a7acf28c8dde6f540bc22c6ee7d3c1f Mon Sep 17 00:00:00 2001 From: Jon Doane Date: Sun, 11 Oct 2015 10:47:14 -0400 Subject: [PATCH 1/5] Added support for preserving dashes in quoted names.o --- src/honeysql/format.clj | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index c879fa7..d17565e 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -38,6 +38,8 @@ (def ^:dynamic *subquery?* false) +(def ^:dynamic *allow-dashed-names?* true) + (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) From 1f6e9151fd2e71882fa23c66aae4aae54a97c067 Mon Sep 17 00:00:00 2001 From: Jon Doane Date: Sun, 11 Oct 2015 11:03:03 -0400 Subject: [PATCH 2/5] Updated default for allowing dashes. --- src/honeysql/format.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index d17565e..5b01834 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -38,7 +38,7 @@ (def ^:dynamic *subquery?* false) -(def ^:dynamic *allow-dashed-names?* true) +(def ^:dynamic *allow-dashed-names?* false) (def ^:private quote-fns {:ansi #(str \" % \") From 7e78b10ae492f4a511edca62cb5ca376b5ce2c53 Mon Sep 17 00:00:00 2001 From: Jon Doane Date: Sun, 11 Oct 2015 11:29:55 -0400 Subject: [PATCH 3/5] Added simple test to check for preserved dashes when the var is bound to true. --- test/honeysql/format_test.clj | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/honeysql/format_test.clj b/test/honeysql/format_test.clj index 26f90bc..f35db3f 100644 --- a/test/honeysql/format_test.clj +++ b/test/honeysql/format_test.clj @@ -18,6 +18,11 @@ :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\"")))) + (deftest test-cte (is (= (format-clause (first {:with [[:query {:select [:foo] :from [:bar]}]]}) nil) From f38668edc64a16195e4a0c2978f6109d7c2570a4 Mon Sep 17 00:00:00 2001 From: Jon Doane Date: Sun, 11 Oct 2015 11:38:50 -0400 Subject: [PATCH 4/5] Added one more assertion to test dashed names that are split by periods. --- test/honeysql/format_test.clj | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/honeysql/format_test.clj b/test/honeysql/format_test.clj index f35db3f..2980260 100644 --- a/test/honeysql/format_test.clj +++ b/test/honeysql/format_test.clj @@ -21,7 +21,9 @@ (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 :style :ansi) "\"foo-bar\"")) + (is (= (quote-identifier :foo-bar.moo-bar :style :ansi) + "\"foo-bar\".\"moo-bar\"")))) (deftest test-cte (is (= (format-clause From 4b1f26632e62125a95ee0709f902b04c9cda0700 Mon Sep 17 00:00:00 2001 From: Jon Doane Date: Sun, 11 Oct 2015 12:14:47 -0400 Subject: [PATCH 5/5] Add a "huh" on the allow-dashed-names option on honeysql.format/format. --- src/honeysql/format.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index 5b01834..8e2dc18 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -232,7 +232,7 @@ *input-params* (atom params) *quote-identifier-fn* (quote-fns (:quoting opts)) *parameterizer* (parameterizers (or (:parameterizer opts) :jdbc)) - *allow-dashed-names?* (:allow-dashed-names opts)] + *allow-dashed-names?* (:allow-dashed-names? opts)] (let [sql-str (to-sql sql-map)] (if (seq @*params*) (if (:return-param-names opts)