sqlx_postgres/message/
copy.rs

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
use crate::error::Result;
use crate::io::BufMutExt;
use crate::message::{
    BackendMessage, BackendMessageFormat, FrontendMessage, FrontendMessageFormat,
};
use sqlx_core::bytes::{Buf, Bytes};
use sqlx_core::Error;
use std::num::Saturating;
use std::ops::Deref;

/// The same structure is sent for both `CopyInResponse` and `CopyOutResponse`
pub struct CopyResponseData {
    pub format: i8,
    pub num_columns: i16,
    pub format_codes: Vec<i16>,
}

pub struct CopyInResponse(pub CopyResponseData);

#[allow(dead_code)]
pub struct CopyOutResponse(pub CopyResponseData);

pub struct CopyData<B>(pub B);

pub struct CopyFail {
    pub message: String,
}

pub struct CopyDone;

impl CopyResponseData {
    #[inline]
    fn decode(mut buf: Bytes) -> Result<Self> {
        let format = buf.get_i8();
        let num_columns = buf.get_i16();

        let format_codes = (0..num_columns).map(|_| buf.get_i16()).collect();

        Ok(CopyResponseData {
            format,
            num_columns,
            format_codes,
        })
    }
}

impl BackendMessage for CopyInResponse {
    const FORMAT: BackendMessageFormat = BackendMessageFormat::CopyInResponse;

    #[inline(always)]
    fn decode_body(buf: Bytes) -> std::result::Result<Self, Error> {
        Ok(Self(CopyResponseData::decode(buf)?))
    }
}

impl BackendMessage for CopyOutResponse {
    const FORMAT: BackendMessageFormat = BackendMessageFormat::CopyOutResponse;

    #[inline(always)]
    fn decode_body(buf: Bytes) -> std::result::Result<Self, Error> {
        Ok(Self(CopyResponseData::decode(buf)?))
    }
}

impl BackendMessage for CopyData<Bytes> {
    const FORMAT: BackendMessageFormat = BackendMessageFormat::CopyData;

    #[inline(always)]
    fn decode_body(buf: Bytes) -> std::result::Result<Self, Error> {
        Ok(Self(buf))
    }
}

impl<B: Deref<Target = [u8]>> FrontendMessage for CopyData<B> {
    const FORMAT: FrontendMessageFormat = FrontendMessageFormat::CopyData;

    #[inline(always)]
    fn body_size_hint(&self) -> Saturating<usize> {
        Saturating(self.0.len())
    }

    #[inline(always)]
    fn encode_body(&self, buf: &mut Vec<u8>) -> Result<(), Error> {
        buf.extend_from_slice(&self.0);
        Ok(())
    }
}

impl FrontendMessage for CopyFail {
    const FORMAT: FrontendMessageFormat = FrontendMessageFormat::CopyFail;

    #[inline(always)]
    fn body_size_hint(&self) -> Saturating<usize> {
        Saturating(self.message.len())
    }

    #[inline(always)]
    fn encode_body(&self, buf: &mut Vec<u8>) -> std::result::Result<(), Error> {
        buf.put_str_nul(&self.message);
        Ok(())
    }
}

impl CopyFail {
    #[inline(always)]
    pub fn new(msg: impl Into<String>) -> CopyFail {
        CopyFail {
            message: msg.into(),
        }
    }
}

impl FrontendMessage for CopyDone {
    const FORMAT: FrontendMessageFormat = FrontendMessageFormat::CopyDone;
    #[inline(always)]
    fn body_size_hint(&self) -> Saturating<usize> {
        Saturating(0)
    }

    #[inline(always)]
    fn encode_body(&self, _buf: &mut Vec<u8>) -> std::result::Result<(), Error> {
        Ok(())
    }
}

impl BackendMessage for CopyDone {
    const FORMAT: BackendMessageFormat = BackendMessageFormat::CopyDone;

    #[inline(always)]
    fn decode_body(bytes: Bytes) -> std::result::Result<Self, Error> {
        if !bytes.is_empty() {
            // Not fatal but may indicate a protocol change
            tracing::debug!(
                "Postgres backend returned non-empty message for CopyDone: \"{}\"",
                bytes.escape_ascii()
            )
        }

        Ok(CopyDone)
    }
}