1{-# language DuplicateRecordFields #-}
2{-# language FlexibleContexts #-}
4{-# language NamedFieldPuns #-}
5{-# language RecordWildCards #-}
6{-# language StandaloneKindSignatures #-}
7{-# language StrictData #-}
9module Rel8.Statement.Insert
18import Data.Foldable ( toList )
19import Data.Kind ( Type )
23import qualified Hasql.Encoders as Hasql
24import qualified Hasql.Statement as Hasql
27import qualified Opaleye.Internal.HaskellDB.Sql.Print as Opaleye
30import Text.PrettyPrint ( Doc, (<+>), ($$), parens, text )
33import Rel8.Query ( Query )
34import Rel8.Schema.Name ( Name, Selects, ppColumn )
35import Rel8.Schema.Table ( TableSchema(..), ppTable )
36import Rel8.Statement.OnConflict ( OnConflict, ppOnConflict )
37import Rel8.Statement.Returning ( Returning, decodeReturning, ppReturning )
38import Rel8.Statement.Select ( ppSelect )
39import Rel8.Table ( Table )
40import Rel8.Table.Name ( showNames )
43import qualified Data.Text as Text ( pack )
44import Data.Text.Encoding ( encodeUtf8 )
47-- | The constituent parts of a SQL @INSERT@ statement.
48type Insert :: Type -> Type
50 Insert :: Selects names exprs =>
51 { into :: TableSchema names
52 -- ^ Which table to insert into.
54 -- ^ The rows to insert. This can be an arbitrary query — use
55 -- 'Rel8.values' insert a static list of rows.
56 , onConflict :: OnConflict names
57 -- ^ What to do if the inserted rows conflict with data already in the
59 , returning :: Returning names a
60 -- ^ What information to return on completion.
65ppInsert :: Insert a -> Doc
67 text "INSERT INTO" <+>
70 ppOnConflict into onConflict $$
71 ppReturning into returning
74ppInto :: Table Name a => TableSchema a -> Doc
75ppInto table@TableSchema {columns} =
77 parens (Opaleye.commaV ppColumn (toList (showNames columns)))
80-- | Run an 'Insert' statement.
81insert :: Insert a -> Hasql.Statement () a
82insert i@Insert {returning} = Hasql.Statement bytes params decode prepare
84 bytes = encodeUtf8 $ Text.pack sql
85 params = Hasql.noParams
86 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