on master help sign in
Document Rel8.Statement.Insert
commitd4354b2a0cb1391252027fb309e545d88e21a69f authorOllie Charles <ollie@ocharles.org.uk> dateTue 23 Mar 2021, 16:17 (6 years ago) parent7aa4f92
1 file changed+22−1
src/Rel8/Statement/Insert.hs+22−1
src/Rel8/Statement/Insert.hs+22−1
@@ -42,20 +42,41 @@ import qualified Data.Text as Text ( pack )
4242import Data.Text.Encoding ( encodeUtf8 )
4343
4444
45+-- | The constituent parts of a SQL @INSERT@ statement.
4546type Insert :: Type -> Type
4647data Insert a where
4748 Insert :: (Selects names exprs, Inserts exprs inserts) =>
4849 { into :: TableSchema names
50+ -- ^ Which table to insert into.
4951 , rows :: [inserts]
52+ -- ^ The rows to insert.
5053 , onConflict :: OnConflict
54+ -- ^ What to do if the inserted rows conflict with data already in the
55+ -- table.
5156 , returning :: Returning names a
57+ -- ^ What information to return on completion.
5258 }
5359 -> Insert a
5460
5561
56−data OnConflict = Abort | DoNothing
62+-- | @OnConflict@ allows you to add an @ON CONFLICT@ clause to an @INSERT@
63+-- statement.
64+data OnConflict
65+ = Abort -- ^ @ON CONFLICT ABORT@
66+ | DoNothing -- ^ @ON CONFLICT DO NOTHING@
5767
5868
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
5980insert :: Insert a -> Connection -> IO a
6081insert Insert {into, rows, onConflict, returning} =
6182 case (rows, returning) of