avoid boilerplate using defrecord

This commit is contained in:
Mike Blume 2015-03-31 11:21:39 -07:00
parent 04a7086687
commit cb296e2e31

View file

@ -1,22 +1,11 @@
(ns honeysql.types) (ns honeysql.types)
(deftype SqlCall [name args _meta] (defrecord SqlCall [name args])
Object
(hashCode [_] (hash-combine (hash name) (hash args)))
(equals [this x]
(cond (identical? this x) true
(instance? SqlCall x) (let [^SqlCall x x]
(and (= name (.name x))
(= args (.args x))))
:else false))
clojure.lang.IObj
(meta [_] _meta)
(withMeta [_ m] (SqlCall. name args 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] (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
@ -30,18 +19,12 @@
;;;; ;;;;
(deftype SqlRaw [s _meta] (defrecord SqlRaw [s])
Object
(hashCode [this] (hash-combine (hash (class this)) (hash s)))
(equals [_ x] (and (instance? SqlRaw x) (= s (.s ^SqlRaw x))))
clojure.lang.IObj
(meta [_] _meta)
(withMeta [_ m] (SqlRaw. s 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] (defn read-sql-raw [form]
;; late bind, as above ;; late bind, as above
@ -55,18 +38,12 @@
;;;; ;;;;
(deftype SqlParam [name _meta] (defrecord SqlParam [name])
Object
(hashCode [this] (hash-combine (hash (class this)) (hash (name name))))
(equals [_ x] (and (instance? SqlParam x) (= name (.name ^SqlParam x))))
clojure.lang.IObj
(meta [_] _meta)
(withMeta [_ m] (SqlParam. name 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))
@ -83,18 +60,12 @@
;;;; ;;;;
(deftype SqlArray [values _meta] (defrecord SqlArray [values])
Object
(hashCode [this] (hash-combine (hash (class this)) (hash values)))
(equals [_ x] (and (instance? SqlArray x) (= values (.values ^SqlArray x))))
clojure.lang.IObj
(meta [_] _meta)
(withMeta [_ m] (SqlArray. values m)))
(defn array (defn array
"Represents a SQL array." "Represents a SQL array."
[values] [values]
(SqlArray. values nil)) (SqlArray. values))
(defn array-vals [^SqlArray a] (defn array-vals [^SqlArray a]
(.-values a)) (.-values a))