-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathcopy.rs
151 lines (127 loc) · 4.78 KB
/
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
142
143
144
145
146
147
148
149
150
151
use std::fmt::Debug;
use std::sync::Arc;
use async_trait::async_trait;
use futures::{Sink, SinkExt};
use tokio::net::TcpListener;
use pgwire::api::auth::noop::NoopStartupHandler;
use pgwire::api::copy::CopyHandler;
use pgwire::api::query::{PlaceholderExtendedQueryHandler, SimpleQueryHandler};
use pgwire::api::results::{CopyResponse, Response};
use pgwire::api::{ClientInfo, NoopErrorHandler, PgWireConnectionState, PgWireServerHandlers};
use pgwire::error::ErrorInfo;
use pgwire::error::{PgWireError, PgWireResult};
use pgwire::messages::copy::{CopyData, CopyDone, CopyFail};
use pgwire::messages::response::NoticeResponse;
use pgwire::messages::PgWireBackendMessage;
use pgwire::tokio::process_socket;
pub struct DummyProcessor;
impl NoopStartupHandler for DummyProcessor {}
#[async_trait]
impl SimpleQueryHandler for DummyProcessor {
async fn do_query<'a, C>(
&self,
client: &mut C,
query: &'a str,
) -> PgWireResult<Vec<Response<'a>>>
where
C: ClientInfo + Sink<PgWireBackendMessage> + Unpin + Send + Sync,
C::Error: Debug,
PgWireError: From<<C as Sink<PgWireBackendMessage>>::Error>,
{
client
.send(PgWireBackendMessage::NoticeResponse(NoticeResponse::from(
ErrorInfo::new(
"NOTICE".to_owned(),
"01000".to_owned(),
format!("Query received {}", query),
),
)))
.await?;
Ok(vec![Response::CopyIn(CopyResponse::new(0, 1, vec![0]))])
}
}
#[async_trait]
impl CopyHandler for DummyProcessor {
async fn on_copy_data<C>(&self, client: &mut C, copy_data: CopyData) -> PgWireResult<()>
where
C: ClientInfo + Sink<PgWireBackendMessage> + Unpin + Send + Sync,
C::Error: Debug,
PgWireError: From<<C as Sink<PgWireBackendMessage>>::Error>,
{
use PgWireConnectionState::*;
// This is set by the `on_query` implementations while handling a
// `CopyIn`/`CopyOut`/`CopyBoth` response.
assert!(matches!(client.state(), CopyInProgress(_)));
println!("receiving data: {:?}", copy_data);
Ok(())
}
async fn on_copy_done<C>(&self, client: &mut C, _done: CopyDone) -> PgWireResult<()>
where
C: ClientInfo + Sink<PgWireBackendMessage> + Unpin + Send + Sync,
C::Error: Debug,
PgWireError: From<<C as Sink<PgWireBackendMessage>>::Error>,
{
use PgWireConnectionState::*;
// This is set by the `on_query` implementations while handling a
// `CopyIn`/`CopyOut`/`CopyBoth` response.
assert!(matches!(client.state(), CopyInProgress(_)));
println!("copy done");
Ok(())
}
async fn on_copy_fail<C>(&self, client: &mut C, fail: CopyFail) -> PgWireError
where
C: ClientInfo + Sink<PgWireBackendMessage> + Unpin + Send + Sync,
C::Error: Debug,
PgWireError: From<<C as Sink<PgWireBackendMessage>>::Error>,
{
use PgWireConnectionState::*;
// This is set by the `on_query` implementations while handling a
// `CopyIn`/`CopyOut`/`CopyBoth` response.
assert!(matches!(client.state(), CopyInProgress(_)));
println!("copy failed: {:?}", fail);
PgWireError::UserError(Box::new(ErrorInfo::new(
"ERROR".to_owned(),
"XX000".to_owned(),
format!("COPY IN mode terminated by the user: {}", fail.message),
)))
}
}
struct DummyProcessorFactory {
handler: Arc<DummyProcessor>,
}
impl PgWireServerHandlers for DummyProcessorFactory {
type StartupHandler = DummyProcessor;
type SimpleQueryHandler = DummyProcessor;
type ExtendedQueryHandler = PlaceholderExtendedQueryHandler;
type CopyHandler = DummyProcessor;
type ErrorHandler = NoopErrorHandler;
fn simple_query_handler(&self) -> Arc<Self::SimpleQueryHandler> {
self.handler.clone()
}
fn extended_query_handler(&self) -> Arc<Self::ExtendedQueryHandler> {
Arc::new(PlaceholderExtendedQueryHandler)
}
fn startup_handler(&self) -> Arc<Self::StartupHandler> {
self.handler.clone()
}
fn copy_handler(&self) -> Arc<Self::CopyHandler> {
self.handler.clone()
}
fn error_handler(&self) -> Arc<Self::ErrorHandler> {
Arc::new(NoopErrorHandler)
}
}
#[tokio::main]
pub async fn main() {
let factory = Arc::new(DummyProcessorFactory {
handler: Arc::new(DummyProcessor),
});
let server_addr = "127.0.0.1:5432";
let listener = TcpListener::bind(server_addr).await.unwrap();
println!("Listening to {}", server_addr);
loop {
let incoming_socket = listener.accept().await.unwrap();
let factory_ref = factory.clone();
tokio::spawn(async move { process_socket(incoming_socket.0, None, factory_ref).await });
}
}