Add Connection type
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
{-# LANGUAGE LambdaCase #-}
|
||||
|
||||
module Database.PostgreSQL.Opium.Connection
|
||||
( Connection
|
||||
, unsafeWithRawConnection
|
||||
, withRawConnection
|
||||
, connect
|
||||
, close
|
||||
) where
|
||||
|
||||
import Data.ByteString (ByteString)
|
||||
import GHC.Stack (HasCallStack)
|
||||
|
||||
import qualified Database.PostgreSQL.LibPQ as LibPQ
|
||||
|
||||
newtype Connection = Connection LibPQ.Connection
|
||||
|
||||
withRawConnection
|
||||
:: (Maybe LibPQ.Connection -> m a)
|
||||
-> Connection
|
||||
-> m a
|
||||
withRawConnection f (Connection rawConn) = f (Just rawConn)
|
||||
|
||||
unsafeWithRawConnection
|
||||
:: HasCallStack
|
||||
=> (LibPQ.Connection -> m a)
|
||||
-> Connection
|
||||
-> m a
|
||||
unsafeWithRawConnection f = withRawConnection $ \case
|
||||
Nothing -> error "raw connection is missing! perhaps the connection was already closed."
|
||||
Just rawConn -> f rawConn
|
||||
|
||||
connect :: ByteString -> IO Connection
|
||||
connect connectionString = do
|
||||
rawConn <- LibPQ.connectdb connectionString
|
||||
pure $ Connection rawConn
|
||||
|
||||
close :: Connection -> IO ()
|
||||
close (Connection rawConn) = LibPQ.finish rawConn
|
||||
@@ -19,6 +19,7 @@ data Error
|
||||
| ErrorInvalidField ErrorPosition Oid ByteString String
|
||||
| ErrorNotExactlyOneRow
|
||||
| ErrorMoreThanOneRow
|
||||
| ErrorConnectionClosed
|
||||
deriving (Eq, Show)
|
||||
|
||||
instance Exception Error where
|
||||
|
||||
Reference in New Issue
Block a user