@@ -29,6 +29,31 @@ import Database.PostgreSQL.Driver.StatementStorage
29
29
import Database.PostgreSQL.Driver.Error
30
30
import Database.PostgreSQL.Driver.RawConnection
31
31
32
+ -- | Public
33
+ data Connection = Connection
34
+ { connRawConnection :: RawConnection
35
+ , connReceiverThread :: ThreadId
36
8000
+ -- channel only for Data messages
37
+ , connOutDataChan :: OutChan (Either Error DataMessage )
38
+ -- channel for all the others messages
39
+ , connOutAllChan :: OutChan ServerMessage
40
+ , connStatementStorage :: StatementStorage
41
+ , connParameters :: ConnectionParameters
42
+ , connMode :: IORef ConnectionMode
43
+ }
44
+
45
+ -- | Parameters of the current connection.
46
+ -- We store only the parameters that cannot change after startup.
47
+ -- For more information about additional parameters see
48
+ -- PostgreSQL documentation.
49
+ data ConnectionParameters = ConnectionParameters
50
+ { paramServerVersion :: ServerVersion
51
+ -- | character set name
52
+ , paramServerEncoding :: B. ByteString
53
+ -- | True if integer datetimes used
54
+ , paramIntegerDatetimes :: Bool
55
+ } deriving (Show )
56
+
32
57
data ConnectionMode
33
58
-- | In this mode, all result's data is ignored
34
59
= SimpleQueryMode
@@ -44,7 +69,7 @@ type NotificationHandler = Notification -> IO ()
44
69
defaultNotificationHandler :: NotificationHandler
45
70
defaultNotificationHandler = const $ pure ()
46
71
47
- type Dispatcher
72
+ type DataDispatcher
48
73
= InChan (Either Error DataMessage )
49
74
-> ServerMessage
50
75
-> [V. Vector (Maybe B. ByteString )]
@@ -53,30 +78,6 @@ type Dispatcher
53
78
data DataMessage = DataMessage [V. Vector (Maybe B. ByteString )]
54
79
deriving (Show , Eq )
55
80
56
- -- | Parameters of the current connection.
57
- -- We store only the parameters that cannot change after startup.
58
- -- For more information about additional parameters see
59
- -- PostgreSQL documentation.
60
- data ConnectionParameters = ConnectionParameters
61
- { paramServerVersion :: ServerVersion
62
- -- | character set name
63
- , paramServerEncoding :: B. ByteString
64
- -- | True if integer datetimes used
65
- , paramIntegerDatetimes :: Bool
66
- } deriving (Show )
67
-
68
- -- | Public
69
- data Connection = Connection
70
- { connRawConnection :: RawConnection
71
- , connReceiverThread :: ThreadId
72
- -- channel only for Data messages
73
- , connOutDataChan :: OutChan (Either Error DataMessage )
74
- -- channel for all the others messages
75
- , connOutAllChan :: OutChan ServerMessage
76
- , connStatementStorage :: StatementStorage
77
- , connParameters :: ConnectionParameters
78
- , connMode :: IORef ConnectionMode
79
- }
80
81
81
82
-- | Public
82
83
connect :: ConnectionSettings -> IO (Either Error Connection )
@@ -93,30 +94,6 @@ connectWith settings msgFilter =
93
94
either throwErrorInIO (\ params ->
94
95
Right <$> buildConnection rawConn params msgFilter))
95
96
96
- buildConnection
97
- :: RawConnection
98
- -> ConnectionParameters
99
- -> ServerMessageFilter
100
- -> IO Connection
101
- buildConnection rawConn connParams msgFilter = do
102
- (inDataChan, outDataChan) <- newChan
103
- (inAllChan, outAllChan) <- newChan
104
- storage <- newStatementStorage
105
- modeRef <- newIORef defaultConnectionMode
106
-
107
- tid <- forkIO $
108
- receiverThread msgFilter rawConn inDataChan inAllChan modeRef
109
- defaultNotificationHandler
110
- pure Connection
111
- { connRawConnection = rawConn
112
- , connReceiverThread = tid
113
- , connOutDataChan = outDataChan
114
- , connOutAllChan = outAllChan
115
- , connStatementStorage = storage
116
- , connParameters = connParams
117
- , connMode = modeRef
118
- }
119
-
120
97
-- | Authorizes on the server and reads connection parameters.
121
98
authorize
122
99
:: RawConnection
@@ -160,6 +137,30 @@ authorize rawConn settings = do
160
137
(settingsPassword settings <> settingsUser settings) <> salt)
161
138
md5Hash bs = BS. pack $ show (hash bs :: Digest MD5 )
162
139
140
+ buildConnection
141
+ :: RawConnection
142
+ -> ConnectionParameters
143
+ -> ServerMessageFilter
144
+ -> IO Connection
145
+ buildConnection rawConn connParams msgFilter = do
146
+ (inDataChan, outDataChan) <- newChan
147
+ (inAllChan, outAllChan) <- newChan
148
+ storage <- newStatementStorage
149
+ modeRef <- newIORef defaultConnectionMode
150
+
151
+ tid <- forkIO $
152
+ receiverThread msgFilter rawConn inDataChan inAllChan modeRef
153
+ defaultNotificationHandler
154
+ pure Connection
155
+ { connRawConnection = rawConn
156
+ , connReceiverThread = tid
157
+ , connOutDataChan = outDataChan
158
+ , connOutAllChan = outAllChan
159
+ , connStatementStorage = storage
160
+ , connParameters = connParams
161
+ , connMode = modeRef
162
+ }
163
+
163
164
-- | Parses connection parameters.
164
165
parseParameters :: B. ByteString -> Either Error ConnectionParameters
165
166
parseParameters str = do
@@ -196,6 +197,7 @@ close conn = do
196
197
killThread $ connReceiverThread conn
197
198
rClose $ connRawConnection conn
198
199
200
+
199
201
receiverThread
200
202
:: ServerMessageFilter
201
203
-> RawConnection
@@ -227,17 +229,16 @@ receiverThread msgFilter rawConn dataChan allChan modeRef ntfHandler =
227
229
dispatchIfNotification (NotificationResponse n) = ntfHandler n
228
230
dispatchIfNotification _ = pure ()
229
231
230
-
231
- dispatch :: ConnectionMode -> Dispatcher
232
+ dispatch :: ConnectionMode -> DataDispatcher
232
233
dispatch SimpleQueryMode = dispatchSimple
233
234
dispatch ExtendedQueryMode = dispatchExtended
234
235
235
236
-- | Dispatcher for the SimpleQuery mode.
236
- dispatchSimple :: Dispatcher
237
+ dispatchSimple :: DataDispatcher
237
238
dispatchSimple dataChan message = pure
238
239
239
240
-- | Dispatcher for the ExtendedQuery mode.
240
- dispatchExtended :: Dispatcher
241
+ dispatchExtended :: DataDispatcher
241
242
dispatchExtended dataChan message acc = case message of
242
243
-- Command is completed, return the result
243
244
CommandComplete _ -> do
0 commit comments