2017-01-29 01:48:25 +03:00
|
|
|
module Connection where
|
|
|
|
|
|
|
|
import Control.Exception (bracket)
|
|
|
|
import Database.PostgreSQL.Driver.Connection
|
2017-02-01 06:53:56 +03:00
|
|
|
import Database.PostgreSQL.Driver.Error
|
2017-01-29 01:48:25 +03:00
|
|
|
import Database.PostgreSQL.Driver.Settings
|
|
|
|
|
|
|
|
-- | Creates connection with default filter.
|
|
|
|
withConnection :: (Connection -> IO a) -> IO a
|
2017-02-01 05:09:35 +03:00
|
|
|
withConnection = bracket (getConnection <$> connect defaultSettings) close
|
2017-01-29 01:48:25 +03:00
|
|
|
|
|
|
|
-- | Creates connection than collects all server messages in chan.
|
|
|
|
withConnectionAll :: (Connection -> IO a) -> IO a
|
2017-02-01 05:09:35 +03:00
|
|
|
withConnectionAll = bracket
|
|
|
|
(getConnection <$> connectWith defaultSettings filterAllowedAll) close
|
2017-01-29 01:48:25 +03:00
|
|
|
|
|
|
|
defaultSettings = defaultConnectionSettings
|
|
|
|
{ settingsHost = "localhost"
|
|
|
|
, settingsDatabase = "travis_test"
|
|
|
|
, settingsUser = "postgres"
|
|
|
|
, settingsPassword = ""
|
|
|
|
}
|
|
|
|
|
2017-02-01 05:09:35 +03:00
|
|
|
getConnection :: Either Error Connection -> Connection
|
|
|
|
getConnection (Left e) = error $ "Connection error " ++ show e
|
|
|
|
getConnection (Right c) = c
|
|
|
|
|