korma / Korma

Tasty SQL for Clojure.
http://sqlkorma.com
1.48k stars 222 forks source link

Field of record support extensible data type #331

Closed gfZeng closed 8 years ago

gfZeng commented 8 years ago

Many korma users want korm support JSON data type while the database is Postgresql. jdbc provider two protocols for users. I port it to korma.

There is an example:

(require '[clojure.java.jdbc :as jdbc]
         '[tools.data.json :as json])

(import '[org.postgresql.util PGobject])

(extend-protocol jdbc/ISQLValue
  clojure.lang.IPersistentMap
  (sql-value [value]
    (doto (PGobject.)
      (.setType "json")
      (.setValue (json/dump-str value))))
  java.util.Date
  (sql-value [value]
    (java.sql.Timestamp. (.getTime value))))

(extend-protocol jdbc/IResultSetReadColumn
  PGobject
  (result-set-read-column [pgobj metadata idx]
    (let [type  (.getType pgobj)
          value (.getValue pgobj)]
      (case type
        "json" (json/load-str value :key-fn keyword)
        :else value))))
gfZeng commented 8 years ago

Currently, the lein test can not pass. And so I close this PR. I will fix those tests when we thinks it is a good feature. And then re-open this PR

immoh commented 8 years ago

Sounds good.

gfZeng commented 8 years ago

@immoh

I think also, korma need some plugins. Such as: pagination, DB sharding and Postgres customize data type.

gfZeng commented 8 years ago

I fixed the tests, And reopen an new PR