git.iko.soy/typeable/postgresql-simple
on master help sign in
Borrow :. from ozataman

See commit cd76bea27a9c1c2c83a32ae11d5882f60836c2d1

commit8ea1c2fcda434784b9ddb3ed95db27ac3549b081 authorLeon P Smith <leon@melding-monads.com> dateFri 4 May 2012, 15:47 (14 years ago) parent9014819
3 files changed+29
postgresql-simple.cabal+1 src/Database/PostgreSQL/Simple/FromRow.hs+4 src/Database/PostgreSQL/Simple/Types.hs+24
postgresql-simple.cabal+1
@@ -52,6 +52,7 @@ Library
5252 vector
5353
5454 extensions: DoAndIfThenElse, OverloadedStrings, BangPatterns, ViewPatterns
55+ TypeOperators
5556
5657 ghc-options: -Wall -fno-warn-name-shadowing
5758
src/Database/PostgreSQL/Simple/FromRow.hs+4
@@ -33,6 +33,7 @@ import Database.PostgreSQL.Simple.Ok
3333import qualified Database.PostgreSQL.LibPQ as PQ
3434import Database.PostgreSQL.Simple.Internal
3535import Database.PostgreSQL.Simple.FromField
36+import Database.PostgreSQL.Simple.Types
3637
3738import Control.Monad.Trans.State.Strict
3839import Control.Monad.Trans.Reader
@@ -150,3 +151,6 @@ instance FromField a => FromRow [a] where
150151 fromRow = do
151152 n <- numFieldsRemaining
152153 replicateM n field
154+
155+instance (FromRow a, FromRow b) => FromRow (a :. b) where
156+ fromRow = (:.) <$> fromRow <*> fromRow
src/Database/PostgreSQL/Simple/Types.hs+24
@@ -1,5 +1,6 @@
11{-# LANGUAGE DeriveDataTypeable, DeriveFunctor, GeneralizedNewtypeDeriving #-}
22
3+------------------------------------------------------------------------------
34-- |
45-- Module: Database.PostgreSQL.Simple.Types
56-- Copyright: (c) 2011 MailRank, Inc.
@@ -10,6 +11,8 @@
1011-- Portability: portable
1112--
1213-- Basic types.
14+--
15+------------------------------------------------------------------------------
1316
1417module Database.PostgreSQL.Simple.Types
1518 (
@@ -19,6 +22,7 @@ module Database.PostgreSQL.Simple.Types
1922 , Binary(..)
2023 , Query(..)
2124 , Oid(..)
25+ , (:.)(..)
2226 ) where
2327
2428import Blaze.ByteString.Builder (toByteString)
@@ -106,3 +110,23 @@ newtype In a = In a
106110-- | Wrap a mostly-binary string to be escaped in hexadecimal.
107111newtype Binary a = Binary a
108112 deriving (Eq, Ord, Read, Show, Typeable, Functor)
113+
114+-- | A composite type to parse your custom data structures without
115+-- having to define dummy newtype wrappers every time.
116+--
117+--
118+-- > instance FromRow MyData where ...
119+--
120+-- > instance FromRow MyData2 where ...
121+--
122+--
123+-- then I can do the following for free:
124+--
125+-- @
126+-- res <- query' c "..."
127+-- forM res $ \\(MyData{..} :. MyData2{..}) -> do
128+-- ....
129+-- @
130+data h :. t = h :. t deriving (Eq,Ord,Show,Read,Typeable)
131+
132+infixr 3 :.