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 ( castTable, 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
42insert :: Connection -> Insert a -> IO a
43insert c Insert {into, rows, onConflict, returning} =
44 case (rows, returning) of
45 ([], NumberOfRowsAffected) -> pure 0
46 ([], Projection _) -> pure []
48 (x:xs, NumberOfRowsAffected) -> Hasql.run session c >>= either throwIO pure
50 session = Hasql.statement () statement
51 statement = Hasql.Statement bytes params decode prepare
52 bytes = encodeUtf8 $ Text.pack sql
53 params = Hasql.noParams
54 decode = Hasql.rowsAffected
56 sql = Opaleye.arrangeInsertManySql into' rows' onConflict'
58 into' = table $ toColumns <$> into
59 rows' = toColumns <$> x :| xs
61 (x:xs, Projection project) -> Hasql.run session c >>= either throwIO pure
63 session = Hasql.statement () statement
64 statement = Hasql.Statement bytes params decode prepare
65 bytes = encodeUtf8 $ Text.pack sql
66 params = Hasql.noParams
67 decode = decoder project
70 Opaleye.arrangeInsertManyReturningSql
77 into' = table $ toColumns <$> into
78 rows' = toColumns <$> x :| xs
79 project' = castTable . toColumns . project . fromColumns
84 DoNothing -> Just Opaleye.DoNothing
87 decoder :: forall exprs projection a. Serializable projection a
88 => (exprs -> projection) -> Hasql.Result [a]
89 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