2{-# language NamedFieldPuns #-}
3{-# language ScopedTypeVariables #-}
4{-# language TypeApplications #-}
6module Rel8.Statement.Insert
14import Control.Exception ( throwIO )
15import Data.List.NonEmpty ( NonEmpty( (:|) ) )
19import Hasql.Connection ( Connection )
20import qualified Hasql.Decoders as Hasql
21import qualified Hasql.Encoders as Hasql
22import qualified Hasql.Session as Hasql
23import qualified Hasql.Statement as Hasql
26import qualified Opaleye.Internal.Manipulation as Opaleye
27import qualified Opaleye.Manipulation as Opaleye
30import Rel8.Schema.Insert ( Insert(..), OnConflict(..) )
31import Rel8.Statement.Returning ( Returning( Projection, NumberOfRowsAffected ) )
32import Rel8.Table ( fromColumns, toColumns )
33import Rel8.Table.Opaleye ( table, unpackspec )
34import Rel8.Table.Serialize ( Serializable, parse )
37import qualified Data.Text as Text ( pack )
38import Data.Text.Encoding ( encodeUtf8 )
41-- | Run an @INSERT@ statement
45-- { into = authorSchema
46-- , rows = [ lit Author{ authorName = "Gabriel Gonzales", authorId = AuthorId 4, authorUrl = Just "https://haskellforall.com" } ]
47-- , onConflict = Abort
48-- , returning = NumberOfRowsAffected
52insert :: Connection -> Insert a -> IO a
53insert c Insert {into, rows, onConflict, returning} =
54 case (rows, returning) of
55 ([], NumberOfRowsAffected) -> pure 0
56 ([], Projection _) -> pure []
58 (x:xs, NumberOfRowsAffected) -> Hasql.run session c >>= either throwIO pure
60 session = Hasql.statement () statement
61 statement = Hasql.Statement bytes params decode prepare
62 bytes = encodeUtf8 $ Text.pack sql
63 params = Hasql.noParams
64 decode = Hasql.rowsAffected
66 sql = Opaleye.arrangeInsertManySql into' rows' onConflict'
68 into' = table $ toColumns <$> into
69 rows' = toColumns <$> x :| xs
71 (x:xs, Projection project) -> Hasql.run session c >>= either throwIO pure
73 session = Hasql.statement () statement
74 statement = Hasql.Statement bytes params decode prepare
75 bytes = encodeUtf8 $ Text.pack sql
76 params = Hasql.noParams
77 decode = decoder project
80 Opaleye.arrangeInsertManyReturningSql
87 into' = table $ toColumns <$> into
88 rows' = toColumns <$> x :| xs
89 project' = toColumns . project . fromColumns
94 DoNothing -> Just Opaleye.DoNothing
97 decoder :: forall exprs projection a. Serializable projection a
98 => (exprs -> projection) -> Hasql.Result [a]
99 decoder _ = Hasql.rowList (parse @projection @a)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list