2{-# language NamedFieldPuns #-}
3{-# language ScopedTypeVariables #-}
4{-# language StandaloneKindSignatures #-}
5{-# language TypeApplications #-}
7module Rel8.Statement.Update
14import Control.Exception ( throwIO )
15import Data.Kind ( Type )
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
29import Data.Profunctor ( lmap )
32import Rel8.Expr ( Expr )
33import Rel8.Expr.Opaleye ( toColumn, toPrimExpr )
34import Rel8.Schema.Table ( TableSchema )
35import Rel8.Statement.Returning ( Returning( Projection, NumberOfRowsAffected ) )
36import Rel8.Table ( fromColumns, toColumns )
37import Rel8.Table.Insert ( toInsert )
38import Rel8.Table.Opaleye ( table, unpackspec )
39import Rel8.Table.Recontextualize ( Selects )
40import Rel8.Table.Serialize ( Serializable, parse )
43import qualified Data.Text as Text
44import Data.Text.Encoding ( encodeUtf8 )
47-- | The constituent parts of an @UPDATE@ statement.
48type Update :: Type -> Type
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.
63-- | Run an @UPDATE@ statement.
65-- >>> mapM_ print =<< select c (each projectSchema)
66-- Project {projectAuthorId = 1, projectName = "rel8"}
67-- Project {projectAuthorId = 2, projectName = "aeson"}
68-- Project {projectAuthorId = 2, projectName = "text"}
72-- { target = projectSchema
73-- , set = \p -> p { projectName = "Rel8!" }
74-- , updateWhere = \p -> projectName p ==. lit "rel8"
75-- , returning = NumberOfRowsAffected
80-- >>> mapM_ print =<< select c (each projectSchema)
81-- Project {projectAuthorId = 2, projectName = "aeson"}
82-- Project {projectAuthorId = 2, projectName = "text"}
83-- Project {projectAuthorId = 1, projectName = "Rel8!"}
84update :: Connection -> Update a -> IO a
85update c Update {target, set, updateWhere, returning} =
87 NumberOfRowsAffected -> Hasql.run session c >>= either throwIO pure
89 session = Hasql.statement () statement
90 statement = Hasql.Statement bytes params decode prepare
91 bytes = encodeUtf8 $ Text.pack sql
92 params = Hasql.noParams
93 decode = Hasql.rowsAffected
95 sql = Opaleye.arrangeUpdateSql target' set' where'
97 target' = lmap toInsert $ table $ toColumns <$> target
98 set' = toColumns . set . fromColumns
99 where' = toColumn . toPrimExpr . updateWhere . fromColumns
101 Projection project -> Hasql.run session c >>= either throwIO pure
103 session = Hasql.statement () statement
104 statement = Hasql.Statement bytes params decode prepare
105 bytes = encodeUtf8 $ Text.pack sql
106 params = Hasql.noParams
107 decode = decoder project
110 Opaleye.arrangeUpdateReturningSql
117 target' = lmap toInsert $ table $ toColumns <$> target
118 set' = toColumns . set . fromColumns
119 where' = toColumn . toPrimExpr . updateWhere . fromColumns
120 project' = toColumns . project . fromColumns
123 decoder :: forall exprs projection a. Serializable projection a
124 => (exprs -> projection) -> Hasql.Result [a]
125 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