This commit is contained in:
Michael Blume 2015-03-13 20:48:58 +00:00
commit 57845e3651
2 changed files with 28 additions and 61 deletions

View file

@ -276,6 +276,16 @@
(apply fn-handler fn-name (.args x))))) (apply fn-handler fn-name (.args x)))))
SqlRaw SqlRaw
(-to-sql [x] (.s x)) (-to-sql [x] (.s x))
SqlParam
(-to-sql [x] (let [pname (param-name x)
x (if (map? @*input-params*)
(get @*input-params* pname)
(let [x (first @*input-params*)]
(swap! *input-params* rest)
x))]
(swap! *param-names* conj pname)
(swap! *params* conj x)
"?"))
clojure.lang.IPersistentMap clojure.lang.IPersistentMap
(-to-sql [x] (let [clause-ops (sort-clauses (keys x)) (-to-sql [x] (let [clause-ops (sort-clauses (keys x))
sql-str (binding [*subquery?* true sql-str (binding [*subquery?* true
@ -289,15 +299,8 @@
nil nil
(-to-sql [x] "NULL") (-to-sql [x] "NULL")
Object Object
(-to-sql [x] (let [[x pname] (if (instance? SqlParam x) ;; Anonymous param name -- :_1, :_2, etc.
(let [pname (param-name x)] (-to-sql [x] (let [pname (keyword (str "_" (swap! *param-counter* inc)))]
(if (map? @*input-params*)
[(get @*input-params* pname) pname]
(let [x (first @*input-params*)]
(swap! *input-params* rest)
[x pname])))
;; Anonymous param name -- :_1, :_2, etc.
[x (keyword (str "_" (swap! *param-counter* inc)))])]
(swap! *param-names* conj pname) (swap! *param-names* conj pname)
(swap! *params* conj x) (swap! *params* conj x)
"?"))) "?")))

View file

@ -1,78 +1,42 @@
(ns honeysql.types) (ns honeysql.types)
(deftype SqlCall [name args _meta] (defrecord SqlCall [name args])
Object
(hashCode [this] (hash-combine (hash name) (hash args)))
(equals [this x]
(cond (identical? this x) true
(instance? SqlCall x) (and (= (.name this) (.name x))
(= (.args this) (.args x)))
:else false))
clojure.lang.IObj
(meta [this] _meta)
(withMeta [this m] (SqlCall. (.name this) (.args this) m)))
(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 nil)) (SqlCall. name args))
(defn read-sql-call [form]
(apply call form))
(defmethod print-method SqlCall [^SqlCall o ^java.io.Writer w]
(.write w (str "#sql/call " (pr-str (into [(.name o)] (.args o))))))
(defmethod print-dup SqlCall [o w]
(print-method o w))
;;;; ;;;;
(deftype SqlRaw [s _meta] (defrecord SqlRaw [s])
Object
(hashCode [this] (hash-combine (hash (class this)) (hash s)))
(equals [this x] (and (instance? SqlRaw x) (= (.s this) (.s x))))
clojure.lang.IObj
(meta [this] _meta)
(withMeta [this m] (SqlRaw. (.s this) m)))
(defn raw (defn raw
"Represents a raw SQL string" "Represents a raw SQL string"
[s] [s]
(SqlRaw. (str s) nil)) (SqlRaw. (str s)))
(defn read-sql-raw [form]
(raw form))
(defmethod print-method SqlRaw [^SqlRaw o ^java.io.Writer w]
(.write w (str "#sql/raw " (pr-str (.s o)))))
(defmethod print-dup SqlRaw [o w]
(print-method o w))
;;;; ;;;;
(deftype SqlParam [name _meta] (defrecord SqlParam [name])
Object
(hashCode [this] (hash-combine (hash (class this)) (hash (name name))))
(equals [this x] (and (instance? SqlParam x) (= (.name this) (.name x))))
clojure.lang.IObj
(meta [this] _meta)
(withMeta [this m] (SqlParam. (.name this) m)))
(defn param (defn param
"Represents a SQL parameter which can be filled in later" "Represents a SQL parameter which can be filled in later"
[name] [name]
(SqlParam. name nil)) (SqlParam. name))
(defn param-name [^SqlParam param] (defn param-name [^SqlParam param]
(.name param)) (.name param))
;;;;
;; retain readers for backwards compatibility
(defn read-sql-call [form]
(apply call form))
(defn read-sql-raw [form]
(raw form))
(defn read-sql-param [form] (defn read-sql-param [form]
(param form)) (param form))
(defmethod print-method SqlParam [^SqlParam o ^java.io.Writer w]
(.write w (str "#sql/param " (pr-str (.name o)))))
(defmethod print-dup SqlParam [o w]
(print-method o w))