@@ -3,26 +3,31 @@ module Database.PostgreSQL.Protocol.Types where
3
3
import Data.Word (Word32 , Word8 )
4
4
import Data.Int (Int32 , Int16 )
5
5
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 )
8
8
9
9
-- 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 )
15
15
16
16
-- 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 )
21
20
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 )
24
28
25
29
newtype RowsCount = RowsCount Word deriving (Show )
30
+ newtype RowsToReceive = RowsToReceive Int32 deriving (Show )
26
31
27
32
-- | Information about completed command.
28
33
data CommandResult
@@ -42,7 +47,7 @@ data CommandResult
42
47
-- For more information about additional parameters see documentation.
43
48
data ConnectionParameters = ConnectionParameters
44
49
{ paramServerVersion :: ServerVersion
45
- , paramServerEncoding :: B. ByteString -- ^ character set name
50
+ , paramServerEncoding :: ByteString -- ^ character set name
46
51
, paramIntegerDatetimes :: Bool -- ^ True if integer datetimes used
47
52
} deriving (Show )
48
53
@@ -54,30 +59,34 @@ instance Show ServerVersion where
54
59
show major ++ " ." ++ show minor ++ " ." ++ show revision
55
60
56
61
data TransactionStatus
62
+ -- | not in a transaction block
57
63
= 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)
59
68
| TransactionFailed
60
69
deriving (Show )
61
70
62
71
data Format = Text | Binary
63
72
deriving (Show )
64
73
65
74
-- 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
+
67
77
data AuthResponse
68
78
= AuthenticationOk
69
79
| AuthenticationCleartextPassword
70
80
| AuthenticationMD5Password MD5Salt
71
81
| AuthenticationGSS
72
82
| AuthenticationSSPI
73
- -- TODO improve
74
- | AuthenticationGSSContinue B. ByteString
83
+ | AuthenticationGSSContinue ByteString
75
84
deriving (Show )
76
85
77
86
data ClientMessage
78
87
= Bind PortalName StatementName
79
88
Format -- parameter format code, one format for all
80
- (V. Vector B. ByteString ) -- the values of parameters
89
+ (Vector ByteString ) -- the values of parameters
81
90
Format -- to apply code to all result columns
82
91
-- Postgres use one command `close` for closing both statements and
83
92
-- portals, but we distinguish them
@@ -87,61 +96,68 @@ data ClientMessage
87
96
-- and portals, but we distinguish them
88
97
| DescribeStatement StatementName
89
98
| DescribePortal PortalName
90
- | Execute PortalName
99
+ | Execute PortalName RowsToReceive
91
100
| Flush
92
- | Parse StatementName StatementSQL (V. Vector Oid )
93
- -- TODO maybe distinguish plain passwords and encrypted
101
+ | Parse StatementName StatementSQL (Vector Oid )
94
102
| PasswordMessage PasswordText
95
103
-- PostgreSQL names it `Query`
96
104
| SimpleQuery StatementSQL
97
105
| Sync
98
106
| Terminate
99
107
deriving (Show )
100
108
109
+ data CancelRequest = CancelRequest ServerProcessId ServerSecretKey
110
+ deriving (Show )
111
+
101
112
data StartMessage
102
113
= StartupMessage Username DatabaseName
103
114
| SSLRequest
104
115
deriving (Show )
105
116
F438
106
117
data ServerMessage
107
- = BackendKeyData ServerProccessId ServerSecretKey
118
+ = BackendKeyData ServerProcessId ServerSecretKey
108
119
| BindComplete
109
120
| CloseComplete
110
121
| CommandComplete CommandResult
111
- | DataRow (V. Vector B. ByteString ) -- the values of a result
122
+ | DataRow (Vector ByteString )
112
123
| EmptyQueryResponse
113
124
| ErrorResponse ErrorDesc
114
125
| NoData
115
126
| 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
123
130
| ParseComplete
124
131
| PortalSuspended
125
132
| ReadForQuery TransactionStatus
126
- | RowDescription (V. Vector FieldDescription )
133
+ | RowDescription (Vector FieldDescription )
127
134
deriving (Show )
128
135
136
+ data Notification = Notification
137
+ { notificationProcessId :: ServerProcessId
138
+ , notificationChannel :: ChannelName
139
+ , notificationPayload :: ByteString
140
+ } deriving (Show )
141
+
129
142
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.
133
147
, 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.
135
150
, fieldColumnNumber :: Int16
136
- -- Oid type
151
+ -- | The object ID of the field's data type.
137
152
, 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
139
154
-- values denote variable-width types.
140
155
, fieldSize :: Int16
141
- -- The type modifier (see pg_attribute.atttypmod).
156
+ -- | The type modifier (see pg_attribute.atttypmod).
142
157
, 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.
145
161
, fieldFormat :: Format
146
162
} deriving (Show )
147
163
@@ -163,52 +179,49 @@ data NoticeSeverity
163
179
164
180
data ErrorDesc = ErrorDesc
165
181
{ 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
170
186
, errorPosition :: Maybe Int
171
187
, 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
180
196
, errorSourceLine :: Maybe Int
181
- , errorRoutine :: Maybe B. ByteString
197
+ , errorSourceRoutine :: Maybe ByteString
182
198
} deriving (Show )
183
199
184
200
data NoticeDesc = NoticeDesc
185
201
{ 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
190
206
, noticePosition :: Maybe Int
191
207
, 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
200
216
, noticeSourceLine :: Maybe Int
201
- , noticeRoutine :: Maybe B. ByteString
217
+ , noticeSourceRoutine :: Maybe ByteString
202
218
} deriving (Show )
203
219
204
220
-- TODO
205
- -- * CancelRequest
206
221
-- * COPY subprotocol commands
207
222
-- * function call, is deprecated by postgres
208
223
-- * AuthenticationKerberosV5 IS deprecated by postgres
209
224
-- * 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
212
225
-- * NOTICE bind command can have different formats for parameters and results
213
226
-- but we assume that there will be one format for all.
214
227
-- * We dont store parameters of connection that may change after startup
0 commit comments