From 415bbce6b3c3a4139aaceb8b7d164e462f91e4d5 Mon Sep 17 00:00:00 2001 From: Mike Blume Date: Tue, 3 Mar 2015 18:51:57 -0800 Subject: [PATCH 1/3] handle SqlParam explicitly --- src/honeysql/format.clj | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index 058f164..527e383 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -245,6 +245,16 @@ (apply fn-handler fn-name (.args x))))) SqlRaw (-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 (-to-sql [x] (let [clause-ops (concat (filter #(contains? x %) clause-order) @@ -260,15 +270,8 @@ nil (-to-sql [x] "NULL") Object - (-to-sql [x] (let [[x pname] (if (instance? SqlParam x) - (let [pname (param-name x)] - (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)))])] + ;; Anonymous param name -- :_1, :_2, etc. + (-to-sql [x] (let [pname (keyword (str "_" (swap! *param-counter* inc)))] (swap! *param-names* conj pname) (swap! *params* conj x) "?"))) From ea6dc981250aaab7c724cff51d531c9d2daaa97c Mon Sep 17 00:00:00 2001 From: Mike Blume Date: Tue, 3 Mar 2015 18:53:34 -0800 Subject: [PATCH 2/3] just use defrecords, not deftypes --- src/honeysql/types.clj | 34 ++++++---------------------------- 1 file changed, 6 insertions(+), 28 deletions(-) diff --git a/src/honeysql/types.clj b/src/honeysql/types.clj index 8ab36f0..4ce304c 100644 --- a/src/honeysql/types.clj +++ b/src/honeysql/types.clj @@ -1,21 +1,11 @@ (ns honeysql.types) -(deftype SqlCall [name args _meta] - 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))) +(defrecord SqlCall [name args]) (defn call "Represents a SQL function call. Name should be a keyword." [name & args] - (SqlCall. name args nil)) + (SqlCall. name args)) (defn read-sql-call [form] (apply call form)) @@ -28,18 +18,12 @@ ;;;; -(deftype SqlRaw [s _meta] - 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))) +(defrecord SqlRaw [s]) (defn raw "Represents a raw SQL string" [s] - (SqlRaw. (str s) nil)) + (SqlRaw. (str s))) (defn read-sql-raw [form] (raw form)) @@ -52,18 +36,12 @@ ;;;; -(deftype SqlParam [name _meta] - 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))) +(defrecord SqlParam [name]) (defn param "Represents a SQL parameter which can be filled in later" [name] - (SqlParam. name nil)) + (SqlParam. name)) (defn param-name [^SqlParam param] (.name param)) From 6177bcf8ae17837e285b8344e43cbba285b3361a Mon Sep 17 00:00:00 2001 From: Mike Blume Date: Tue, 3 Mar 2015 18:55:13 -0800 Subject: [PATCH 3/3] remove custom printers leave custom readers for backards compatibility --- src/honeysql/types.clj | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/src/honeysql/types.clj b/src/honeysql/types.clj index 4ce304c..ffa6ccc 100644 --- a/src/honeysql/types.clj +++ b/src/honeysql/types.clj @@ -7,15 +7,6 @@ [name & args] (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)) - ;;;; (defrecord SqlRaw [s]) @@ -25,15 +16,6 @@ [s] (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)) - ;;;; (defrecord SqlParam [name]) @@ -46,11 +28,15 @@ (defn param-name [^SqlParam 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] (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))