8000 Different dispatchers · postgres-haskell/postgres-wire@c0a0ca8 · GitHub
[go: up one dir, main page]

Skip to content

Commit c0a0ca8

Browse files
Different dispatchers
1 parent 3dc608a commit c0a0ca8

File tree

1 file changed

+43
-28
lines changed

1 file changed

+43
-28
lines changed

src/Database/PostgreSQL/Driver/Connection.hs

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ defaultConnectionMode = ExtendedQueryMode
3838
type ServerMessageFilter = ServerMessage -> Bool
3939
type NotificationHandler = Notification -> IO ()
4040

41+
type Dispatcher
42+
= InChan (Either Error DataMessage)
43+
-> ServerMessage
44+
-> [V.Vector B.ByteString]
45+
-> IO [V.Vector B.ByteString]
4146
data DataMessage = DataMessage [V.Vector B.ByteString]
4247
deriving (Show, Eq)
4348

@@ -185,41 +190,51 @@ receiverThread msgFilter rawConn dataChan allChan modeRef = receiveLoop []
185190
BG.Done rest _ v -> do
186191
-- putStrLn $ "Received: " ++ show v
187192
when (msgFilter v) $ writeChan allChan v
188-
newAcc <- dispatch dataChan v acc
193+
mode <- readIORef modeRef
194+
newAcc <- dispatch mode dataChan v acc
189195
if B.null rest
190196
then pure newAcc
191197
else go rest newAcc
192198
-- TODO right parsing
193199
BG.Partial _ -> error "Partial"
194200
BG.Fail _ _ e -> error e
195201

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
223238

224239
-- | For testings purposes.
225240
filterAllowedAll :: ServerMessageFilter

0 commit comments

Comments
 (0)
0