@@ -24,19 +24,38 @@ data Query = Query
24
24
, qCachePolicy :: CachePolicy
25
25
} deriving (Show )
26
26
27
+ -- | Public
28
+ sendBatchAndFlush :: Connection -> [Query ] -> IO ()
29
+ sendBatchAndFlush = sendBatchEndBy Flush
30
+
27
31
-- | Public
28
32
sendBatchAndSync :: Connection -> [Query ] -> IO ()
29
33
sendBatchAndSync = sendBatchEndBy Sync
30
34
31
35
-- | Public
32
- sendBatchAndFlush :: Connection -> [Query ] -> IO ()
33
- sendBatchAndFlush = sendBatchEndBy Flush
36
+ sendSimpleQuery :: Connection -> B. ByteString -> IO (Either Error () )
37
+ sendSimpleQuery conn q = withConnectionMode conn SimpleQueryMode $ \ c -> do
38
+ sendMessage (connRawConnection c) $ SimpleQuery (StatementSQL q)
39
+ waitReadyForQuery c
40
+
41
+ -- | Public
42
+ readNextData :: Connection -> IO (Either Error DataMessage )
43
+ readNextData conn = readChan $ connOutDataChan conn
44
+
45
+ -- | Public
46
+ -- MUST BE called after every sended `Sync` message
47
+ -- discards all messages preceding `ReadyForQuery`
48
+ waitReadyForQuery :: Connection -> IO (Either Error () )
49
+ waitReadyForQuery = fmap (>>= (liftError . findFirstError))
50
+ . collectUntilReadyForQuery
51
+ where
52
+ liftError = maybe (Right () ) (Left . PostgresError )
34
53
35
54
-- Helper
36
55
sendBatchEndBy :: ClientMessage -> Connection -> [Query ] -> IO ()
37
56
sendBatchEndBy msg conn qs = do
38
57
batch <- constructBatch conn qs
39
- sendEncode (connRawConnection conn) $ batch <> encodeClientMessage msg
58
+ sendEncode conn $ batch <> encodeClientMessage msg
40
59
41
60
constructBatch :: Connection -> [Query ] -> IO Encode
42
61
constructBatch conn = fmap fold . traverse constructSingle
@@ -65,61 +84,40 @@ constructBatch conn = fmap fold . traverse constructSingle
65
84
Execute pname noLimitToReceive
66
85
pure $ parseMessage <> bindMessage <> executeMessage
67
86
68
- -- | Public
69
- readNextData :: Connection -> IO (Either Error DataMessage )
70
- readNextData conn = readChan $ connOutDataChan conn
71
-
72
- -- | Public
73
- sendSimpleQuery :: Connection -> B. ByteString -> IO (Either Error () )
74
- sendSimpleQuery conn q = withConnectionMode conn SimpleQueryMode $ \ c -> do
75
- sendMessage (connRawConnection c) $ SimpleQuery (StatementSQL q)
76
- readReadyForQuery c
77
-
78
-
79
- -- | Public
80
- -- SHOULD BE called after every sended `Sync` message
81
- -- skips all messages except `ReadyForQuery`
82
- readReadyForQuery :: Connection -> IO (Either Error () )
83
- readReadyForQuery = fmap (>>= (liftError . findFirstError))
84
- . collectBeforeReadyForQuery
85
- where
86
- liftError = maybe (Right () ) (Left . PostgresError )
87
-
88
- findFirstError :: [ServerMessage ] -> Maybe ErrorDesc
89
- findFirstError [] = Nothing
90
- findFirstError (ErrorResponse desc : _) = Just desc
91
- findFirstError (_ : xs) = findFirstError xs
92
-
93
- -- Collects all messages received before ReadyForQuery
94
- collectBeforeReadyForQuery :: Connection -> IO (Either Error [ServerMessage ])
95
- collectBeforeReadyForQuery conn = do
96
- msg <- readChan $ connOutAllChan conn
97
- case msg of
98
- Left e -> pure $ Left e
99
- Right ReadForQuery {} -> pure $ Right []
100
- Right m -> fmap (m: ) <$> collectBeforeReadyForQuery conn
101
-
102
87
-- | Public
103
88
describeStatement
104
89
:: Connection
105
90
-> B. ByteString
106
91
-> IO (Either Error (V. Vector Oid , V. Vector FieldDescription ))
107
92
describeStatement conn stmt = do
108
- sendEncode s $
93
+ sendEncode conn $
109
94
encodeClientMessage (Parse sname (StatementSQL stmt) V. empty)
110
95
<> encodeClientMessage (DescribeStatement sname)
111
96
<> encodeClientMessage Sync
112
- (parseMessages =<< ) <$> collectBeforeReadyForQuery conn
97
+ (parseMessages =<< ) <$> collectUntilReadyForQuery conn
113
98
where
114
- s = connRawConnection conn
115
99
sname = StatementName " "
116
100
parseMessages msgs = case msgs of
117
101
[ParameterDescription params, NoData ]
118
102
-> Right (params, V. empty)
119
103
[ParameterDescription params, RowDescription fields]
120
104
-> Right (params, fields)
121
105
xs -> Left . maybe
122
- (DecodeError " Unexpected response on describe query" )
106
+ (DecodeError " Unexpected response on a describe query" )
123
107
PostgresError
124
108
$ findFirstError xs
125
109
110
+ -- Collects all messages preceding `ReadyForQuery`
111
+ collectUntilReadyForQuery :: Connection -> IO (Either Error [ServerMessage ])
112
+ collectUntilReadyForQuery conn = do
113
+ msg <- readChan $ connOutAllChan conn
114
+ case msg of
115
+ Left e -> pure $ Left e
116
+ Right ReadForQuery {} -> pure $ Right []
117
+ Right m -> fmap (m: ) <$> collectUntilReadyForQuery conn
118
+
119
+ findFirstError :: [ServerMessage ] -> Maybe ErrorDesc
120
+ findFirstError [] = Nothing
121
+ findFirstError (ErrorResponse desc : _) = Just desc
122
+ findFirstError (_ : xs) = findFirstError xs
123
+
0 commit comments