1{-# language DuplicateRecordFields #-}
3{-# language NamedFieldPuns #-}
4{-# language RecordWildCards #-}
5{-# language StandaloneKindSignatures #-}
6{-# language StrictData #-}
8module Rel8.Statement.Update
16import Control.Exception ( throwIO )
17import Data.Kind ( Type )
21import Hasql.Connection ( Connection )
22import qualified Hasql.Encoders as Hasql
23import qualified Hasql.Session as Hasql
24import qualified Hasql.Statement as Hasql
27import Text.PrettyPrint ( Doc, (<+>), ($$), text )
30import Rel8.Expr ( Expr )
31import Rel8.Query ( Query )
32import Rel8.Schema.Name ( Selects )
33import Rel8.Schema.Table ( TableSchema(..), ppTable )
34import Rel8.Statement.Returning
36 , decodeReturning, emptyReturning, ppReturning
38import Rel8.Statement.Set ( ppSet )
39import Rel8.Statement.Using ( ppFrom )
40import Rel8.Statement.Where ( ppWhere )
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.
54 -- ^ @FROM@ clause — this can be used to join against other tables,
55 -- and its results can be referenced in the @SET@ and @WHERE@ clauses.
56 , set :: from -> exprs -> exprs
57 -- ^ How to update each selected row.
58 , updateWhere :: from -> exprs -> Expr Bool
59 -- ^ Which rows to select for update.
60 , returning :: Returning names a
61 -- ^ What to return from the @UPDATE@ statement.
66ppUpdate :: Update a -> Maybe Doc
67ppUpdate Update {..} = do
68 (fromDoc, i) <- ppFrom from
72 ppSet target (set i) $$
74 ppWhere target (updateWhere i) $$
75 ppReturning target returning
78-- | Run an @UPDATE@ statement.
79update :: Connection -> Update a -> IO a
80update connection u@Update {returning} =
81 case show <$> ppUpdate u of
82 Nothing -> pure (emptyReturning returning)
84 Hasql.run session connection >>= either throwIO pure
86 session = Hasql.statement () statement
87 statement = Hasql.Statement bytes params decode prepare
88 bytes = encodeUtf8 $ Text.pack sql
89 params = Hasql.noParams
90 decode = decodeReturning returning
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