git.iko.soy/typeable/postgresql-simple
on master help sign in
Added ToHStore class
commita39dd868e346470320e2774a61414908926c8d7c authorLeon P Smith <leon@melding-monads.com> dateThu 25 Apr 2013, 19:06 (14 years ago) parent995ded2
2 files changed+21−7
src/Database/PostgreSQL/Simple/HStore.hs+4−3 src/Database/PostgreSQL/Simple/HStore/Implementation.hs+17−4
src/Database/PostgreSQL/Simple/HStore.hs+4−3
@@ -24,12 +24,13 @@
2424module Database.PostgreSQL.Simple.HStore
2525 ( HStoreList(..)
2626 , HStoreMap(..)
27+ , ToHStore(..)
2728 , HStoreBuilder
28− , HStoreText
29− , ToHStoreText(..)
30− , hstore
3129 , toBuilder
3230 , toLazyByteString
31+ , hstore
32+ , ToHStoreText(..)
33+ , HStoreText
3334 ) where
3435
3536import Database.PostgreSQL.Simple.HStore.Implementation
src/Database/PostgreSQL/Simple/HStore/Implementation.hs+17−4
@@ -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)