git.iko.soy/typeable/postgresql-simple
on master help sign in
Added functions from HStoreBuilder to Builder and lazy ByteString
commit995ded2177fb43565287c1804461bb2c17e33aaa authorLeon P Smith <leon@melding-monads.com> dateThu 25 Apr 2013, 18:49 (14 years ago) parent2dddc2b
2 files changed+23−11
src/Database/PostgreSQL/Simple/HStore.hs+7−5 src/Database/PostgreSQL/Simple/HStore/Implementation.hs+16−6
src/Database/PostgreSQL/Simple/HStore.hs+7−5
@@ -8,14 +8,14 @@
88-- Portability: portable
99--
1010-- Parsers and printers for hstore, a extended type bundled with
11−-- PostgreSQL providing finite maps from text strings to text strings.
12−-- See <http://www.postgresql.org/docs/9.2/static/hstore.html> for more
13−-- information.
11+-- PostgreSQL providing finite maps from text strings to text strings.
12+-- See <http://www.postgresql.org/docs/9.2/static/hstore.html> for more
13+-- information.
1414--
1515-- Note that in order to use this type, a database superuser must
1616-- install it by running a sql script in the share directory. This
17−-- can be done on PostgreSQL 9.1 and later with the command
18−-- @CREATE EXTENSION hstore@. See
17+-- can be done on PostgreSQL 9.1 and later with the command
18+-- @CREATE EXTENSION hstore@. See
1919-- <http://www.postgresql.org/docs/9.2/static/contrib.html> for more
2020-- information.
2121--
@@ -28,6 +28,8 @@ module Database.PostgreSQL.Simple.HStore
2828 , HStoreText
2929 , ToHStoreText(..)
3030 , hstore
31+ , toBuilder
32+ , toLazyByteString
3133 ) where
3234
3335import Database.PostgreSQL.Simple.HStore.Implementation
src/Database/PostgreSQL/Simple/HStore/Implementation.hs+16−6
@@ -16,7 +16,7 @@
1616module Database.PostgreSQL.Simple.HStore.Implementation where
1717
1818import Control.Applicative
19−import Blaze.ByteString.Builder
19+import Blaze.ByteString.Builder as Blaze
2020 ( Builder, toLazyByteString, copyByteString )
2121import Blaze.ByteString.Builder.Char8 (fromChar)
2222import qualified Data.Attoparsec.ByteString as P
@@ -42,6 +42,16 @@ data HStoreBuilder
4242 | Comma !Builder
4343 deriving (Typeable)
4444
45+toBuilder :: HStoreBuilder -> Builder
46+toBuilder x = case x of
47+ Empty -> mempty
48+ Comma x -> x
49+
50+toLazyByteString :: HStoreBuilder -> BL.ByteString
51+toLazyByteString x = case x of
52+ Empty -> BL.empty
53+ Comma x -> Blaze.toLazyByteString x
54+
4555instance Monoid HStoreBuilder where
4656 mempty = Empty
4757 mappend Empty x = x
@@ -94,7 +104,7 @@ hstore (toHStoreText -> (HStoreText key)) (toHStoreText -> (HStoreText val)) =
94104
95105instance ToField HStoreBuilder where
96106 toField Empty = toField (BS.empty)
97− toField (Comma x) = toField (toLazyByteString x)
107+ toField (Comma x) = toField (Blaze.toLazyByteString x)
98108
99109newtype HStoreList = HStoreList [(Text,Text)] deriving (Typeable, Show)
100110
@@ -108,14 +118,14 @@ instance FromField HStoreList where
108118 then returnError Incompatible f ""
109119 else case mdat of
110120 Nothing -> returnError UnexpectedNull f ""
111− Just dat ->
121+ Just dat ->
112122 case P.parseOnly parseHStore dat of
113− Left err ->
123+ Left err ->
114124 returnError ConversionFailed f err
115− Right (Left err) ->
125+ Right (Left err) ->
116126 returnError ConversionFailed f "unicode exception" <|>
117127 conversionError err
118− Right (Right val) ->
128+ Right (Right val) ->
119129 return val
120130
121131newtype HStoreMap = HStoreMap (Map Text Text) deriving (Eq, Ord, Typeable, Show)