git.iko.soy/typeable/postgresql-simple
on master help sign in
Added functions from HStoreBuilder to Builder and lazy ByteString
995ded2Leon P Smith
src/Database/PostgreSQL/Simple/HStore.hs+7−5
@@ -8,14 +8,14 @@
8-- Portability: portable
8-- Portability: portable
9--
9--
10-- Parsers and printers for hstore, a extended type bundled with
10-- Parsers and printers for hstore, a extended type bundled with
11−-- PostgreSQL providing finite maps from text strings to text strings.
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
12+-- See <http://www.postgresql.org/docs/9.2/static/hstore.html> for more
13−-- information.
13+-- information.
14--
14--
15-- Note that in order to use this type, a database superuser must
15-- Note that in order to use this type, a database superuser must
16-- install it by running a sql script in the share directory. This
16-- 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
17+-- can be done on PostgreSQL 9.1 and later with the command
18−-- @CREATE EXTENSION hstore@. See
18+-- @CREATE EXTENSION hstore@. See
19-- <http://www.postgresql.org/docs/9.2/static/contrib.html> for more
19-- <http://www.postgresql.org/docs/9.2/static/contrib.html> for more
20-- information.
20-- information.
21--
21--
@@ -28,6 +28,8 @@ module Database.PostgreSQL.Simple.HStore
28 , HStoreText
28 , HStoreText
29 , ToHStoreText(..)
29 , ToHStoreText(..)
30 , hstore
30 , hstore
31+ , toBuilder
32+ , toLazyByteString
31 ) where
33 ) where
32
34
33import Database.PostgreSQL.Simple.HStore.Implementation
35import Database.PostgreSQL.Simple.HStore.Implementation
src/Database/PostgreSQL/Simple/HStore/Implementation.hs+16−6
@@ -16,7 +16,7 @@
16module Database.PostgreSQL.Simple.HStore.Implementation where
16module Database.PostgreSQL.Simple.HStore.Implementation where
17
17
18import Control.Applicative
18import Control.Applicative
19−import Blaze.ByteString.Builder
19+import Blaze.ByteString.Builder as Blaze
20 ( Builder, toLazyByteString, copyByteString )
20 ( Builder, toLazyByteString, copyByteString )
21import Blaze.ByteString.Builder.Char8 (fromChar)
21import Blaze.ByteString.Builder.Char8 (fromChar)
22import qualified Data.Attoparsec.ByteString as P
22import qualified Data.Attoparsec.ByteString as P
@@ -42,6 +42,16 @@ data HStoreBuilder
42 | Comma !Builder
42 | Comma !Builder
43 deriving (Typeable)
43 deriving (Typeable)
44
44
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+
45instance Monoid HStoreBuilder where
55instance Monoid HStoreBuilder where
46 mempty = Empty
56 mempty = Empty
47 mappend Empty x = x
57 mappend Empty x = x
@@ -94,7 +104,7 @@ hstore (toHStoreText -> (HStoreText key)) (toHStoreText -> (HStoreText val)) =
94
104
95instance ToField HStoreBuilder where
105instance ToField HStoreBuilder where
96 toField Empty = toField (BS.empty)
106 toField Empty = toField (BS.empty)
97− toField (Comma x) = toField (toLazyByteString x)
107+ toField (Comma x) = toField (Blaze.toLazyByteString x)
98
108
99newtype HStoreList = HStoreList [(Text,Text)] deriving (Typeable, Show)
109newtype HStoreList = HStoreList [(Text,Text)] deriving (Typeable, Show)
100
110
@@ -108,14 +118,14 @@ instance FromField HStoreList where
108 then returnError Incompatible f ""
118 then returnError Incompatible f ""
109 else case mdat of
119 else case mdat of
110 Nothing -> returnError UnexpectedNull f ""
120 Nothing -> returnError UnexpectedNull f ""
111− Just dat ->
121+ Just dat ->
112 case P.parseOnly parseHStore dat of
122 case P.parseOnly parseHStore dat of
113− Left err ->
123+ Left err ->
114 returnError ConversionFailed f err
124 returnError ConversionFailed f err
115− Right (Left err) ->
125+ Right (Left err) ->
116 returnError ConversionFailed f "unicode exception" <|>
126 returnError ConversionFailed f "unicode exception" <|>
117 conversionError err
127 conversionError err
118− Right (Right val) ->
128+ Right (Right val) ->
119 return val
129 return val
120
130
121newtype HStoreMap = HStoreMap (Map Text Text) deriving (Eq, Ord, Typeable, Show)
131newtype HStoreMap = HStoreMap (Map Text Text) deriving (Eq, Ord, Typeable, Show)