Merge pull request #73 from axman6/add-insertmany-example

Add documentation on how to perform an insert or multiple values.
This commit is contained in:
Nikita Volkov 2017-04-25 10:41:45 +03:00 committed by GitHub
commit 8458526e4e

View File

@ -39,6 +39,8 @@ module Hasql.Encoders
arrayValue, arrayValue,
arrayNullableValue, arrayNullableValue,
arrayDimension, arrayDimension,
-- ** Insert Many
-- $insertMany
) )
where where
@ -533,3 +535,26 @@ arrayDimension :: (forall a. (a -> b -> a) -> a -> c -> a) -> Array b -> Array c
arrayDimension foldl (Array imp) = arrayDimension foldl (Array imp) =
Array (Array.dimension foldl imp) Array (Array.dimension foldl imp)
-- $insertMany
-- It is not currently possible to pass in an array of encodable values
-- to use in an 'insert many' query using Hasql. Instead, PostgreSQL's
-- (9.4 or later) `unnest` function can be used to in an analogous way
-- to haskell's `zip` function by passing in multiple arrays of values
-- to be zipped into the rows we want to insert:
--
-- @
-- insertMultipleLocations :: Query (Vector (UUID, Double, Double)) ()
-- insertMultipleLocations =
-- statement sql encoder decoder True
-- where
-- sql =
-- "insert into location (id, x, y) select * from unnest ($1, $2, $3)"
-- encoder =
-- contramap Vector.unzip3 $
-- contrazip3 (vector Encoders.uuid) (vector Encoders.float8) (vector Encoders.float8)
-- where
-- vector value =
-- Encoders.value (Encoders.array (Encoders.arrayDimension foldl' (Encoders.arrayValue value)))
-- decoder =
-- Decoders.unit
-- @