@@ -32,6 +32,7 @@ module Database.PostgreSQL.Simple.Copy
34 , CopyOutResult(..)
34 , CopyOutResult(..)
35 , getCopyData
36 , getCopyData
36 , putCopyData
37 , putCopyData
37 , putCopyEnd
38 , putCopyEnd
@@ -104,6 +105,30 @@ data CopyOutResult
104 deriving (Eq, Typeable, Show)
105 deriving (Eq, Typeable, Show)
108+-- | Fold over @COPY TO STDOUT@ query passing each copied row to an accumulator
109+-- and calling a post-process at the end. A connection must be in the
110+-- @CopyOut@ state in order to call this function.
114+-- > (acc, count) <- foldCopyData conn
115+-- > (\acc row -> return (row:acc))
116+-- > (\acc count -> return (acc, count))
120+ :: Connection -- ^ Database connection
121+ -> (a -> B.ByteString -> IO a) -- ^ Accumulate one row of the result
122+ -> (a -> Int64 -> IO b) -- ^ Post-process accumulator with a count of rows
123+ -> a -- ^ Initial accumulator
125+foldCopyData conn f g !acc = do
126+ result <- getCopyData conn
128+ CopyOutRow row -> f acc row >>= foldCopyData conn f g
129+ CopyOutDone count -> g acc count
107-- | Retrieve some data from a @COPY TO STDOUT@ query. A connection
132-- | Retrieve some data from a @COPY TO STDOUT@ query. A connection
108-- must be in the @CopyOut@ state in order to call this function. If this
133-- must be in the @CopyOut@ state in order to call this function. If this
109-- returns a 'CopyOutRow', the connection remains in the @CopyOut@ state,
134-- returns a 'CopyOutRow', the connection remains in the @CopyOut@ state,