@@ -38,6 +38,11 @@ defaultConnectionMode = ExtendedQueryMode
38
38
type ServerMessageFilter = ServerMessage -> Bool
39
39
type NotificationHandler = Notification -> IO ()
40
40
41
+ type Dispatcher
42
+ = InChan (Either Error DataMessage )
43
+ -> ServerMessage
44
+ -> [V. Vector B. ByteString ]
45
+ -> IO [V. Vector B. ByteString ]
41
46
data DataMessage = DataMessage [V. Vector B. ByteString ]
42
47
deriving (Show , Eq )
43
48
@@ -185,41 +190,51 @@ receiverThread msgFilter rawConn dataChan allChan modeRef = receiveLoop []
185
190
BG. Done rest _ v -> do
186
191
-- putStrLn $ "Received: " ++ show v
187
192
when (msgFilter v) $ writeChan allChan v
188
- newAcc <- dispatch dataChan v acc
193
+ mode <- readIORef modeRef
194
+ newAcc <- dispatch mode dataChan v acc
189
195
if B. null rest
190
196
then pure newAcc
191
197
else go rest newAcc
192
198
-- TODO right parsing
193
199
BG. Partial _ -> error " Partial"
194
200
BG. Fail _ _ e -> error e
195
201
196
- dispatch
197
- :: InChan (Either Error DataMessage )
198
- -> ServerMessage
199
- -> [V. Vector B. ByteString ]
200
- -> IO [V. Vector B. ByteString ]
201
- -- Command is completed, return the result
202
- dispatch dataChan (CommandComplete _) acc = do
203
- writeChan dataChan . Right . DataMessage $ reverse acc
204
- pure []
205
- -- note that data rows go in reversed order
206
- dispatch dataChan (DataRow row) acc = pure (row: acc)
207
- -- PostgreSQL sends this if query string was empty and datarows should be
208
- -- empty, but anyway we return data collected in `acc`.
209
- dispatch dataChan EmptyQueryResponse acc = do
210
- writeChan dataChan . Right . DataMessage $ reverse acc
211
- pure []
212
- -- On ErrorResponse we should discard all the collected datarows
213
- dispatch dataChan (ErrorResponse desc) acc = do
214
- writeChan dataChan $ Left $ PostgresError desc
215
- pure []
216
- -- TODO handle notifications
217
- dispatch dataChan (NotificationResponse n) acc = pure acc
218
- -- We does not handled this case because we always send `execute`
219
- -- with no limit.
220
- dispatch dataChan PortalSuspended acc = pure acc
221
- -- do nothing on other messages
222
- dispatch dataChan _ ac
8000
c = pure acc
202
+ dispatch :: ConnectionMode -> Dispatcher
203
+ dispatch SimpleQueryMode = dispatchSimple
204
+ dispatch ExtendedQueryMode = dispatchExtended
205
+
206
+ -- | Dispatcher for the SimpleQuery mode.
207
+ dispatchSimple :: Dispatcher
208
+ dispatchSimple dataChan message acc = case message of
209
+ NotificationResponse n -> pure acc
210
+ -- do nothing on other messages
211
+ _ -> pure acc
212
+
213
+ -- | Dispatcher for the ExtendedQuery mode.
214
+ dispatchExtended :: Dispatcher
215
+ dispatchExtended dataChan message acc = case message of
216
+ -- Command is completed, return the result
217
+ CommandComplete _ -> do
218
+ writeChan dataChan . Right . DataMessage $ reverse acc
219
+ pure []
220
+ -- note that data rows go in reversed order
221
+ DataRow row -> pure (row: acc)
222
+ -- PostgreSQL sends this if query string was empty and datarows should be
223
+ -- empty, but anyway we return data collected in `acc`.
224
+ EmptyQueryResponse -> do
225
+ writeChan dataChan . Right . DataMessage $ reverse acc
226
+ pure []
227
+ -- On ErrorResponse we should discard all the collected datarows
228
+ ErrorResponse desc -> do
229
+ writeChan dataChan $ Left $ PostgresError desc
230
+ pure []
231
+ -- TODO handle notifications
232
+ NotificationResponse n -> pure acc
233
+ -- We does not handled this case because we always send `execute`
234
+ -- with no limit.
235
+ PortalSuspended -> pure acc
236
+ -- do nothing on other messages
237
+ _ -> pure acc
223
238
224
239
-- | For testings purposes.
225
240
filterAllowedAll :: ServerMessageFilter
0 commit comments