git.iko.soy/typeable/postgresql-simple
on master help sign in
Compatibility with Semigroup/Monoid proposal
commit44c0bb8dec3b71e8daefe104cf643c0c4fb26768 authorBen Gamari <ben@smart-cactus.org> dateSat 16 Sep 2017, 23:27 (9 years ago) parent9033bcb
3 files changed+23−9
postgresql-simple.cabal+1 src/Database/PostgreSQL/Simple/HStore/Implementation.hs+11−5 src/Database/PostgreSQL/Simple/Types.hs+11−4
postgresql-simple.cabal+1
@@ -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+11−5
@@ -34,6 +34,7 @@ import Data.Text.Encoding.Error(UnicodeException)
3434import qualified Data.Text.Lazy as TL
3535import Data.Typeable
3636import Data.Monoid(Monoid(..))
37+import Data.Semigroup
3738import Database.PostgreSQL.Simple.FromField
3839import 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+
7076class 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
7682instance ToHStoreText HStoreText where
7783 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 #-}
22
33------------------------------------------------------------------------------
44-- |
@@ -33,7 +33,9 @@ module Database.PostgreSQL.Simple.Types
3333import Control.Arrow (first)
3434import Data.ByteString (ByteString)
3535import Data.Hashable (Hashable(hashWithSalt))
36+import Data.Foldable (toList)
3637import Data.Monoid (Monoid(..))
38+import Data.Semigroup
3739import Data.String (IsString(..))
3840import Data.Typeable (Typeable)
3941import Data.ByteString.Builder ( stringUtf8 )
@@ -88,11 +90,16 @@ instance Read Query where
8890instance 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+
9198instance 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