make SqlCall and SqlRaw readable and accept metadata

This commit is contained in:
Justin Kramer 2012-07-13 12:35:58 -04:00
parent b575439cb4
commit 039ab16447
2 changed files with 24 additions and 4 deletions

View file

@ -81,6 +81,10 @@ calls and raw SQL fragments:
(sql/format *1)
=> ["SELECT COUNT(*), @var := foo.bar FROM foo"]
;; Fully readable
(= *2 (read-string (pr-str *2)))
=> true
```
Here's a complicated query:

View file

@ -6,10 +6,20 @@
;;;;
(deftype SqlCall [name args])
(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)))
(defn call [name & args]
(SqlCall. name args))
(SqlCall. name args nil))
(defn read-sql-call [form]
(apply call form))
@ -22,10 +32,16 @@
;;;;
(deftype SqlRaw [s])
(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)))
(defn raw [s]
(SqlRaw. (str s)))
(SqlRaw. (str s) nil))
(defn read-sql-raw [form]
(raw form))