File tree Expand file tree Collapse file tree 3 files changed +19
-8
lines changed
src/Database/PostgreSQL/Protocol Expand file tree Collapse file tree 3 files changed +19
-8
lines changed Original file line number Diff line number Diff line change @@ -51,8 +51,7 @@ decodeServerMessage = do
51
51
>>= decodeCommandResult)
52
52
' D' -> do
53
53
columnCount <- fromIntegral <$> getInt16be
54
- DataRow <$> V. replicateM columnCount
55
- (getInt32be >>= getByteString . fromIntegral )
54
+ DataRow <$> V. replicateM columnCount decodeValue
56
55
' I' -> pure EmptyQueryResponse
57
56
' E' -> ErrorResponse <$>
58
57
(getByteString (fromIntegral $ len - 4 ) >>= decodeErrorDesc)
@@ -72,6 +71,12 @@ decodeServerMessage = do
72
71
rowsCount <- fromIntegral <$> getInt16be
73
72
RowDescription <$> V. replicateM rowsCount decodeFieldDescription
74
73
74
+ -- | Decodes a single data value. Length `-1` indicates a NULL column value.
75
+ -- No value bytes follow in the NULL case.
76
+ decodeValue :: Get B. ByteString
77
+ decodeValue = fromIntegral <$> getInt32be >>= \ n ->
78
+ if n == - 1 then pure " " else getByteString n
79
+
75
80
decodeTransactionStatus :: Get TransactionStatus
76
81
decodeTransactionStatus = getWord8 >>= \ t ->
77
82
case chr $ fromIntegral t of
Original file line number Diff line number Diff line change @@ -36,10 +36,7 @@ encodeClientMessage (Bind (PortalName portalName) (StatementName stmtName)
36
36
int16BE 1 <>
37
37
encodeFormat paramFormat <>
38
38
int16BE (fromIntegral $ V. length values) <>
39
- -- TODO -
8000
1 indicates a NULL parameter value. No value bytes
40
- -- follow in the NULL case.
41
- fold ((\ v -> int32BE (fromIntegral $ B. length v) <> byteString v)
42
- <$> values) <>
39
+ fold (encodeValue <$> values) <>
43
40
-- `1` means that the specified format code is applied to all
44
41
-- result columns (if any)
45
42
int16BE 1 <>
@@ -76,6 +73,13 @@ encodeClientMessage Sync
76
73
encodeClientMessage Terminate
77
74
= prependHeader ' X' mempty
78
75
76
+ -- Encodes single data values. Length `-1` indicates a NULL parameter value.
77
+ -- No value bytes follow in the NULL case.
78
+ encodeValue :: B. ByteString -> Builder
79
+ encodeValue v | B. null v = int32BE (- 1 )
80
+ | otherwise = int32BE (fromIntegral $ B. length v)
81
+ <> byteString v
82
+
79
83
encodeFormat :: Format -> Builder
80
84
encodeFormat Text = int16BE 0
81
85
encodeFormat Binary = int16BE 1
Original file line number Diff line number Diff line change @@ -102,7 +102,8 @@ data AuthResponse
102
102
data ClientMessage
103
103
= Bind PortalName StatementName
104
104
Format -- parameter format code, one format for all
105
- (Vector ByteString ) -- the values of parameters
105
+ (Vector ByteString ) -- the values of parameters, the empty string
106
+ -- is recognized as NULL
106
107
Format -- to apply code to all result columns
107
108
-- Postgres use one command `close` for closing both statements and
108
109
-- portals, but we distinguish them
@@ -130,7 +131,8 @@ data ServerMessage
130
131
| BindComplete
131
132
| CloseComplete
132
133
| CommandComplete CommandResult
133
- | DataRow (Vector ByteString )
134
+ | DataRow (Vector ByteString ) -- an empty string should be recognized
135
+ -- as NULL
134
136
| EmptyQueryResponse
135
137
| ErrorResponse ErrorDesc
136
138
| NoData
You can’t perform that action at this time.
0 commit comments