add withConnect: Memory bracket around `connect` and `close`
commit5231a40db8057e78e98790e95c9b82ac6c32119f
authorSteve Mao <maochenyan@gmail.com>
dateSat 4 Sep 2021, 15:15 (5 years ago)
parent6cabb13
src/Database/PostgreSQL/Simple.hs+1
@@ -73,6 +73,7 @@ module Database.PostgreSQL.Simple
7373 , Base.connectPostgreSQL
7474 , Base.close
7575 , Base.connect
76+ , Base.withConnect
7677 , Base.ConnectInfo(..)
7778 , Base.defaultConnectInfo
7879 , Base.postgreSQLConnectionString
src/Database/PostgreSQL/Simple/Internal.hs+4
@@ -155,6 +155,10 @@ defaultConnectInfo = ConnectInfo {
155155connect :: ConnectInfo -> IO Connection
156156connect = connectPostgreSQL . postgreSQLConnectionString
157157
158+-- | Memory bracket around 'connect' and 'close'.
159+withConnect :: ConnectInfo -> (Connection -> IO c) -> IO c
160+withConnect connInfo = bracket (connect connInfo) close
161+
158162-- | Attempt to make a connection based on a libpq connection string.
159163-- See <https://www.postgresql.org/docs/9.5/static/libpq-connect.html#LIBPQ-CONNSTRING>
160164-- for more information. Also note that environment variables also affect