8000 Full protocal types · postgres-haskell/postgres-wire@bdf2751 · GitHub
[go: up one dir, main page]

Skip to content

Commit bdf2751

Browse files
Full protocal types
1 parent da8bba2 commit bdf2751

File tree

1 file changed

+83
-70
lines changed
  • src/Database/PostgreSQL/Protocol

1 file changed

+83
-70
lines changed

src/Database/PostgreSQL/Protocol/Types.hs

Lines changed: 83 additions & 70 deletions
F438
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,31 @@ module Database.PostgreSQL.Protocol.Types where
33
import Data.Word (Word32, Word8)
44
import Data.Int (Int32, Int16)
55
import Data.Hashable (Hashable)
6-
import qualified Data.ByteString as B
7-
import qualified Data.Vector as V
6+
import Data.ByteString (ByteString)
7+
import Data.Vector (Vector)
88

99
-- Common
10-
newtype Oid = Oid { unOid :: Int32 } deriving (Show)
11-
newtype StatementName = StatementName B.ByteString deriving (Show)
12-
newtype StatementSQL = StatementSQL B.ByteString deriving (Show, Eq, Hashable)
13-
newtype PortalName = PortalName B.ByteString deriving (Show)
14-
newtype ChannelName = ChannelName B.ByteString deriving (Show)
10+
newtype Oid = Oid { unOid :: Int32 } deriving (Show)
11+
newtype StatementName = StatementName ByteString deriving (Show)
12+
newtype StatementSQL = StatementSQL ByteString deriving (Show, Eq, Hashable)
13+
newtype PortalName = PortalName ByteString deriving (Show)
14+
newtype ChannelName = ChannelName ByteString deriving (Show)
1515

1616
-- Startup phase
17-
newtype Username = Username B.ByteString deriving (Show)
18-
newtype DatabaseName = DatabaseName B.ByteString deriving (Show)
19-
newtype PasswordText = PasswordText B.ByteString deriving (Show)
20-
newtype MD5Salt = MD5Salt Word32 deriving (Show)
17+
newtype Username = Username ByteString deriving (Show)
18+
newtype DatabaseName = DatabaseName ByteString deriving (Show)
19+
newtype MD5Salt = MD5Salt ByteString deriving (Show)
2120

22-
newtype ServerProccessId = ServerProcessId Int32 deriving (Show)
23-
newtype ServerSecretKey = ServerSecrecKey Int32 deriving (Show)
21+
data PasswordText
22+
= PasswordPlain ByteString
23+
| PasswordMD5 ByteString
24+
deriving (Show)
25+
26+
newtype ServerProcessId = ServerProcessId Int32 deriving (Show)
27+
newtype ServerSecretKey = ServerSecretKey Int32 deriving (Show)
2428

2529
newtype RowsCount = RowsCount Word deriving (Show)
30+
newtype RowsToReceive = RowsToReceive Int32 deriving (Show)
2631

2732
-- | Information about completed command.
2833
data CommandResult
@@ -42,7 +47,7 @@ data CommandResult
4247
-- For more information about additional parameters see documentation.
4348
data ConnectionParameters = ConnectionParameters
4449
{ paramServerVersion :: ServerVersion
45-
, paramServerEncoding :: B.ByteString -- ^ character set name
50+
, paramServerEncoding :: ByteString -- ^ character set name
4651
, paramIntegerDatetimes :: Bool -- ^ True if integer datetimes used
4752
} deriving (Show)
4853

@@ -54,30 +59,34 @@ instance Show ServerVersion where
5459
show major ++ "." ++ show minor ++ "." ++ show revision
5560

5661
data TransactionStatus
62+
-- | not in a transaction block
5763
= TransactionIdle
58-
| TransactionInProgress
64+
-- | in a transaction block
65+
| TransactionInBlock
66+
-- | in a failed transaction block
67+
-- (queries will be rejected until block is ended)
5968
| TransactionFailed
6069
deriving (Show)
6170

6271
data Format = Text | Binary
6372
deriving (Show)
6473

6574
-- All the commands have the same names as presented in the official
66-
-- postgres documentation except explicit exclusions
75+
-- postgres documentation except explicit exclusions.
76+
6777
data AuthResponse
6878
= AuthenticationOk
6979
| AuthenticationCleartextPassword
7080
| AuthenticationMD5Password MD5Salt
7181
| AuthenticationGSS
7282
| AuthenticationSSPI
73-
-- TODO improve
74-
| AuthenticationGSSContinue B.ByteString
83+
| AuthenticationGSSContinue ByteString
7584
deriving (Show)
7685

7786
data ClientMessage
7887
= Bind PortalName StatementName
7988
Format -- parameter format code, one format for all
80-
(V.Vector B.ByteString) -- the values of parameters
89+
(Vector ByteString) -- the values of parameters
8190
Format -- to apply code to all result columns
8291
-- Postgres use one command `close` for closing both statements and
8392
-- portals, but we distinguish them
@@ -87,61 +96,68 @@ data ClientMessage
8796
-- and portals, but we distinguish them
8897
| DescribeStatement StatementName
8998
| DescribePortal PortalName
90-
| Execute PortalName
99+
| Execute PortalName RowsToReceive
91100
| Flush
92-
| Parse StatementName StatementSQL (V.Vector Oid)
93-
-- TODO maybe distinguish plain passwords and encrypted
101+
| Parse StatementName StatementSQL (Vector Oid)
94102
| PasswordMessage PasswordText
95103
-- PostgreSQL names it `Query`
96104
| SimpleQuery StatementSQL
97105
| Sync
98106
| Terminate
99107
deriving (Show)
100108

109+
data CancelRequest = CancelRequest ServerProcessId ServerSecretKey
110+
deriving (Show)
111+
101112
data StartMessage
102113
= StartupMessage Username DatabaseName
103114
| SSLRequest
104115
deriving (Show)
105116

106117
data ServerMessage
107-
= BackendKeyData ServerProccessId ServerSecretKey
118+
= BackendKeyData ServerProcessId ServerSecretKey
108119
| BindComplete
109120
| CloseComplete
110121
| CommandComplete CommandResult
111-
| DataRow (V.Vector B.ByteString) -- the values of a result
122+
| DataRow (Vector ByteString)
112123
| EmptyQueryResponse
113124
| ErrorResponse ErrorDesc
114125
| NoData
115126
| NoticeResponse NoticeDesc
116-
| NotificationResponse
117-
ServerProccessId
118-
ChannelName
119-
B.ByteString -- payload - does not have structure
120-
| ParameterDescription (V.Vector Oid)
121-
-- parameter name and its value
122-
| ParameterStatus B.ByteString B.ByteString
127+
| NotificationResponse Notification
128+
| ParameterDescription (Vector Oid)
129+
| ParameterStatus ByteString ByteString -- name and value
123130
| ParseComplete
124131
| PortalSuspended
125132
| ReadForQuery TransactionStatus
126-
| RowDescription (V.Vector FieldDescription)
133+
| RowDescription (Vector FieldDescription)
127134
deriving (Show)
128135

136+
data Notification = Notification
137+
{ notificationProcessId :: ServerProcessId
138+
, notificationChannel :: ChannelName
139+
, notificationPayload :: ByteString
140+
} deriving (Show)
141+
129142
data FieldDescription = FieldDescription {
130-
-- the name
131-
fieldName :: B.ByteString
132-
-- the object ID of the table
143+
-- | the field name
144+
fieldName :: ByteString
145+
-- | If the field can be identified as a column of a specific table,
146+
-- the object ID of the table; otherwise zero.
133147
, fieldTableOid :: Oid
134-
-- the attribute number of the column
148+
-- | If the field can be identified as a column of a specific table,
149+
-- the attribute number of the column; otherwise zero.
135150
, fieldColumnNumber :: Int16
136-
-- Oid type
151+
-- | The object ID of the field's data type.
137152
, fieldTypeOid :: Oid
138-
-- The data type size (see pg_type.typlen). Note that negative
153+
-- | The data type size (see pg_type.typlen). Note that negative
139154
-- values denote variable-width types.
140155
, fieldSize :: Int16
141-
-- The type modifier (see pg_attribute.atttypmod).
156+
-- | The type modifier (see pg_attribute.atttypmod).
142157
, fieldMode :: Int32
143-
-- In a RowDescription returned from the statement variant of Describe,
144-
-- the format code is not yet known and will always be zero.
158+
-- | The format code being used for the field. In a RowDescription
159+
-- returned from the statement variant of Describe, the format code
160+
-- is not yet known and will always be zero.
145161
, fieldFormat :: Format
146162
} deriving (Show)
147163

@@ -163,52 +179,49 @@ data NoticeSeverity
163179

164180
data ErrorDesc = ErrorDesc
165181
{ errorSeverity :: ErrorSeverity
166-
, errorCode :: B.ByteString
167-
, errorMessage :: B.ByteString
168-
, errorDetail :: Maybe B.ByteString
169-
, errorHint :: Maybe B.ByteString
182+
, errorCode :: ByteString
183+
, errorMessage :: ByteString
184+
, errorDetail :: Maybe ByteString
185+
, errorHint :: Maybe ByteString
170186
, errorPosition :: Maybe Int
171187
, errorInternalPosition :: Maybe Int
172-
, errorInternalQuery :: Maybe B.ByteString
173-
, errorContext :: Maybe B.ByteString
174-
, errorSchema :: Maybe B.ByteString
175-
, errorTable :: Maybe B.ByteString
176-
, errorColumn :: Maybe B.ByteString
177-
, errorDataType :: Maybe B.ByteString
178-
, errorConstraint :: Maybe B.ByteString
179-
, errorSourceFilename :: Maybe B.ByteString
188+
, errorInternalQuery :: Maybe ByteString
189+
, errorContext :: Maybe ByteString
190+
, errorSchema :: Maybe ByteString
191+
, errorTable :: Maybe ByteString
192+
, errorColumn :: Maybe ByteString
193+
, errorDataType :: Maybe ByteString
194+
, errorConstraint :: Maybe ByteString
195+
, errorSourceFilename :: Maybe ByteString
180196
, errorSourceLine :: Maybe Int
181-
, errorRoutine :: Maybe B.ByteString
197+
, errorSourceRoutine :: Maybe ByteString
182198
} deriving (Show)
183199

184200
data NoticeDesc = NoticeDesc
185201
{ noticeSeverity :: NoticeSeverity
186-
, noticeCode :: B.ByteString
187-
, noticeMessage :: B.ByteString
188-
, noticeDetail :: Maybe B.ByteString
189-
, noticeHint :: Maybe B.ByteString
202+
, noticeCode :: ByteString
203+
, noticeMessage :: ByteString
204+
, noticeDetail :: Maybe ByteString
205+
, noticeHint :: Maybe ByteString
190206
, noticePosition :: Maybe Int
191207
, noticeInternalPosition :: Maybe Int
192-
, noticeInternalQuery :: Maybe B.ByteString
193-
, noticeContext :: Maybe B.ByteString
194-
, noticeSchema :: Maybe B.ByteString
195-
, noticeTable :: Maybe B.ByteString
196-
, noticeColumn :: Maybe B.ByteString
197-
, noticeDataType :: Maybe B.ByteString
198-
, noticeConstraint :: Maybe B.ByteString
199-
, noticeSourceFilename :: Maybe B.ByteString
208+
, noticeInternalQuery :: Maybe ByteString
209+
, noticeContext :: Maybe ByteString
210+
, noticeSchema :: Maybe ByteString
211+
, noticeTable :: Maybe ByteString
212+
, noticeColumn :: Maybe ByteString
213+
, noticeDataType :: Maybe ByteString
214+
, noticeConstraint :: Maybe ByteString
215+
, noticeSourceFilename :: Maybe ByteString
200216
, noticeSourceLine :: Maybe Int
201-
, noticeRoutine :: Maybe B.ByteString
217+
, noticeSourceRoutine :: Maybe ByteString
202218
} deriving (Show)
203219

204220
-- TODO
205-
-- * CancelRequest
206221
-- * COPY subprotocol commands
207222
-- * function call, is deprecated by postgres
208223
-- * AuthenticationKerberosV5 IS deprecated by postgres
209224
-- * AuthenticationSCMCredential IS deprecated since postgres 9.1
210-
-- * NOTICE execute command can have number of rows to receive, but we
211-
-- dont support this feature
212225
-- * NOTICE bind command can have different formats for parameters and results
213226
-- but we assume that there will be one format for all.
214227
-- * We dont store parameters of connection that may change after startup

0 commit comments

Comments
 (0)
0