@@ -3,99 +3,93 @@ module Database.PostgreSQL.Protocol.Encoders where
3
3
import Data.Word
4
4
import Data.Int
5
5
import Data.Monoid
6
- import Data.ByteString.Lazy as BL
7
- import Data.ByteString.Builder
8
6
import Data.Foldable
9
7
import qualified Data.Vector as V
10
8
import qualified Data.ByteString as B
11
9
12
10
import Database.PostgreSQL.Protocol.Types
11
+ import Database.PostgreSQL.Protocol.Store
13
12
14
13
-- | Protocol Version 3.0, major version in the first word16.
15
14
currentVersion :: Int32
16
15
currentVersion = 3 * 256 * 256
17
16
18
- encodeStartMessage :: StartMessage -> Builder
17
+ encodeStartMessage :: StartMessage -> Encode
19
18
encodeStartMessage (StartupMessage (Username uname) (DatabaseName dbname))
20
- = int32BE (len + 4 ) <> payload
19
+ = putInt32BE (len + 4 ) <> payload
21
20
where
22
- len = fromIntegral $ BL. length $ toLazyByteString payload
23
- payload = int32BE currentVersion <>
24
- pgString " user" <> pgString uname <>
25
- pgString " database" <> pgString dbname <> word8 0
21
+ len = fromIntegral $ getEncodeLen payload
22
+ payload = putInt32BE currentVersion <>
23
+ putPgString " user" <> putPgString uname <>
24
+ putPgString " database" <> putPgString dbname <> putWord8 0
26
25
encodeStartMessage SSLRequest
27
- = int32BE 8 <> int32BE 80877103 -- value hardcoded by PostgreSQL docs.
26
+ = putInt32BE 8 <> putInt32BE 80877103 -- value hardcoded by PostgreSQL docs.
28
27
29
- encodeClientMessage :: ClientMessage -> Builder
28
+ encodeClientMessage :: ClientMessage -> Encode
30
29
encodeClientMessage (Bind (PortalName portalName) (StatementName stmtName)
31
30
paramFormat values resultFormat)
32
31
= prependHeader ' B' $
33
- pgString portalName <>
34
- pgString stmtName <>
32
+ putPgString portalName <>
33
+ putPgString stmtName <>
35
34
-- `1` means that the specified format code is applied to all parameters
36
- int16BE 1 <>
35
+ putInt16BE 1 <>
37
36
encodeFormat paramFormat <>
38
- int16BE (fromIntegral $ V. length values) <>
37
+ putInt16BE (fromIntegral $ V. length values) <>
39
38
fold (encodeValue <$> values) <>
40
39
-- `1` means that the specified format code is applied to all
41
40
-- result columns (if any)
42
- int16BE 1 <>
41
+ putInt16BE 1 <>
43
42
encodeFormat resultFormat
44
43
encodeClientMessage (CloseStatement (StatementName stmtName))
45
- = prependHeader ' C' $ char8 ' S' <> pgString stmtName
44
+ = prependHeader ' C' $ putChar8 ' S' <> putPgString stmtName
46
45
encodeClientMessage (ClosePortal (PortalName portalName))
47
- = prependHeader ' C' $ char8 ' P' <> pgString portalName
46
+ = prependHeader ' C' $ putChar8 ' P' <> putPgString portalName
48
47
encodeClientMessage (DescribeStatement (StatementName stmtName))
49
- = prependHeader ' D' $ char8 ' S' <> pgString stmtName
48
+ = prependHeader ' D' $ putChar8 ' S' <> putPgString stmtName
50
49
encodeClientMessage (DescribePortal (PortalName portalName))
51
- = prependHeader ' D' $ char8 ' P' <> pgString portalName
50
+ = prependHeader ' D' $ putChar8 ' P' <> putPgString portalName
52
51
encodeClientMessage (Execute (PortalName portalName) (RowsToReceive rows))
53
52
= prependHeader ' E' $
54
- pgString portalName <>
55
- int32BE rows
53
+ putPgString portalName <>
54
+ putInt32BE rows
56
55
encodeClientMessage Flush
57
56
= prependHeader ' H' mempty
58
57
encodeClientMessage (Parse (StatementName stmtName) (StatementSQL stmt) oids)
59
58
= prependHeader ' P' $
60
- pgString stmtName <>
61
- pgString stmt <>
62
- int16BE (fromIntegral $ V. length oids) <>
63
- fold (int32BE . unOid <$> oids)
59
+ putPgString stmtName <>
60
+ putPgString stmt <>
61
+ putInt16BE (fromIntegral $ V. length oids) <>
62
+ fold (putInt32BE . unOid <$> oids)
64
63
encodeClientMessage (PasswordMessage passtext)
65
- = prependHeader ' p' $ pgString $ getPassword passtext
64
+ = prependHeader ' p' $ putPgString $ getPassword passtext
66
65
where
67
66
getPassword (PasswordPlain p) = p
68
67
getPassword (PasswordMD5 p) = p
69
68
encodeClientMessage (SimpleQuery (StatementSQL stmt))
70
- = prependHeader ' Q' $ pgString stmt
69
+ = prependHeader ' Q' $ putPgString stmt
71
70
encodeClientMessage Sync
72
71
= prependHeader ' S' mempty
73
72
encodeClientMessage Terminate
74
73
= prependHeader ' X' mempty
75
74
76
75
-- Encodes single data values. Length `-1` indicates a NULL parameter value.
77
76
-- 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
77
+ encodeValue :: B. ByteString -> Encode
78
+ encodeValue v | B. null v = putInt32BE (- 1 )
79
+ | otherwise = putInt32BE (fromIntegral $ B. length v)
80
+ <> putByteString v
82
81
83
- encodeFormat :: Format -> Builder
84
- encodeFormat Text = int16BE 0
85
- encodeFormat Binary = int16BE 1
82
+ encodeFormat :: Format -> Encode
83
+ encodeFormat Text = putInt16BE 0
84
+ encodeFormat Binary = putInt16BE 1
86
85
87
86
----------
88
87
-- Utils
89
88
---------
90
89
91
- -- | C-like string
92
- pgString :: B. ByteString -> Builder
93
- pgString s = byteString s <> word8 0
94
-
95
- prependHeader :: Char -> Builder -> Builder
96
- prependHeader c builder =
97
- let payload = toLazyByteString builder
98
- -- Length includes itself but not the first message-type byte
99
- len = 4 + fromIntegral (BL. length payload)
100
- in char8 c <> int32BE len <> lazyByteString payload
90
+ prependHeader :: Char -> Encode -> Encode
91
+ prependHeader c payload =
92
+ -- Length includes itself but not the first message-type byte
93
+ let len = 4 + fromIntegral (getEncodeLen payload)
94
+ in putChar8 c <> putInt32BE len <> payload
101
95
0 commit comments