@@ -1,4 +1,6 @@
1+{-# LANGUAGE FlexibleContexts #-}
1−{-# LANGUAGE RecordWildCards, FlexibleInstances #-}
2+{-# LANGUAGE RecordWildCards, FlexibleInstances, DefaultSignatures #-}
3------------------------------------------------------------------------------
5------------------------------------------------------------------------------
@@ -27,7 +29,7 @@ module Database.PostgreSQL.Simple.FromRow
29import Prelude hiding (null)
31import Prelude hiding (null)
30−import Control.Applicative (Applicative(..), (<$>), (<|>), (*>))
32+import Control.Applicative (Applicative(..), (<$>), (<|>), (*>), liftA2)
31import Control.Monad (replicateM, replicateM_)
33import Control.Monad (replicateM, replicateM_)
32import Control.Monad.Trans.State.Strict
34import Control.Monad.Trans.State.Strict
33import Control.Monad.Trans.Reader
35import Control.Monad.Trans.Reader
@@ -45,6 +47,9 @@ import Database.PostgreSQL.Simple.Ok
45import Database.PostgreSQL.Simple.Types ((:.)(..), Null)
47import Database.PostgreSQL.Simple.Types ((:.)(..), Null)
46import Database.PostgreSQL.Simple.TypeInfo
48import Database.PostgreSQL.Simple.TypeInfo
48-- | A collection type that can be converted from a sequence of fields.
53-- | A collection type that can be converted from a sequence of fields.
49-- Instances are provided for tuples up to 10 elements and lists of any length.
54-- Instances are provided for tuples up to 10 elements and lists of any length.
@@ -68,6 +73,8 @@ import Database.PostgreSQL.Simple.TypeInfo
69class FromRow a where
74class FromRow a where
70 fromRow :: RowParser a
75 fromRow :: RowParser a
76+ default fromRow :: (Generic a, GFromRow (Rep a)) => RowParser a
77+ fromRow = to <$> gfromRow
72getvalue :: PQ.Result -> PQ.Row -> PQ.Column -> Maybe ByteString
79getvalue :: PQ.Result -> PQ.Row -> PQ.Column -> Maybe ByteString
73getvalue result row col = unsafeDupablePerformIO (PQ.getvalue' result row col)
80getvalue result row col = unsafeDupablePerformIO (PQ.getvalue' result row col)
@@ -252,3 +259,21 @@ instance FromField a => FromRow (Maybe (Vector a)) where
253instance (FromRow a, FromRow b) => FromRow (a :. b) where
260instance (FromRow a, FromRow b) => FromRow (a :. b) where
254 fromRow = (:.) <$> fromRow <*> fromRow
261 fromRow = (:.) <$> fromRow <*> fromRow
265+-- Type class for default implementation of FromRow using generics
266+class GFromRow f where
267+ gfromRow :: RowParser (f p)
269+instance GFromRow f => GFromRow (M1 c i f) where
270+ gfromRow = M1 <$> gfromRow
272+instance (GFromRow f, GFromRow g) => GFromRow (f :*: g) where
273+ gfromRow = liftA2 (:*:) gfromRow gfromRow
275+instance (FromField a) => GFromRow (K1 R a) where
276+ gfromRow = K1 <$> field
278+instance GFromRow U1 where