on master help sign in
Document Rel8.Statement.Insert
d4354b2Ollie Charles
src/Rel8/Statement/Insert.hs+22−1
@@ -42,20 +42,41 @@ import qualified Data.Text as Text ( pack )
42import Data.Text.Encoding ( encodeUtf8 )
42import Data.Text.Encoding ( encodeUtf8 )
43
43
44
44
45+-- | The constituent parts of a SQL @INSERT@ statement.
45type Insert :: Type -> Type
46type Insert :: Type -> Type
46data Insert a where
47data Insert a where
47 Insert :: (Selects names exprs, Inserts exprs inserts) =>
48 Insert :: (Selects names exprs, Inserts exprs inserts) =>
48 { into :: TableSchema names
49 { into :: TableSchema names
50+ -- ^ Which table to insert into.
49 , rows :: [inserts]
51 , rows :: [inserts]
52+ -- ^ The rows to insert.
50 , onConflict :: OnConflict
53 , onConflict :: OnConflict
54+ -- ^ What to do if the inserted rows conflict with data already in the
55+ -- table.
51 , returning :: Returning names a
56 , returning :: Returning names a
57+ -- ^ What information to return on completion.
52 }
58 }
53 -> Insert a
59 -> Insert a
54
60
55
61
62+-- | @OnConflict@ allows you to add an @ON CONFLICT@ clause to an @INSERT@
63+-- statement.
56−data OnConflict = Abort | DoNothing
64+data OnConflict
65+ = Abort -- ^ @ON CONFLICT ABORT@
66+ | DoNothing -- ^ @ON CONFLICT DO NOTHING@
57
67
58
68
69+-- | Run an @INSERT@ statement
70+--
71+-- >>> :{
72+-- insert c Insert
73+-- { into = authorSchema
74+-- , rows = [ lit Author{ authorName = "Gabriel Gonzales", authorId = AuthorId 4, authorUrl = Just "https://haskellforall.com" } ]
75+-- , onConflict = Abort
76+-- , returning = NumberOfRowsAffected
77+-- }
78+-- :}
79+-- 1
59insert :: Insert a -> Connection -> IO a
80insert :: Insert a -> Connection -> IO a
60insert Insert {into, rows, onConflict, returning} =
81insert Insert {into, rows, onConflict, returning} =
61 case (rows, returning) of
82 case (rows, returning) of