git.iko.soy/typeable/postgresql-simple
on master help sign in
Added ToHStore class
a39dd86Leon P Smith
src/Database/PostgreSQL/Simple/HStore.hs+4−3
@@ -24,12 +24,13 @@
24module Database.PostgreSQL.Simple.HStore
24module Database.PostgreSQL.Simple.HStore
25 ( HStoreList(..)
25 ( HStoreList(..)
26 , HStoreMap(..)
26 , HStoreMap(..)
27+ , ToHStore(..)
27 , HStoreBuilder
28 , HStoreBuilder
28− , HStoreText
29− , ToHStoreText(..)
30− , hstore
31 , toBuilder
29 , toBuilder
32 , toLazyByteString
30 , toLazyByteString
31+ , hstore
32+ , ToHStoreText(..)
33+ , HStoreText
33 ) where
34 ) where
34
35
35import Database.PostgreSQL.Simple.HStore.Implementation
36import Database.PostgreSQL.Simple.HStore.Implementation
src/Database/PostgreSQL/Simple/HStore/Implementation.hs+17−4
@@ -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
39
39
40+class ToHStore a where
41+ toHStore :: a -> HStoreBuilder
42+
43+-- | Represents valid hstore syntax.
40data HStoreBuilder
44data HStoreBuilder
41 = Empty
45 = Empty
42 | Comma !Builder
46 | Comma !Builder
43 deriving (Typeable)
47 deriving (Typeable)
44
48
49+instance ToHStore HStoreBuilder where
50+ toHStore = id
51+
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
108
115
109newtype HStoreList = HStoreList [(Text,Text)] deriving (Typeable, Show)
116newtype HStoreList = HStoreList [(Text,Text)] deriving (Typeable, Show)
110
117
118+instance ToHStore HStoreList where
119+ toHStore (HStoreList xs) = mconcat (map (uncurry hstore) xs)
120+
111instance ToField HStoreList where
121instance ToField HStoreList where
112− toField (HStoreList xs) = toField (mconcat (map (uncurry hstore) xs))
122+ toField xs = toField (toHStore xs)
113
123
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
130
140
131−newtype HStoreMap = HStoreMap (Map Text Text) deriving (Eq, Ord, Typeable, Show)
141+newtype HStoreMap = HStoreMap (Map Text Text) deriving (Eq, Ord, Typeable, Show)
132
142
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
136
146
147+instance ToField HStoreMap where
148+ toField xs = toField (toHStore xs)
149+
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)