postro 0.1.1

Asynchronous Postgres Driver and Utility
Documentation
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
//! Postgres Backend Messages
//!
//! <https://www.postgresql.org/docs/current/protocol-message-formats.html>
use bytes::{Buf, Bytes};

use super::ProtocolError;
use crate::{common::ByteStr, ext::BytesExt};

/// A type that can be decoded into postgres backend message.
pub trait BackendProtocol: Sized + std::fmt::Debug {
    /// Try decode given bytes into message.
    ///
    /// Note that `body` is only the main body, **excluding** message type and length.
    fn decode(msgtype: u8, body: Bytes) -> Result<Self, ProtocolError>;
}

/// Postgres backend messages.
pub enum BackendMessage {
    /// Identifies the message as an authentication request.
    Authentication(Authentication),
    /// Identifies the message as cancellation key data.
    BackendKeyData(BackendKeyData),
    /// Identifies the message as a Bind-complete indicator.
    BindComplete(BindComplete),
    /// Identifies the message as a Close-complete indicator.
    CloseComplete(CloseComplete),
    /// Identifies the message as a command-completed response.
    CommandComplete(CommandComplete),
    /// Identifies the message as a data row.
    DataRow(DataRow),
    /// Identifies the message as an error.
    ErrorResponse(ErrorResponse),
    /// Identifies the message as a response to an empty query string.
    EmptyQueryResponse(EmptyQueryResponse),
    /// Identifies the message as a protocol version negotiation message.
    NegotiateProtocolVersion(NegotiateProtocolVersion),
    /// Identifies the message as a no-data indicator.
    NoData(NoData),
    /// Identifies the message as a notice.
    NoticeResponse(NoticeResponse),
    /// Identifies the message as a parameter description.
    ParameterDescription(ParameterDescription),
    /// Identifies the message as a run-time parameter status report
    ParameterStatus(ParameterStatus),
    /// Identifies the message as a Parse-complete indicator.
    ParseComplete(ParseComplete),
    /// Identifies the message as a portal-suspended indicator.
    PortalSuspended(PortalSuspended),
    /// Identifies the message type. ReadyForQuery is sent whenever the backend is ready for a new query cycle.
    ReadyForQuery(ReadyForQuery),
    /// Identifies the message as a row description
    RowDescription(RowDescription),
}

macro_rules! match_backend {
    ($($name:ident,)*) => {
        impl BackendMessage {
            /// Returns the message type.
            pub const fn msgtype(&self) -> u8 {
                match self {
                    $(Self::$name(_) => $name::MSGTYPE,)*
                }
            }

            /// Get message name from message type.
            ///
            /// Returns `"Unknown"` for unknown message type.
            pub const fn message_name(msgtype: u8) -> &'static str {
                match msgtype {
                    $($name::MSGTYPE => stringify!($name),)*
                    _ => "Unknown",
                }
            }
        }
        impl BackendProtocol for BackendMessage {
            fn decode(msgtype: u8, body: Bytes) -> Result<Self, ProtocolError> {
                let message = match msgtype {
                    $($name::MSGTYPE => Self::$name(<$name as BackendProtocol>::decode(msgtype, body)?),)*
                    _ => return Err(ProtocolError::unknown(msgtype)),
                };
                Ok(message)
            }
        }
        impl std::fmt::Debug for BackendMessage {
            fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                match self {
                    $(Self::$name(e) => std::fmt::Debug::fmt(e, f),)*
                }
            }
        }
    };
}


match_backend! {
    Authentication,
    BackendKeyData,
    BindComplete,
    CloseComplete,
    CommandComplete,
    DataRow,
    ErrorResponse,
    EmptyQueryResponse,
    NegotiateProtocolVersion,
    NoData,
    NoticeResponse,
    ParameterDescription,
    ParameterStatus,
    ParseComplete,
    PortalSuspended,
    ReadyForQuery,
    RowDescription,
}

macro_rules! assert_msgtype {
    ($typ:ident) => {
        if Self::MSGTYPE != $typ {
            return Err(ProtocolError::unexpected(Self::MSGTYPE,$typ))
        }
    };
}

macro_rules! msgtype {
    ($me:ident,$ty:literal) => {
        impl $me {
            #[doc = concat!("`",stringify!($ty),"`")]
            pub const MSGTYPE: u8 = $ty;
        }
    };
}

/// Identifies the message as an authentication request.
#[derive(Debug)]
pub enum Authentication {
    /// Specifies that the authentication was successful.
    Ok,
    /// Specifies that Kerberos V5 authentication is required.
    KerberosV5,
    /// Specifies that a clear-text password is required.
    CleartextPassword,
    /// Specifies that an MD5-encrypted password is required.
    MD5Password {
        /// The salt to use when encrypting the password.
        salt: [u8;4],
    },
    /// Specifies that GSSAPI authentication is required.
    GSS,
    /// GSSAPI or SSPI authentication data.
    GSSContinue {
        data: Bytes,
    },
    /// Specifies that SSPI authentication is required.
    SSPI,
    /// Specifies that SASL authentication is required.
    SASL {
        /// The message body is a list of SASL authentication mechanisms, in the server's order of preference.
        ///
        /// A zero byte is required as terminator after the last authentication mechanism name.
        ///
        /// For each mechanism, there is the following:
        name: Bytes,
    },
    /// Specifies that this message contains a SASL challenge.
    SASLContinue {
        /// SASL data, specific to the SASL mechanism being used.
        data: Bytes,
    },
    /// Specifies that SASL authentication has completed.
    SASLFinal {
        /// SASL outcome "additional data", specific to the SASL mechanism being used.
        data: Bytes,
    },
}

msgtype!(Authentication, b'R');

impl BackendProtocol for Authentication {
    fn decode(msgtype: u8, mut body: Bytes) -> Result<Self,ProtocolError> {
        assert_msgtype!(msgtype);
        let auth = match body.get_u32() {
            0 => Self::Ok,
            2 => Self::KerberosV5,
            3 => Self::CleartextPassword,
            5 => Self::MD5Password { salt: body.get_u32().to_be_bytes(), },
            7 => Self::GSS,
            8 => Self::GSSContinue { data: body },
            9 => Self::SSPI,
            10 => Self::SASL { name: body },
            11 => Self::SASLContinue { data: body },
            12 => Self::SASLFinal { data: body },
            auth => panic!("Unknown Authentication type: \"{auth}\""),
        };
        Ok(auth)
    }
}

/// Identifies the message as cancellation key data.
///
/// The frontend must save these values if it wishes to be able to issue CancelRequest messages later.
#[derive(Clone, Copy)]
pub struct BackendKeyData {
    /// The process ID of this backend.
    pub process_id: u32,
    /// The secret key of this backend.
    pub secret_key: u32,
}

msgtype!(BackendKeyData, b'K');

impl BackendProtocol for BackendKeyData {
    fn decode(msgtype: u8, mut body: Bytes) -> Result<Self,ProtocolError> {
        assert_msgtype!(msgtype);
        Ok(Self {
            process_id: body.get_u32(),
            secret_key: body.get_u32(),
        })
    }
}

/// Identifies the message as a run-time parameter status report.
#[derive(Debug)]
pub struct ParameterStatus {
    /// The name of the run-time parameter being reported.
    pub name: ByteStr,
    /// The current value of the parameter.
    pub value: ByteStr,
}

msgtype!(ParameterStatus, b'S');

impl BackendProtocol for ParameterStatus {
    fn decode(msgtype: u8, mut body: Bytes) -> Result<Self,ProtocolError> {
        assert_msgtype!(msgtype);
        Ok(Self {
            name: body.get_nul_bytestr()?,
            value: body.get_nul_bytestr()?,
        })
    }
}

/// Identifies the message as a notice.
pub struct NoticeResponse {
    /// Raw message body.
    ///
    /// The message body consists of one or more identified fields, followed by a zero byte as a terminator.
    ///
    /// Fields can appear in any order.
    ///
    /// For each field there is the following:
    ///
    /// - `Byte1` A code identifying the field type; if zero, this is the message terminator and no string follows.
    ///
    /// The presently defined field types are listed in [Section 53.8][53_8].
    ///
    /// Since more field types might be added in future, frontends should silently ignore fields of unrecognized type.
    ///
    /// - `String` The field value.
    ///
    /// [53_8]: https://www.postgresql.org/docs/current/protocol-error-fields.html
    pub body: Bytes
}

msgtype!(NoticeResponse, b'N');

impl NoticeResponse {
    pub fn new(body: Bytes) -> Self {
        Self { body }
    }
}

impl BackendProtocol for NoticeResponse {
    fn decode(msgtype: u8, body: Bytes) -> Result<Self,ProtocolError> {
        assert_msgtype!(msgtype);
        Ok(Self { body })
    }
}

/// Identifies the message as an error.
pub struct ErrorResponse {
    /// Raw message body.
    ///
    /// The message body consists of one or more identified fields, followed by a zero byte as a terminator.
    /// Fields can appear in any order.
    ///
    /// For each field there is the following:
    ///
    /// - `Byte1` A code identifying the field type; if zero, this is the message terminator and no string follows.
    ///
    /// The presently defined field types are listed in [Section 53.8][53_8].
    ///
    /// Since more field types might be added in future, frontends should silently ignore fields of unrecognized type.
    ///
    /// - `String` The field value.
    ///
    /// [53_8]: https://www.postgresql.org/docs/current/protocol-error-fields.html
    pub body: Bytes,
}

msgtype!(ErrorResponse, b'E');

impl ErrorResponse {
    pub fn new(body: Bytes) -> Self {
        Self { body }
    }
}

impl BackendProtocol for ErrorResponse {
    fn decode(msgtype: u8, body: Bytes) -> Result<Self,ProtocolError> {
        assert_msgtype!(msgtype);
        Ok(Self { body })
    }
}

/// Identifies the message as a row description
pub struct RowDescription {
    /// Raw message body.
    ///
    /// - `Int16` Specifies the number of fields in a row (can be zero).
    ///
    /// For each field, there is the following:
    ///
    /// - `String` The field name.
    /// - `Int32` If the field can be identified as a column of a specific table,
    ///   the object ID of the table; otherwise zero.
    /// - `Int16` If the field can be identified as a column of a specific table,
    ///   the attribute number of the column; otherwise zero.
    /// - `Int32` The object ID of the field's data type.
    /// - `Int16` The data type size (see pg_type.typlen). Note that negative values denote variable-width types.
    /// - `Int32` The type modifier (see pg_attribute.atttypmod). The meaning of the modifier is type-specific.
    /// - `Int16` The format code being used for the field. Currently will be zero (text) or one (binary).
    ///   In a RowDescription returned from the statement variant of Describe,
    ///   the format code is not yet known and will always be zero.
    pub body: Bytes,
}

msgtype!(RowDescription, b'T');

impl BackendProtocol for RowDescription {
    fn decode(msgtype: u8, body: Bytes) -> Result<Self, ProtocolError> {
        assert_msgtype!(msgtype);
        Ok(Self { body })
    }
}

/// Identifies the message as a data row.
pub struct DataRow {
    /// Raw row buffer.
    ///
    /// - `Int16` The number of column values that follow (possibly zero).
    ///
    /// Next, the following pair of fields appear for each column:
    ///
    /// - `Int32` The length of the column value, in bytes (this count does not include itself).
    ///
    /// Can be zero. As a special case, -1 indicates a NULL column value. No value bytes follow in the NULL case.
    ///
    /// - `Byte[n]` The value of the column, in the format indicated by the associated format code.
    pub body: Bytes,
}

msgtype!(DataRow, b'D');

impl BackendProtocol for DataRow {
    fn decode(msgtype: u8, body: Bytes) -> Result<Self, ProtocolError> {
        assert_msgtype!(msgtype);
        Ok(Self { body })
    }
}

/// Identifies the message as a command-completed response.
#[derive(Debug)]
pub struct CommandComplete {
    /// The command tag. This is usually a single word that identifies which SQL command was completed.
    ///
    /// For an `INSERT` command, the tag is `INSERT oid rows`, where `rows` is the number of rows inserted.
    /// `oid` used to be the object ID of the inserted row if `rows` was 1 and the target table had OIDs,
    /// but OIDs system columns are not supported anymore; therefore `oid` is always 0.
    ///
    /// For a `DELETE` command, the tag is `DELETE rows` where `rows` is the number of rows deleted.
    ///
    /// For an `UPDATE` command, the tag is `UPDATE rows` where `rows` is the number of rows updated.
    ///
    /// For a `MERGE` command, the tag is `MERGE rows` where `rows` is the
    /// number of rows inserted, updated, or deleted.
    ///
    /// For a `SELECT` or `CREATE TABLE AS` command, the tag is `SELECT rows`
    /// where `rows` is the number of rows retrieved.
    ///
    /// For a `MOVE` command, the tag is `MOVE rows` where `rows` is the number of rows
    /// the cursor's position has been changed by.
    ///
    /// For a `FETCH` command, the tag is `FETCH rows` where `rows` is the number of rows that have
    /// been retrieved from the cursor.
    ///
    /// For a `COPY` command, the tag is `COPY rows` where `rows` is the number of rows copied.
    /// (Note: the row count appears only in PostgreSQL 8.2 and later.)
    pub tag: ByteStr,
}

msgtype!(CommandComplete, b'C');

impl BackendProtocol for CommandComplete {
    fn decode(msgtype: u8, mut body: Bytes) -> Result<Self, ProtocolError> {
        assert_msgtype!(msgtype);
        Ok(Self {
            tag: body.get_nul_bytestr()?,
        })
    }
}

/// Identifies the message as a protocol version negotiation message.
#[derive(Debug)]
pub struct NegotiateProtocolVersion {
    /// Newest minor protocol version supported by the server for the major protocol version requested by the client.
    pub minor: u32,
    /// Number of protocol options not recognized by the server.
    pub len: u32,
    /// Raw buffer for option not recognized by the server.
    ///
    /// There is the following:
    ///
    /// - `String` The option name.
    pub opt_names: Bytes,
}

msgtype!(NegotiateProtocolVersion, b'v');

impl BackendProtocol for NegotiateProtocolVersion {
    fn decode(msgtype: u8, mut body: Bytes) -> Result<Self,ProtocolError> {
        assert_msgtype!(msgtype);
        Ok(Self {
            minor: body.get_u32(),
            len: body.get_u32(),
            opt_names: body,
        })
    }
}

/// Identifies the message as a parameter description.
#[derive(Debug)]
pub struct ParameterDescription {
    /// The number of parameters used by the statement (can be zero).
    pub param_len: u16,
    /// Raw buffer for message body.
    ///
    /// For each parameter, there is the following:
    ///
    /// - `Int32` Specifies the object ID of the parameter data type.
    pub oids: Bytes,
}

msgtype!(ParameterDescription, b't');

impl BackendProtocol for ParameterDescription {
    fn decode(msgtype: u8, mut body: Bytes) -> Result<Self,ProtocolError> {
        assert_msgtype!(msgtype);
        Ok(Self {
            param_len: body.get_u16(),
            oids: body,
        })
    }
}

/// Identifies the message type. ReadyForQuery is sent whenever the backend is ready for a new query cycle.
pub struct ReadyForQuery {
    /// Current backend transaction status indicator.
    ///
    /// Possible values are 'I' if idle (not in a transaction block);
    /// 'T' if in a transaction block;
    /// or 'E' if in a failed transaction block (queries will be rejected until block is ended).
    pub tx_status: u8
}

msgtype!(ReadyForQuery, b'Z');

impl BackendProtocol for ReadyForQuery {
    fn decode(msgtype: u8, mut body: Bytes) -> Result<Self,ProtocolError> {
        assert_msgtype!(msgtype);
        Ok(Self { tx_status: body.get_u8() })
    }
}

macro_rules! unit_msg {
    ($(
        $(#[$doc:meta])* struct $name:ident, $ty:literal;
    )*) => {$(
            $(#[$doc])*
            #[derive(Debug)]
            pub struct $name;

            msgtype!($name, $ty);

            impl BackendProtocol for $name {
                fn decode(msgtype: u8, _: Bytes) -> Result<Self,ProtocolError> {
                    if $name::MSGTYPE != msgtype {
                        return Err(ProtocolError::unexpected(Self::MSGTYPE,msgtype))
                    }
                    Ok(Self)
                }
            }
    )*};
}

unit_msg! {
    /// Identifies the message as a Bind-complete indicator.
    struct BindComplete, b'2';

    /// Identifies the message as a Close-complete indicator.
    struct CloseComplete, b'3';

    /// Identifies the message as a response to an empty query string.
    ///
    /// This substitutes for CommandComplete.
    struct EmptyQueryResponse, b'I';

    /// Identifies the message as a no-data indicator.
    struct NoData, b'n';

    /// Identifies the message as a Parse-complete indicator.
    struct ParseComplete, b'1';

    /// Identifies the message as a portal-suspended indicator.
    ///
    /// Note this only appears if an Execute message's row-count limit was reached.
    struct PortalSuspended, b's';
}

// CUSTOM DEBUG

impl std::fmt::Debug for BackendKeyData {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BackendKeyData")
            .field("process_id", &self.process_id)
            .field("secret_key", &"<REDACTED>")
            .finish()
    }
}

impl std::fmt::Debug for ReadyForQuery {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ReadyForQuery")
            .field("tx_status", &match self.tx_status {
                b'I' => "Idle(I)",
                b'T' => "Transaction(T)",
                b'E' => "FailedTx(E)",
                _ => "unknown",
            })
            .finish()
    }
}

impl std::fmt::Debug for RowDescription {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RowDescription")
            .field("body", &"<BINARY>")
            .finish()
    }
}

impl std::fmt::Debug for DataRow {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DataRow")
            .field("body", &"<BINARY>")
            .finish()
    }
}