2{-# language NamedFieldPuns #-}
3{-# language ScopedTypeVariables #-}
4{-# language StandaloneKindSignatures #-}
5{-# language TypeApplications #-}
7module Rel8.Statement.Update
14import Control.Exception ( throwIO )
15import Control.Monad ( (>=>) )
16import Data.Kind ( Type )
20import Hasql.Connection ( Connection )
21import qualified Hasql.Decoders as Hasql
22import qualified Hasql.Encoders as Hasql
23import qualified Hasql.Session as Hasql
24import qualified Hasql.Statement as Hasql
27import qualified Opaleye.Internal.Manipulation as Opaleye
30import Data.Profunctor ( lmap )
33import Rel8.Expr ( Expr )
34import Rel8.Expr.Opaleye ( toColumn, toPrimExpr )
35import Rel8.Schema.Table ( TableSchema )
36import Rel8.Statement.Returning ( Returning( Projection, NumberOfRowsAffected ) )
37import Rel8.Table ( fromColumns, toColumns )
38import Rel8.Table.Insert ( toInsert )
39import Rel8.Table.Opaleye ( table, unpackspec )
40import Rel8.Table.Recontextualize ( Selects )
41import Rel8.Table.Serialize ( Serializable, parse )
44import qualified Data.Text as Text
45import Data.Text.Encoding ( encodeUtf8 )
48type Update :: Type -> Type
50 Update :: Selects names exprs =>
51 { target :: TableSchema names
52 , set :: exprs -> exprs
53 , updateWhere :: exprs -> Expr Bool
54 , returning :: Returning names a
59update :: Update a -> Connection -> IO a
60update Update {target, set, updateWhere, returning} =
62 NumberOfRowsAffected -> Hasql.run session >=> either throwIO pure
64 session = Hasql.statement () statement
65 statement = Hasql.Statement bytes params decode prepare
66 bytes = encodeUtf8 $ Text.pack sql
67 params = Hasql.noParams
68 decode = Hasql.rowsAffected
70 sql = Opaleye.arrangeUpdateSql target' set' where'
72 target' = lmap toInsert $ table $ toColumns <$> target
73 set' = toColumns . set . fromColumns
74 where' = toColumn . toPrimExpr . updateWhere . fromColumns
76 Projection project -> Hasql.run session >=> either throwIO pure
78 session = Hasql.statement () statement
79 statement = Hasql.Statement bytes params decode prepare
80 bytes = encodeUtf8 $ Text.pack sql
81 params = Hasql.noParams
82 decode = decoder project
85 Opaleye.arrangeUpdateReturningSql
92 target' = lmap toInsert $ table $ toColumns <$> target
93 set' = toColumns . set . fromColumns
94 where' = toColumn . toPrimExpr . updateWhere . fromColumns
95 project' = toColumns . project . fromColumns
98 decoder :: forall exprs projection a. Serializable projection a
99 => (exprs -> projection) -> Hasql.Result [a]
100 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