pghstore-clj
pghstore-clj copied to clipboard
Commas cause all kinds of havoc
A comma in the text of an hstore value causes serious problems when reading it out of the DB. Right now pghstore-clj assumes that commas strictly denote the separations between key/value pairs. This goes to hell when there's commas in the text of fields, however.
I suspect a better implementation may be something closer to the state machine approach at http://code.google.com/p/pg-spring-type-mapper/source/browse/src/org/valgog/utils/postgres/HStore.java?spec=svnbac909c06dd97a2c8b2bbfa96141d0e1ba942f73&r=bac909c06dd97a2c8b2bbfa96141d0e1ba942f73
Temporary hack: replacing ,
, =
and >
with unicode private use characters
(ns hstore
(:use pghstore-clj.core)
(:require [clojure.string :as string]))
(defn hstore-escape [v]
(-> v
(string/replace "\"" "\\\"")
(string/replace \, (char 57344))
(string/replace \= (char 57345))
(string/replace \> (char 57346))))
(defn hstore-unescape [v]
(-> v
(string/replace "\\" "\"")
(string/replace (char 57344) \,)
(string/replace (char 57345) \=)
(string/replace (char 57346) \>)))
(defn from-quoted-hstore [x]
(into {} (filter identity (map (fn [[k v]] (when v [k (hstore-unescape v)])) (from-hstore x)))))
(defn to-quoted-hstore [x]
(to-hstore (into {} (map (fn [[k v]] [k (hstore-escape v)]) x))))
Not very good if you need to access the database from other applications as they need to do the same thing…