@@ -37,11 +37,18 @@ import Data.Monoid(Monoid(..))
37import Database.PostgreSQL.Simple.FromField
37import Database.PostgreSQL.Simple.FromField
38import Database.PostgreSQL.Simple.ToField
38import Database.PostgreSQL.Simple.ToField
40+class ToHStore a where
41+ toHStore :: a -> HStoreBuilder
43+-- | Represents valid hstore syntax.
40data HStoreBuilder
44data HStoreBuilder
42 | Comma !Builder
46 | Comma !Builder
43 deriving (Typeable)
47 deriving (Typeable)
49+instance ToHStore HStoreBuilder where
45toBuilder :: HStoreBuilder -> Builder
52toBuilder :: HStoreBuilder -> Builder
46toBuilder x = case x of
53toBuilder x = case x of
47 Empty -> mempty
54 Empty -> mempty
@@ -108,8 +115,11 @@ instance ToField HStoreBuilder where
109newtype HStoreList = HStoreList [(Text,Text)] deriving (Typeable, Show)
116newtype HStoreList = HStoreList [(Text,Text)] deriving (Typeable, Show)
118+instance ToHStore HStoreList where
119+ toHStore (HStoreList xs) = mconcat (map (uncurry hstore) xs)
111instance ToField HStoreList where
121instance ToField HStoreList where
112− toField (HStoreList xs) = toField (mconcat (map (uncurry hstore) xs))
122+ toField xs = toField (toHStore xs)
114instance FromField HStoreList where
124instance FromField HStoreList where
115 fromField f mdat = do
125 fromField f mdat = do
@@ -128,12 +138,15 @@ instance FromField HStoreList where
128 Right (Right val) ->
138 Right (Right val) ->
129 return val
139 return val
131−newtype HStoreMap = HStoreMap (Map Text Text) deriving (Eq, Ord, Typeable, Show)
141+newtype HStoreMap = HStoreMap (Map Text Text) deriving (Eq, Ord, Typeable, Show)
133−instance ToField HStoreMap where
143+instance ToHStore HStoreMap where
134− toField (HStoreMap xs) = toField (Map.foldWithKey f mempty xs)
144+ toHStore (HStoreMap xs) = Map.foldWithKey f mempty xs
135 where f k v xs = hstore k v `mappend` xs
145 where f k v xs = hstore k v `mappend` xs
147+instance ToField HStoreMap where
148+ toField xs = toField (toHStore xs)
137instance FromField HStoreMap where
150instance FromField HStoreMap where
138 fromField f mdat = convert <$> fromField f mdat
151 fromField f mdat = convert <$> fromField f mdat
139 where convert (HStoreList xs) = HStoreMap (Map.fromList xs)
152 where convert (HStoreList xs) = HStoreMap (Map.fromList xs)