on db148ff help sign in
rel8/src/Rel8/Statement/Update.hs
105 lines 3.3 kB history raw
1{-# language GADTs #-}
2{-# language NamedFieldPuns #-}
3{-# language ScopedTypeVariables #-}
4{-# language StandaloneKindSignatures #-}
5{-# language TypeApplications #-}
7module Rel8.Statement.Update
8 ( Update(..)
9 , update
10 )
11where
13-- base
14import Control.Exception ( throwIO )
15import Data.Kind ( Type )
16import Prelude
18-- hasql
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
25-- opaleye
26import qualified Opaleye.Internal.Manipulation as Opaleye
28-- profunctors
29import Data.Profunctor ( lmap )
31-- rel8
32import Rel8.Expr ( Expr )
33import Rel8.Expr.Opaleye ( toColumn, toPrimExpr )
34import Rel8.Schema.Name ( Selects )
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 ( castTable, table, unpackspec )
40import Rel8.Table.Serialize ( Serializable, parse )
42-- text
43import qualified Data.Text as Text
44import Data.Text.Encoding ( encodeUtf8 )
47-- | The constituent parts of an @UPDATE@ statement.
48type Update :: Type -> Type
49data Update a where
50 Update :: Selects names exprs =>
51 { target :: TableSchema names
52 -- ^ Which table to update.
53 , set :: exprs -> exprs
54 -- ^ How to update each selected row.
55 , updateWhere :: exprs -> Expr Bool
56 -- ^ Which rows to select for update.
57 , returning :: Returning names a
58 -- ^ What to return from the @UPDATE@ statement.
59 }
60 -> Update a
63-- | Run an @UPDATE@ statement.
64update :: Connection -> Update a -> IO a
65update c Update {target, set, updateWhere, returning} =
66 case returning of
67 NumberOfRowsAffected -> Hasql.run session c >>= either throwIO pure
68 where
69 session = Hasql.statement () statement
70 statement = Hasql.Statement bytes params decode prepare
71 bytes = encodeUtf8 $ Text.pack sql
72 params = Hasql.noParams
73 decode = Hasql.rowsAffected
74 prepare = False
75 sql = Opaleye.arrangeUpdateSql target' set' where'
76 where
77 target' = lmap toInsert $ table $ toColumns <$> target
78 set' = toColumns . set . fromColumns
79 where' = toColumn . toPrimExpr . updateWhere . fromColumns
81 Projection project -> Hasql.run session c >>= either throwIO pure
82 where
83 session = Hasql.statement () statement
84 statement = Hasql.Statement bytes params decode prepare
85 bytes = encodeUtf8 $ Text.pack sql
86 params = Hasql.noParams
87 decode = decoder project
88 prepare = False
89 sql =
90 Opaleye.arrangeUpdateReturningSql
91 unpackspec
92 target'
93 set'
94 where'
95 project'
96 where
97 target' = lmap toInsert $ table $ toColumns <$> target
98 set' = toColumns . set . fromColumns
99 where' = toColumn . toPrimExpr . updateWhere . fromColumns
100 project' = castTable . toColumns . project . fromColumns
102 where
103 decoder :: forall exprs projection a. Serializable projection a
104 => (exprs -> projection) -> Hasql.Result [a]
105 decoder _ = Hasql.rowList (parse @projection @a)