Skip to content

Commit 6361568

Browse files
authored
Merge pull request #224 from bgamari/master
Compatibility with Semigroup/Monoid proposal
2 parents 9033bcb + 44c0bb8 commit 6361568

3 files changed

Lines changed: 23 additions & 9 deletions

File tree

postgresql-simple.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Library
7373
transformers,
7474
uuid-types >= 1.0.0,
7575
scientific,
76+
semigroups,
7677
vector
7778

7879
if !impl(ghc >= 7.6)

src/Database/PostgreSQL/Simple/HStore/Implementation.hs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import Data.Text.Encoding.Error(UnicodeException)
3434
import qualified Data.Text.Lazy as TL
3535
import Data.Typeable
3636
import Data.Monoid(Monoid(..))
37+
import Data.Semigroup
3738
import Database.PostgreSQL.Simple.FromField
3839
import Database.PostgreSQL.Simple.ToField
3940

@@ -59,19 +60,24 @@ toLazyByteString x = case x of
5960
Empty -> BL.empty
6061
Comma x -> BU.toLazyByteString x
6162

62-
instance Monoid HStoreBuilder where
63-
mempty = Empty
64-
mappend Empty x = x
65-
mappend (Comma a) x
63+
instance Semigroup HStoreBuilder where
64+
Empty <> x = x
65+
Comma a <> x
6666
= Comma (a `mappend` case x of
6767
Empty -> mempty
6868
Comma b -> char8 ',' `mappend` b)
6969

70+
instance Monoid HStoreBuilder where
71+
mempty = Empty
72+
#if !(MIN_VERSION_base(4,11,0))
73+
mappend = (<>)
74+
#endif
75+
7076
class ToHStoreText a where
7177
toHStoreText :: a -> HStoreText
7278

7379
-- | 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)
7581

7682
instance ToHStoreText HStoreText where
7783
toHStoreText = id

src/Database/PostgreSQL/Simple/Types.hs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{-# LANGUAGE DeriveDataTypeable, DeriveFunctor, GeneralizedNewtypeDeriving #-}
1+
{-# LANGUAGE CPP, DeriveDataTypeable, DeriveFunctor, GeneralizedNewtypeDeriving #-}
22

33
------------------------------------------------------------------------------
44
-- |
@@ -33,7 +33,9 @@ module Database.PostgreSQL.Simple.Types
3333
import Control.Arrow (first)
3434
import Data.ByteString (ByteString)
3535
import Data.Hashable (Hashable(hashWithSalt))
36+
import Data.Foldable (toList)
3637
import Data.Monoid (Monoid(..))
38+
import Data.Semigroup
3739
import Data.String (IsString(..))
3840
import Data.Typeable (Typeable)
3941
import Data.ByteString.Builder ( stringUtf8 )
@@ -88,11 +90,16 @@ instance Read Query where
8890
instance IsString Query where
8991
fromString = Query . toByteString . stringUtf8
9092

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+
9198
instance Monoid Query where
9299
mempty = Query B.empty
93-
mappend (Query a) (Query b) = Query (B.append a b)
94-
{-# INLINE mappend #-}
95-
mconcat xs = Query (B.concat (map fromQuery xs))
100+
#if !(MIN_VERSION_base(4,11,0))
101+
mappend = (<>)
102+
#endif
96103

97104
-- | Wrap a list of values for use in an @IN@ clause. Replaces a
98105
-- single \"@?@\" character with a parenthesized list of rendered

0 commit comments

Comments
 (0)