@@ -37,11 +37,18 @@ import Data.Monoid(Monoid(..))
3737import Database.PostgreSQL.Simple.FromField
3838import Database.PostgreSQL.Simple.ToField
3939
40+class ToHStore a where
41+ toHStore :: a -> HStoreBuilder
42+
43+-- | Represents valid hstore syntax.
4044data HStoreBuilder
4145 = Empty
4246 | Comma !Builder
4347 deriving (Typeable)
4448
49+instance ToHStore HStoreBuilder where
50+ toHStore = id
51+
4552toBuilder :: HStoreBuilder -> Builder
4653toBuilder x = case x of
4754 Empty -> mempty
@@ -108,8 +115,11 @@ instance ToField HStoreBuilder where
108115
109116newtype HStoreList = HStoreList [(Text,Text)] deriving (Typeable, Show)
110117
118+instance ToHStore HStoreList where
119+ toHStore (HStoreList xs) = mconcat (map (uncurry hstore) xs)
120+
111121instance ToField HStoreList where
112− toField (HStoreList xs) = toField (mconcat (map (uncurry hstore) xs))
122+ toField xs = toField (toHStore xs)
113123
114124instance FromField HStoreList where
115125 fromField f mdat = do
@@ -128,12 +138,15 @@ instance FromField HStoreList where
128138 Right (Right val) ->
129139 return val
130140
131−newtype HStoreMap = HStoreMap (Map Text Text) deriving (Eq, Ord, Typeable, Show)
141+newtype HStoreMap = HStoreMap (Map Text Text) deriving (Eq, Ord, Typeable, Show)
132142
133−instance ToField HStoreMap where
134− toField (HStoreMap xs) = toField (Map.foldWithKey f mempty xs)
143+instance ToHStore HStoreMap where
144+ toHStore (HStoreMap xs) = Map.foldWithKey f mempty xs
135145 where f k v xs = hstore k v `mappend` xs
136146
147+instance ToField HStoreMap where
148+ toField xs = toField (toHStore xs)
149+
137150instance FromField HStoreMap where
138151 fromField f mdat = convert <$> fromField f mdat
139152 where convert (HStoreList xs) = HStoreMap (Map.fromList xs)