git.iko.soy/typeable/postgresql-simple
on master help sign in
Compatibility with Semigroup/Monoid proposal
44c0bb8Ben Gamari
postgresql-simple.cabal+1
@@ -73,6 +73,7 @@ Library
73 transformers,
73 transformers,
74 uuid-types >= 1.0.0,
74 uuid-types >= 1.0.0,
75 scientific,
75 scientific,
76+ semigroups,
76 vector
77 vector
77
78
78 if !impl(ghc >= 7.6)
79 if !impl(ghc >= 7.6)
src/Database/PostgreSQL/Simple/HStore/Implementation.hs+11−5
@@ -34,6 +34,7 @@ import Data.Text.Encoding.Error(UnicodeException)
34import qualified Data.Text.Lazy as TL
34import qualified Data.Text.Lazy as TL
35import Data.Typeable
35import Data.Typeable
36import Data.Monoid(Monoid(..))
36import Data.Monoid(Monoid(..))
37+import Data.Semigroup
37import Database.PostgreSQL.Simple.FromField
38import Database.PostgreSQL.Simple.FromField
38import Database.PostgreSQL.Simple.ToField
39import Database.PostgreSQL.Simple.ToField
39
40
@@ -59,19 +60,24 @@ toLazyByteString x = case x of
59 Empty -> BL.empty
60 Empty -> BL.empty
60 Comma x -> BU.toLazyByteString x
61 Comma x -> BU.toLazyByteString x
61
62
62−instance Monoid HStoreBuilder where
63+instance Semigroup HStoreBuilder where
63− mempty = Empty
64+ Empty <> x = x
64− mappend Empty x = x
65− mappend (Comma a) x
65+ Comma a <> x
66 = Comma (a `mappend` case x of
66 = Comma (a `mappend` case x of
67 Empty -> mempty
67 Empty -> mempty
68 Comma b -> char8 ',' `mappend` b)
68 Comma b -> char8 ',' `mappend` b)
69
69
70+instance Monoid HStoreBuilder where
71+ mempty = Empty
72+#if !(MIN_VERSION_base(4,11,0))
73+ mappend = (<>)
74+#endif
75+
70class ToHStoreText a where
76class ToHStoreText a where
71 toHStoreText :: a -> HStoreText
77 toHStoreText :: a -> HStoreText
72
78
73-- | Represents escape text, ready to be the key or value to a hstore value
79-- | Represents escape text, ready to be the key or value to a hstore value
74−newtype HStoreText = HStoreText Builder deriving (Typeable, Monoid)
80+newtype HStoreText = HStoreText Builder deriving (Typeable, Semigroup, Monoid)
75
81
76instance ToHStoreText HStoreText where
82instance ToHStoreText HStoreText where
77 toHStoreText = id
83 toHStoreText = id
src/Database/PostgreSQL/Simple/Types.hs+11−4
@@ -1,4 +1,4 @@
1−{-# LANGUAGE DeriveDataTypeable, DeriveFunctor, GeneralizedNewtypeDeriving #-}
1+{-# LANGUAGE CPP, DeriveDataTypeable, DeriveFunctor, GeneralizedNewtypeDeriving #-}
2
2
3------------------------------------------------------------------------------
3------------------------------------------------------------------------------
4-- |
4-- |
@@ -33,7 +33,9 @@ module Database.PostgreSQL.Simple.Types
33import Control.Arrow (first)
33import Control.Arrow (first)
34import Data.ByteString (ByteString)
34import Data.ByteString (ByteString)
35import Data.Hashable (Hashable(hashWithSalt))
35import Data.Hashable (Hashable(hashWithSalt))
36+import Data.Foldable (toList)
36import Data.Monoid (Monoid(..))
37import Data.Monoid (Monoid(..))
38+import Data.Semigroup
37import Data.String (IsString(..))
39import Data.String (IsString(..))
38import Data.Typeable (Typeable)
40import Data.Typeable (Typeable)
39import Data.ByteString.Builder ( stringUtf8 )
41import Data.ByteString.Builder ( stringUtf8 )
@@ -88,11 +90,16 @@ instance Read Query where
88instance IsString Query where
90instance IsString Query where
89 fromString = Query . toByteString . stringUtf8
91 fromString = Query . toByteString . stringUtf8
90
92
93+instance Semigroup Query where
94+ Query a <> Query b = Query (B.append a b)
95+ {-# INLINE (<>) #-}
96+ sconcat xs = Query (B.concat $ map fromQuery $ toList xs)
97+
91instance Monoid Query where
98instance Monoid Query where
92 mempty = Query B.empty
99 mempty = Query B.empty
93− mappend (Query a) (Query b) = Query (B.append a b)
100+#if !(MIN_VERSION_base(4,11,0))
94− {-# INLINE mappend #-}
101+ mappend = (<>)
95− mconcat xs = Query (B.concat (map fromQuery xs))
102+#endif
96
103
97-- | Wrap a list of values for use in an @IN@ clause. Replaces a
104-- | Wrap a list of values for use in an @IN@ clause. Replaces a
98-- single \"@?@\" character with a parenthesized list of rendered
105-- single \"@?@\" character with a parenthesized list of rendered