From 780fa126b581acc1dff904eb0de17d05b2acd35b Mon Sep 17 00:00:00 2001 From: Jon Doane Date: Thu, 14 Apr 2016 15:09:29 -0400 Subject: [PATCH 1/3] Updated SqlCall to support quoted function calls. Added test to check if a quoted call is actually quoted. --- src/honeysql/core.clj | 1 + src/honeysql/format.clj | 6 +++--- src/honeysql/types.clj | 12 +++++++++--- test/honeysql/core_test.clj | 6 ++++++ 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/honeysql/core.clj b/src/honeysql/core.clj index cb4f03a..776fc6b 100644 --- a/src/honeysql/core.clj +++ b/src/honeysql/core.clj @@ -7,6 +7,7 @@ [clojure.string :as string])) (defalias call types/call) +(defalias call-quoted types/call-quoted) (defalias raw types/raw) (defalias param types/param) (defalias format format/format) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index 79efdc3..ea85b28 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -312,9 +312,9 @@ SqlCall (to-sql [x] (binding [*fn-context?* true] - (let [fn-name (name (.-name x)) - fn-name (fn-aliases fn-name fn-name)] - (apply fn-handler fn-name (.-args x))))) + (let [quoted? (.-quoted? x) + fn-name ((if quoted? to-sql name) (.-name x))] + (apply fn-handler (fn-aliases fn-name fn-name) (.-args x))))) SqlRaw (to-sql [x] (.-s x)) clojure.lang.IPersistentMap diff --git a/src/honeysql/types.clj b/src/honeysql/types.clj index bc746d7..c98f25d 100644 --- a/src/honeysql/types.clj +++ b/src/honeysql/types.clj @@ -1,18 +1,24 @@ (ns honeysql.types) -(defrecord SqlCall [name args]) +(defrecord SqlCall [name args quoted?]) (defn call "Represents a SQL function call. Name should be a keyword." [name & args] - (SqlCall. name args)) + (SqlCall. name args false)) + +(defn call-quoted + "Represents a SQL function call that must be quoted. Like the normal SqlCall + the name should be a keyword." + [name & args] (SqlCall. name args true)) (defn read-sql-call [form] ;; late bind so that we get new class on REPL reset (apply (resolve `call) form)) (defmethod print-method SqlCall [^SqlCall o ^java.io.Writer w] - (.write w (str "#sql/call " (pr-str (into [(.-name o)] (.-args o)))))) + (.write w (str "#sql/call" (when (.-quoted? o) "-quoted") + " " (pr-str (into [(.-name o)] (.-args o)))))) (defmethod print-dup SqlCall [o w] (print-method o w)) diff --git a/test/honeysql/core_test.clj b/test/honeysql/core_test.clj index a59f59f..50d74c3 100644 --- a/test/honeysql/core_test.clj +++ b/test/honeysql/core_test.clj @@ -166,6 +166,12 @@ (from "table") sql/format)))) +(deftest test-quoted-call + (is (= ["SELECT \"foo\".\"bar\"(1, 2, 3)"] + (sql/format + {:select [(sql/call-quoted :foo.bar 1 2 3)]} + :quoting :ansi)))) + (deftest join-test (testing "nil join" (is (= ["SELECT * FROM foo INNER JOIN x ON foo.id = x.id INNER JOIN y"] From 09670c8e1f4b4347ca40264c4524abd985b5a8b3 Mon Sep 17 00:00:00 2001 From: Jon Doane Date: Thu, 14 Apr 2016 15:17:50 -0400 Subject: [PATCH 2/3] Update README to have an example of doing quoted function calls. --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 68aeb0a..5350a38 100644 --- a/README.md +++ b/README.md @@ -249,6 +249,16 @@ To be able to use dashes in quoted names, you can pass ```:allow-dashed-names tr => ["SELECT \"f\".\"foo-id\", \"f\".\"foo-name\" FROM \"foo-bar\" \"f\" WHERE \"f\".\"foo-id\" = 12345"] ``` +In some cases, you may want to quote function calls. To do this, you can use the +```sql/call-quoted``` function instead of the ```sql/call``` function. + +```clj +(sql/format + {:select (sql/quoted-call :my-schema.some-function 1 2 3)} + :quoting :ansi :allow-quoted-names? true) +=> ["SELECT \"my-schema\".\"some-function\"(1, 2, 3)"] +``` + Here's a big, complicated query. Note that Honey SQL makes no attempt to verify that your queries make any sense. It merely renders surface syntax. ```clj From dc13677b4b75f485993b039b7c4dd88da74cbc9b Mon Sep 17 00:00:00 2001 From: Jon Doane Date: Thu, 14 Apr 2016 15:25:25 -0400 Subject: [PATCH 3/3] Add change to CHANGES file. --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 83a7cb0..bb4b6b1 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,7 @@ ## 0.6.4 In development +* Added support for quoted database function calls. (@jrdoane) + ## 0.6.3 Fix bug when SqlCall/SqlRaw object is first argument to another helper (@MichaelBlume)