diff --git a/src/honeysql/format.clj b/src/honeysql/format.clj index ded5eff..bd16e97 100644 --- a/src/honeysql/format.clj +++ b/src/honeysql/format.clj @@ -276,6 +276,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 (sort-clauses (keys x)) sql-str (binding [*subquery?* true @@ -289,15 +299,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) "?"))) diff --git a/src/honeysql/types.clj b/src/honeysql/types.clj index 8ab36f0..ffa6ccc 100644 --- a/src/honeysql/types.clj +++ b/src/honeysql/types.clj @@ -1,78 +1,42 @@ (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)) - -(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)) + (SqlCall. name args)) ;;;; -(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)) - -(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)) + (SqlRaw. (str s))) ;;;; -(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)) +;;;; + +;; 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))