Updated SqlCall to support quoted function calls.

Added test to check if a quoted call is actually quoted.
This commit is contained in:
Jon Doane 2016-04-14 15:09:29 -04:00
parent 43df955a86
commit 780fa126b5
4 changed files with 19 additions and 6 deletions

View file

@ -7,6 +7,7 @@
[clojure.string :as string])) [clojure.string :as string]))
(defalias call types/call) (defalias call types/call)
(defalias call-quoted types/call-quoted)
(defalias raw types/raw) (defalias raw types/raw)
(defalias param types/param) (defalias param types/param)
(defalias format format/format) (defalias format format/format)

View file

@ -312,9 +312,9 @@
SqlCall SqlCall
(to-sql [x] (to-sql [x]
(binding [*fn-context?* true] (binding [*fn-context?* true]
(let [fn-name (name (.-name x)) (let [quoted? (.-quoted? x)
fn-name (fn-aliases fn-name fn-name)] fn-name ((if quoted? to-sql name) (.-name x))]
(apply fn-handler fn-name (.-args x))))) (apply fn-handler (fn-aliases fn-name fn-name) (.-args x)))))
SqlRaw SqlRaw
(to-sql [x] (.-s x)) (to-sql [x] (.-s x))
clojure.lang.IPersistentMap clojure.lang.IPersistentMap

View file

@ -1,18 +1,24 @@
(ns honeysql.types) (ns honeysql.types)
(defrecord SqlCall [name args]) (defrecord SqlCall [name args quoted?])
(defn call (defn call
"Represents a SQL function call. Name should be a keyword." "Represents a SQL function call. Name should be a keyword."
[name & args] [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] (defn read-sql-call [form]
;; late bind so that we get new class on REPL reset ;; late bind so that we get new class on REPL reset
(apply (resolve `call) form)) (apply (resolve `call) form))
(defmethod print-method SqlCall [^SqlCall o ^java.io.Writer w] (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] (defmethod print-dup SqlCall [o w]
(print-method o w)) (print-method o w))

View file

@ -166,6 +166,12 @@
(from "table") (from "table")
sql/format)))) 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 (deftest join-test
(testing "nil join" (testing "nil join"
(is (= ["SELECT * FROM foo INNER JOIN x ON foo.id = x.id INNER JOIN y"] (is (= ["SELECT * FROM foo INNER JOIN x ON foo.id = x.id INNER JOIN y"]