@@ -14,19 +14,19 @@ import qualified Data.ByteString as B
14
14
import Data.ByteString.Char8 (readInteger , readInt )
15
15
import qualified Data.ByteString.Lazy as BL
16
16
import qualified Data.HashMap.Strict as HM
17
- import Data.Binary.Get
18
17
19
18
import Database.PostgreSQL.Protocol.Types
19
+ import Database.PostgreSQL.Protocol.Store.Decode
20
20
21
- decodeAuthResponse :: Get AuthResponse
21
+ decodeAuthResponse :: Decode AuthResponse
22
22
decodeAuthResponse = do
23
23
c <- getWord8
24
- len <- getInt32be
24
+ len <- getInt32BE
25
25
case chr $ fromIntegral c of
26
26
' E' -> AuthErrorResponse <$>
27
27
(getByteString (fromIntegral $ len - 4 ) >>= decodeErrorDesc)
28
28
' R' -> do
29
- rType <- getInt32be
29
+ rType <- getInt32BE
30
30
case rType of
31
31
0 -> pure AuthenticationOk
32
32
3 -> pure AuthenticationCleartextPassword
@@ -38,19 +38,19 @@ decodeAuthResponse = do
38
38
_ -> fail " Unknown authentication response"
39
39
_ -> fail " Invalid auth response"
40
40
41
- decodeServerMessage :: Get ServerMessage
41
+ decodeServerMessage :: Decode ServerMessage
42
42
decodeServerMessage = do
43
43
c <- getWord8
44
- len <- getInt32be
44
+ len <- getInt32BE
45
45
case chr $ fromIntegral c of
46
- ' K' -> BackendKeyData <$> (ServerProcessId <$> getInt32be )
47
- <*> (ServerSecretKey <$> getInt32be )
46
+ ' K' -> BackendKeyData <$> (ServerProcessId <$> getInt32BE )
47
+ <*> (ServerSecretKey <$> getInt32BE )
48
48
' 2' -> pure BindComplete
49
49
' 3' -> pure CloseComplete
50
50
' C' -> CommandComplete <$> (getByteString (fromIntegral $ len - 4 )
51
51
>>= decodeCommandResult)
52
52
' D' -> do
53
- columnCount <- fromIntegral <$> getInt16be
53
+ columnCount <- fromIntegral <$> getInt16BE
54
54
DataRow <$> V. replicateM columnCount decodeValue
55
55
' I' -> pure EmptyQueryResponse
56
56
' E' -> ErrorResponse <$>
@@ -60,55 +60,57 @@ decodeServerMessage = do
60
60
(getByteString (fromIntegral $ len - 4 ) >>= decodeNoticeDesc)
61
61
' A' -> NotificationResponse <$> decodeNotification
62
62
' t' -> do
63
- paramCount <- fromIntegral <$> getInt16be
63
+ paramCount <- fromIntegral <$> getInt16BE
64
64
ParameterDescription <$> V. replicateM paramCount
65
- (Oid <$> getInt32be )
66
- ' S' -> ParameterStatus <$> decodePgString <*> decodePgString
65
+ (Oid <$> getInt32BE )
66
+ ' S' -> ParameterStatus <$> getByteStringNull <*> getByteStringNull
67
67
' 1' -> pure ParseComplete
68
68
' s' -> pure PortalSuspended
69
69
' Z' -> ReadForQuery <$> decodeTransactionStatus
70
70
' T' -> do
71
- rowsCount <- fromIntegral <$> getInt16be
71
+ rowsCount <- fromIntegral <$> getInt16BE
72
72
RowDescription <$> V. replicateM rowsCount decodeFieldDescription
73
73
74
74
-- | Decodes a single data value. Length `-1` indicates a NULL column value.
75
75
-- No value bytes follow in the NULL case.
76
- decodeValue :: Get (Maybe B. ByteString )
77
- decodeValue = fromIntegral <$> getInt32be >>= \ n ->
78
- if n == - 1 then pure Nothing else Just <$> getByteString n
76
+ decodeValue :: Decode (Maybe B. ByteString )
77
+ decodeValue = fromIntegral <$> getInt32BE >>= \ n ->
78
+ if n == - 1
79
+ then pure Nothing
80
+ else Just <$> getByteString n
79
81
80
- decodeTransactionStatus :: Get TransactionStatus
82
+ decodeTransactionStatus :: Decode TransactionStatus
81
83
decodeTransactionStatus = getWord8 >>= \ t ->
82
84
case chr $ fromIntegral t of
83
85
' I' -> pure TransactionIdle
84
86
' T' -> pure TransactionInBlock
85
87
' E' -> pure TransactionFailed
86
88
_ -> fail " unknown transaction status"
87
89
88
- decodeFieldDescription :: Get FieldDescription
90
+ decodeFieldDescription :: Decode FieldDescription
89
91
decodeFieldDescription = FieldDescription
90
- <$> decodePgString
91
- <*> (Oid <$> getInt32be )
92
- <*> getInt16be
93
- <*> (Oid <$> getInt32be )
94
- <*> getInt16be
95
- <*> getInt32be
92
+ <$> getByteStringNull
93
+ <*> (Oid <$> getInt32BE )
94
+ <*> getInt16BE
95
+ <*> (Oid <$> getInt32BE )
96
+ <*> getInt16BE
97
+ <*> getInt32BE
96
98
<*> decodeFormat
97
99
98
- decodeNotification :: Get Notification
100
+ decodeNotification :: Decode Notification
99
101
decodeNotification = Notification
100
- <$> (ServerProcessId <$> getInt32be )
101
- <*> (ChannelName <$> decodePgString )
102
- <*> decodePgString
102
+ <$> (ServerProcessId <$> getInt32BE )
103
+ <*> (ChannelName <$> getByteStringNull )
104
+ <*> getByteStringNull
103
105
104
- decodeFormat :: Get Format
105
- decodeFormat = getInt16be >>= \ f ->
106
+ decodeFormat :: Decode Format
107
+
B41A
decodeFormat = getInt16BE >>= \ f ->
106
108
case f of
107
109
0 -> pure Text
108
110
1 -> pure Binary
109
111
_ -> fail " Unknown field format"
110
112
111
- decodeCommandResult :: B. ByteString -> Get CommandResult
113
+ decodeCommandResult :: B. ByteString -> Decode CommandResult
112
114
decodeCommandResult s =
113
115
let (command, rest) = B. break (== space) s
114
116
in case command of
@@ -151,7 +153,7 @@ decodeNoticeSeverity "INFO" = SeverityInfo
151
153
decodeNoticeSeverity " LOG" = SeverityLog
152
154
decodeNoticeSeverity _ = UnknownNoticeSeverity
153
155
154
- decodeErrorDesc :: B. ByteString -> Get ErrorDesc
156
+ decodeErrorDesc :: B. ByteString -> Decode ErrorDesc
155
157
decodeErrorDesc s = do
156
158
let hm = decodeErrorNoticeFields s
157
159
errorSeverityOld <- lookupKey ' S' hm
@@ -184,7 +186,7 @@ decodeErrorDesc s = do
184
186
" is not presented in ErrorResponse message" )
185
187
pure . HM. lookup c
186
188
187
- decodeNoticeDesc :: B. ByteString -> Get NoticeDesc
189
+ decodeNoticeDesc :: B. ByteString -> Decode NoticeDesc
188
190
decodeNoticeDesc s = do
189
191
let hm = decodeErrorNoticeFields s
190
192
noticeSeverityOld <- lookupKey ' S' hm
@@ -217,10 +219,3 @@ decodeNoticeDesc s = do
217
219
" is not presented in NoticeResponse message" )
218
220
pure . HM. lookup c
219
221
220
- ------
221
- -- Utils
222
- ------
223
-
224
- decodePgString :: Get B. ByteString
225
- decodePgString = BL. toStrict <$> getLazyByteStringNul
226
-
0 commit comments