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 )
48-- | The constituent parts of an @UPDATE@ statement.
49type Update :: Type -> Type
51 Update :: Selects names exprs =>
52 { target :: TableSchema names
53 -- ^ Which table to update.
54 , set :: exprs -> exprs
55 -- ^ How to update each selected row.
56 , updateWhere :: exprs -> Expr Bool
57 -- ^ Which rows to select for update.
58 , returning :: Returning names a
59 -- ^ What to return from the @UPDATE@ statement.
64-- | Run an @UPDATE@ statement.
66-- >>> mapM_ print =<< select c (each projectSchema)
67-- Project {projectAuthorId = 1, projectName = "rel8"}
68-- Project {projectAuthorId = 2, projectName = "aeson"}
69-- Project {projectAuthorId = 2, projectName = "text"}
73-- { target = projectSchema
74-- , set = \p -> p { projectName = "Rel8!" }
75-- , updateWhere = \p -> projectName p ==. lit "rel8"
76-- , returning = NumberOfRowsAffected
81-- >>> mapM_ print =<< select c (each projectSchema)
82-- Project {projectAuthorId = 2, projectName = "aeson"}
83-- Project {projectAuthorId = 2, projectName = "text"}
84-- Project {projectAuthorId = 1, projectName = "Rel8!"}
85update :: Update a -> Connection -> IO a
86update Update {target, set, updateWhere, returning} =
88 NumberOfRowsAffected -> Hasql.run session >=> either throwIO pure
90 session = Hasql.statement () statement
91 statement = Hasql.Statement bytes params decode prepare
92 bytes = encodeUtf8 $ Text.pack sql
93 params = Hasql.noParams
94 decode = Hasql.rowsAffected
96 sql = Opaleye.arrangeUpdateSql target' set' where'
98 target' = lmap toInsert $ table $ toColumns <$> target
99 set' = toColumns . set . fromColumns
100 where' = toColumn . toPrimExpr . updateWhere . fromColumns
102 Projection project -> Hasql.run session >=> either throwIO pure
104 session = Hasql.statement () statement
105 statement = Hasql.Statement bytes params decode prepare
106 bytes = encodeUtf8 $ Text.pack sql
107 params = Hasql.noParams
108 decode = decoder project
111 Opaleye.arrangeUpdateReturningSql
118 target' = lmap toInsert $ table $ toColumns <$> target
119 set' = toColumns . set . fromColumns
120 where' = toColumn . toPrimExpr . updateWhere . fromColumns
121 project' = toColumns . project . fromColumns
124 decoder :: forall exprs projection a. Serializable projection a
125 => (exprs -> projection) -> Hasql.Result [a]
126 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