-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBackend.roc
More file actions
386 lines (307 loc) Β· 7.58 KB
/
Copy pathBackend.roc
File metadata and controls
386 lines (307 loc) Β· 7.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
module [
header,
message,
Message,
KeyData,
Status,
RowField,
ParameterField,
Error,
]
import Bytes.Decode exposing [
Decode,
await,
map,
succeed,
fail,
loop,
u8,
i16,
i32,
cStr,
take,
]
Message : [
AuthOk,
AuthCleartextPassword,
AuthUnsupported,
ParameterStatus { name : Str, value : Str },
BackendKeyData KeyData,
ReadyForQuery Status,
ErrorResponse Error,
ParseComplete,
BindComplete,
NoData,
RowDescription (List RowField),
ParameterDescription (List ParameterField),
DataRow (List (List U8)),
PortalSuspended,
CommandComplete Str,
EmptyQueryResponse,
CloseComplete,
]
header : Decode { msgType : U8, len : I32 } _
header =
msgType <- await u8
len <- map i32
{ msgType, len: len - 4 }
message : U8 -> Decode Message _
message = \msgType ->
when msgType is
'R' ->
authRequest
'S' ->
paramStatus
'K' ->
backendKeyData
'Z' ->
readyForQuery
'E' ->
errorResponse
'1' ->
succeed ParseComplete
'2' ->
succeed BindComplete
'n' ->
succeed NoData
'T' ->
rowDescription
't' ->
parameterDescription
'D' ->
dataRow
's' ->
succeed PortalSuspended
'C' ->
commandComplete
'I' ->
succeed EmptyQueryResponse
'3' ->
succeed CloseComplete
_ ->
fail (UnrecognizedBackendMessage msgType)
authRequest : Decode Message _
authRequest =
authType <- map i32
when authType is
0 ->
AuthOk
3 ->
AuthCleartextPassword
_ ->
AuthUnsupported
paramStatus : Decode Message _
paramStatus =
name <- await cStr
value <- await cStr
succeed (ParameterStatus { name, value })
KeyData : { processId : I32, secretKey : I32 }
backendKeyData : Decode Message _
backendKeyData =
processId <- await i32
secretKey <- await i32
succeed (BackendKeyData { processId, secretKey })
Status : [Idle, TransactionBlock, FailedTransactionBlock]
readyForQuery : Decode Message _
readyForQuery =
status <- await u8
when status is
'I' ->
succeed (ReadyForQuery Idle)
'T' ->
succeed (ReadyForQuery TransactionBlock)
'E' ->
succeed (ReadyForQuery FailedTransactionBlock)
_ ->
fail (UnrecognizedBackendStatus status)
Error : {
localizedSeverity : Str,
severity : Result ErrorSeverity {},
code : Str,
message : Str,
detail : Result Str {},
hint : Result Str {},
position : Result U32 {},
internalPosition : Result U32 {},
internalQuery : Result Str {},
ewhere : Result Str {},
schemaName : Result Str {},
tableName : Result Str {},
columnName : Result Str {},
dataTypeName : Result Str {},
constraintName : Result Str {},
file : Result Str {},
line : Result Str {},
routine : Result Str {},
}
errorResponse : Decode Message _
errorResponse =
dict <- await knownStrFields
localizedSeverity <- 'S' |> requiredField dict
severity <- 'V' |> optionalFieldWith dict decodeSeverity
code <- 'C' |> requiredField dict
msg <- 'M' |> requiredField dict
position <- 'P' |> optionalFieldWith dict Str.toU32
internalPosition <- 'p' |> optionalFieldWith dict Str.toU32
ErrorResponse {
localizedSeverity,
severity,
code,
message: msg,
detail: 'D' |> optionalField dict,
hint: 'H' |> optionalField dict,
position,
internalPosition,
internalQuery: 'q' |> optionalField dict,
ewhere: 'W' |> optionalField dict,
schemaName: 's' |> optionalField dict,
tableName: 't' |> optionalField dict,
columnName: 'c' |> optionalField dict,
dataTypeName: 'd' |> optionalField dict,
constraintName: 'n' |> optionalField dict,
file: 'F' |> optionalField dict,
line: 'L' |> optionalField dict,
routine: 'R' |> optionalField dict,
}
|> succeed
optionalField = \fieldId, dict ->
when Dict.get dict fieldId is
Ok value ->
Ok value
Err _ ->
Err {}
optionalFieldWith = \fieldId, dict, validate, callback ->
result = Dict.get dict fieldId
when result is
Ok value ->
when validate value is
Ok validated ->
callback (Ok validated)
Err err ->
fail err
Err _ ->
callback (Err {})
requiredField = \fieldId, dict, callback ->
result = Dict.get dict fieldId
when result is
Ok value ->
callback value
Err _ ->
fail (MissingField fieldId)
ErrorSeverity : [
Error,
Fatal,
Panic,
Warning,
Notice,
Debug,
Info,
Log,
]
decodeSeverity = \str ->
when str is
"ERROR" ->
Ok Error
"FATAL" ->
Ok Fatal
"PANIC" ->
Ok Panic
"WARNING" ->
Ok Warning
"NOTICE" ->
Ok Notice
"DEBUG" ->
Ok Debug
"INFO" ->
Ok Info
"LOG" ->
Ok Log
_ ->
Err (InvalidSeverity str)
knownStrFields : Decode (Dict U8 Str) _
knownStrFields =
collected <- loop (Dict.empty {})
fieldId <- await u8
if fieldId == 0 then
succeed (Done collected)
else
value <- map cStr
collected
|> Dict.insert fieldId value
|> Loop
RowField : {
name : Str,
column : Result { tableOid : I32, attributeNumber : I16 } [NotAColumn],
dataTypeOid : I32,
dataTypeSize : I16,
typeModifier : I32,
formatCode : I16,
}
rowDescription : Decode Message _
rowDescription =
fieldCount <- await i16
fixedList fieldCount rowField
|> map RowDescription
rowField : Decode RowField _
rowField =
name <- await cStr
tableOid <- await i32
attributeNumber <- await i16
dataTypeOid <- await i32
dataTypeSize <- await i16
typeModifier <- await i32
formatCode <- map i16
column =
if tableOid != 0 && attributeNumber != 0 then
Ok { tableOid, attributeNumber }
else
Err NotAColumn
{
name,
column,
dataTypeOid,
dataTypeSize,
typeModifier,
formatCode,
}
ParameterField : {
dataTypeOid : I32,
}
parameterDescription : Decode Message _
parameterDescription =
fieldCount <- await i16
if fieldCount == 0 then
succeed (ParameterDescription [])
else
fixedList
fieldCount
parameterField
|> map ParameterDescription
parameterField : Decode ParameterField _
parameterField =
dataTypeOid <- await i32
succeed { dataTypeOid }
dataRow : Decode Message _
dataRow =
columnCount <- await i16
fixedList
columnCount
(
valueLen <- await i32
if valueLen == -1 then
succeed []
else
take (Num.toU64 valueLen) \x -> x
)
|> map DataRow
fixedList = \count, itemDecode ->
collected <- loop (List.withCapacity (Num.toU64 count))
item <- map itemDecode
added = List.append collected item
if List.len added == Num.toU64 count then
Done added
else
Loop added
commandComplete : Decode Message _
commandComplete =
map cStr CommandComplete