2
2
module Database.PostgreSQL.Driver.RawConnection where
3
3
4
4
import Control.Monad (void )
5
+ import Control.Exception (bracketOnError )
5
6
import Safe (headMay )
6
7
import Data.Monoid ((<>) )
7
8
import System.Socket (socket , AddressInfo (.. ), getAddressInfo , socketAddress ,
@@ -38,6 +39,12 @@ createRawConnection settings
38
39
| " /" `B.isPrefixOf` host = unixConnection host
39
40
| otherwise = tcpConnection
40
41
where
42
+ createAndConnect Nothing creating = throwAuthErrorInIO AuthInvalidAddress
43
+ createAndConnect (Just address) creating =
44
+ bracketOnError creating close $ \ s -> do
45
+ connect s address
46
+ pure . Right $ constructRawConnection s
47
+
41
48
unixConnection dirPath = do
42
49
let mAddress = socketAddressUnixPath $ makeUnixPath dirPath
43
50
createAndConnect mAddress (socket :: IO (Socket Unix Stream Unix ))
@@ -48,12 +55,6 @@ createRawConnection settings
48
55
:: IO [AddressInfo Inet Stream TCP ])
49
56
createAndConnect mAddress (socket :: IO (Socket Inet Stream TCP ))
50
57
51
- createAndConnect Nothing creating = throwAuthErrorInIO AuthInvalidAddress
52
- createAndConnect (Just address) creating = do
53
- s <- creating
54
- connect s address
55
- pure . Right $ constructRawConnection s
56
-
57
58
portStr = BS. pack . show $ settingsPort settings
58
59
host = settingsHost settings
59
60
makeUnixPath dirPath =
0 commit comments