From f17a6f55823916cfe9e7fc066cae4ad7b101428a Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Sat, 7 Sep 2019 15:18:10 -0700 Subject: [PATCH] Fixes #247 by reverting #132 / #131 Adds `*allow-namespaced-names?*` Var and `:allow-namespaced-names?` option so the previous behavior can still be used. --- CHANGES.md | 2 +- README.md | 4 ++-- src/honeysql/format.cljc | 12 +++++++++--- test/honeysql/format_test.cljc | 10 +++++++--- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 51143cb..974ebf6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,7 @@ ## Changes coming in 0.9.7 * Fix #248 by treating alias as "not a subquery" when generating SQL for it. (@seancorfield) +* Fix #247 by reverting #132 / #131 so the default behavior is friendlier for namespace-qualified keywords used for table and column names, but adds `honeysql.format/*allow-namespaced-names?*` to restore the previous behavior. A `:allow-namespaced-names?` option has been adding to `format` to set this more easily. * Fix #235 by adding `set0` (`:set0`) and `set1` (`:set1`) variants of `sset` (`:set`) to support different placements of `SET` (before `FROM` or after `JOIN` respectively) that different databases require. See also #200. (@seancorfield) * Fix #139 by checking arguments to `columns`/`merge-columns` and throwing an exception if a single collection is supplied (instead of varargs). (@seancorfield) * Fix #128 by adding `truncate` support. (@seancorfield) @@ -64,7 +65,6 @@ BREAKING CHANGES: * Reprioritize WITH wrt UNION and UNION ALL (@emidln) * Helpers for :with and :with-recursive clauses (@enaeher) -* Allow namespaced keywords and symbols for queries. (@jrdoane) * Ensure sequences act as function invocations when in value position (@joodie) * Correct generated arglist for helpers defined with defhelper (@michaelblume) * Don't depend on map iteration order, fix bug with multiple map types (@tomconnors) diff --git a/README.md b/README.md index 96768d2..dfe651e 100644 --- a/README.md +++ b/README.md @@ -334,8 +334,8 @@ To quote identifiers, pass the `:quoting` keyword option to `format`. Valid opti => ["SELECT `foo`.`a` FROM `foo` WHERE `foo`.`a` = ?" "baz"] ``` -To issue a locking select, add a :lock to the query or use the lock helper. The lock value must be a map with a :mode value. The built-in -modes are the standard :update (FOR UPDATE) or the vendor-specific :mysql-share (LOCK IN SHARE MODE) or :postresql-share (FOR SHARE). The +To issue a locking select, add a `:lock` to the query or use the lock helper. The lock value must be a map with a `:mode` value. The built-in +modes are the standard :update (FOR UPDATE) or the vendor-specific `:mysql-share` (LOCK IN SHARE MODE) or `:postresql-share` (FOR SHARE). The lock map may also provide a :wait value, which if false will append the NOWAIT parameter, supported by PostgreSQL. ```clojure diff --git a/src/honeysql/format.cljc b/src/honeysql/format.cljc index 6d9b017..9880068 100644 --- a/src/honeysql/format.cljc +++ b/src/honeysql/format.cljc @@ -44,6 +44,8 @@ (def ^:dynamic *allow-dashed-names?* false) +(def ^:dynamic *allow-namespaced-names?* false) + (def ^:dynamic *name-transform-fn* nil) (def ^:private quote-fns @@ -93,8 +95,11 @@ s (cond (or (keyword? x) (symbol? x)) (name-transform-fn - (str (when-let [n (namespace x)] - (str n "/")) (name x))) + (if *allow-namespaced-names?* + (str (when-let [n (namespace x)] + (str n "/")) + (name x)) + (name x))) (string? x) (if qf x (name-transform-fn x)) :else (str x))] (if-not qf @@ -279,7 +284,8 @@ *input-params* (atom params) *quote-identifier-fn* (quote-fns (:quoting opts)) *parameterizer* (or (:parameterizer opts) :jdbc) - *allow-dashed-names?* (:allow-dashed-names? opts)] + *allow-dashed-names?* (:allow-dashed-names? opts) + *allow-namespaced-names?* (:allow-namespaced-names? opts)] (let [sql-str (to-sql sql-map)] (if (and (seq @*params*) (not= :none (:parameterizer opts))) (if (:return-param-names opts) diff --git a/test/honeysql/format_test.cljc b/test/honeysql/format_test.cljc index 5b85409..ca1267c 100644 --- a/test/honeysql/format_test.cljc +++ b/test/honeysql/format_test.cljc @@ -5,7 +5,8 @@ honeysql.core [honeysql.types :as sql] [honeysql.format :refer - [*allow-dashed-names?* quote-identifier format-clause format + [*allow-dashed-names?* *allow-namespaced-names?* + quote-identifier format-clause format parameterize]])) (deftest test-quote @@ -35,8 +36,11 @@ "\"foo-bar\".\"moo-bar\"")))) (deftest test-namespaced-identifier - (is (= (quote-identifier :foo/bar) "foo/bar")) - (is (= (quote-identifier :foo/bar :style :ansi) "\"foo/bar\""))) + (is (= (quote-identifier :foo/bar) "bar")) + (is (= (quote-identifier :foo/bar :style :ansi) "\"bar\"")) + (binding [*allow-namespaced-names?* true] + (is (= (quote-identifier :foo/bar) "foo/bar")) + (is (= (quote-identifier :foo/bar :style :ansi) "\"foo/bar\"")))) (deftest alias-splitting (is (= ["SELECT `aa`.`c` AS `a.c`, `bb`.`c` AS `b.c`, `cc`.`c` AS `c.c`"]