-
-
Notifications
You must be signed in to change notification settings - Fork 467
/
Copy pathcopy_both.rs
56 lines (47 loc) · 1.61 KB
/
copy_both.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
use crate::client::{InnerClient, Responses};
use crate::codec::FrontendMessage;
use crate::connection::RequestMessages;
use crate::{simple_query, Error};
use bytes::Bytes;
use futures_util::{ready, Stream};
use log::debug;
use pin_project_lite::pin_project;
use postgres_protocol::message::backend::Message;
use std::marker::PhantomPinned;
use std::pin::Pin;
use std::task::{Context, Poll};
pub async fn copy_both(client: &InnerClient, query: &str) -> Result<CopyBothStream, Error> {
debug!("executing copy out statement {query}");
let buf = simple_query::encode(client, query)?;
let responses = start(client, buf).await?;
Ok(CopyBothStream {
responses,
_p: PhantomPinned,
})
}
async fn start(client: &InnerClient, buf: Bytes) -> Result<Responses, Error> {
let mut responses = client.send(RequestMessages::Single(FrontendMessage::Raw(buf)))?;
match responses.next().await? {
Message::CopyBothResponse(_) => {}
_ => return Err(Error::unexpected_message()),
}
Ok(responses)
}
pin_project! {
pub struct CopyBothStream {
responses: Responses,
#[pin]
_p: PhantomPinned,
}
}
impl Stream for CopyBothStream {
type Item = Result<Bytes, Error>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.project();
match ready!(this.responses.poll_next(cx)?) {
Message::CopyData(body) => Poll::Ready(Some(Ok(body.into_bytes()))),
Message::CopyDone => Poll::Ready(None),
_ => Poll::Ready(Some(Err(Error::unexpected_message()))),
}
}
}