Skip to content

Add some new row instances and remove some dead code. #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion postgresql-simple.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Cabal-version: >= 1.9.2

extra-source-files:
CONTRIBUTORS
CHANGELOG.md
CHANGES.md

Library
hs-source-dirs: src
Expand Down
14 changes: 0 additions & 14 deletions src/Database/PostgreSQL/Simple/Compat.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,9 @@ import Unsafe.Coerce (unsafeCoerce)
-- 'withTransactionMode' function calls the restore callback only once, so we
-- don't need that polymorphism.
mask :: ((IO a -> IO a) -> IO b) -> IO b
#if MIN_VERSION_base(4,3,0)
mask io = E.mask $ \restore -> io restore
#else
mask io = do
b <- E.blocked
E.block $ io $ \m -> if b then m else E.unblock m
#endif
{-# INLINE mask #-}

#if !MIN_VERSION_base(4,5,0)
infixr 6 <>

(<>) :: Monoid m => m -> m -> m
(<>) = mappend
{-# INLINE (<>) #-}
#endif

toByteString :: Builder -> ByteString
#if MIN_VERSION_bytestring(0,10,0)
toByteString x = toStrict (toLazyByteString x)
Expand Down
31 changes: 29 additions & 2 deletions src/Database/PostgreSQL/Simple/FromRow.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RecordWildCards, FlexibleInstances, DefaultSignatures #-}

Expand Down Expand Up @@ -36,6 +37,11 @@ import Control.Monad.Trans.Reader
import Control.Monad.Trans.Class
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as B
#if MIN_VERSION_base(4,9,0)
import Data.Functor.Const
import Data.Functor.Identity
import qualified Data.List.NonEmpty as NE
#endif
import Data.Vector (Vector)
import qualified Data.Vector as V
import Database.PostgreSQL.Simple.Types (Only(..))
Expand Down Expand Up @@ -108,8 +114,8 @@ fieldWith fieldP = RP $ do
""
("at least " ++ show (unCol column + 1)
++ " slots in target type")
"mismatch between number of columns to \
\convert and number in target type"
("mismatch between number of columns to convert and number"
++ "in target type")
conversionError err
else do
let !result = rowresult
Expand Down Expand Up @@ -141,6 +147,27 @@ instance (FromField a) => FromRow (Maybe (Only a)) where
fromRow = (null *> pure Nothing)
<|> (Just <$> fromRow)

#if MIN_VERSION_base(4,9,0)
instance (FromField a) => FromRow (Identity a) where
fromRow = Identity <$> field

instance (FromField a) => FromRow (Const a b) where
fromRow = Const <$> field

instance (FromField a) => FromRow (Maybe (Identity a)) where
fromRow = (null *> pure Nothing)
<|> (Just <$> fromRow)

instance (FromField a) => FromRow (Maybe (Const a b)) where
fromRow = (null *> pure Nothing)
<|> (Just <$> fromRow)

instance (FromField a) => FromRow (NE.NonEmpty a) where
fromRow = do
n <- numFieldsRemaining
(NE.:|) <$> field <*> replicateM (n - 1) field
#endif

instance (FromField a, FromField b) => FromRow (a,b) where
fromRow = (,) <$> field <*> field

Expand Down
26 changes: 25 additions & 1 deletion src/Database/PostgreSQL/Simple/ToRow.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE DefaultSignatures, FlexibleInstances, FlexibleContexts #-}
{-# LANGUAGE CPP, DefaultSignatures, FlexibleInstances, FlexibleContexts #-}
------------------------------------------------------------------------------
-- |
-- Module: Database.PostgreSQL.Simple.ToRow
Expand All @@ -23,6 +23,14 @@ module Database.PostgreSQL.Simple.ToRow

import Database.PostgreSQL.Simple.ToField (Action(..), ToField(..))
import Database.PostgreSQL.Simple.Types (Only(..), (:.)(..))
#if MIN_VERSION_base(4,9,0)
import Data.Functor.Const
import Data.Functor.Identity
import qualified Data.List.NonEmpty as NE
#endif
#if MIN_VERSION_base(4,7,0)
import Data.Proxy
#endif
import GHC.Generics

-- | A collection type that can be turned into a list of rendering
Expand All @@ -39,9 +47,25 @@ class ToRow a where
instance ToRow () where
toRow _ = []

#if MIN_VERSION_base(4,7,0)
instance ToRow (Proxy a) where
toRow _ = []
#endif

instance (ToField a) => ToRow (Only a) where
toRow (Only v) = [toField v]

#if MIN_VERSION_base(4,9,0)
instance (ToField a) => ToRow (Identity a) where
toRow (Identity v) = [toField v]

instance (ToField a) => ToRow (Const a b) where
toRow (Const v) = [toField v]

instance (ToField a) => ToRow (NE.NonEmpty a) where
toRow = toRow . NE.toList
#endif

instance (ToField a, ToField b) => ToRow (a,b) where
toRow (a,b) = [toField a, toField b]

Expand Down