Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use futures::StreamExt;
use tokio::net::TcpListener;

use pgwire::api::auth::noop::NoopStartupHandler;
use pgwire::api::copy::NoopCopyHandler;
use pgwire::api::query::{PlaceholderExtendedQueryHandler, SimpleQueryHandler};
use pgwire::api::results::{DataRowEncoder, FieldFormat, FieldInfo, QueryResponse, Response};
use pgwire::api::{ClientInfo, MakeHandler, StatelessMakeHandler, Type};
Expand Down Expand Up @@ -73,6 +74,7 @@ pub async fn main() {
PlaceholderExtendedQueryHandler,
)));
let authenticator = Arc::new(StatelessMakeHandler::new(Arc::new(NoopStartupHandler)));
let noop_copy_handler = Arc::new(NoopCopyHandler);

let server_addr = "127.0.0.1:5433";
let listener = TcpListener::bind(server_addr).await.unwrap();
Expand All @@ -82,13 +84,16 @@ pub async fn main() {
let authenticator_ref = authenticator.make();
let processor_ref = processor.make();
let placeholder_ref = placeholder.make();
let copy_handler_ref = noop_copy_handler.clone();

tokio::spawn(async move {
process_socket(
incoming_socket.0,
None,
authenticator_ref,
processor_ref,
placeholder_ref,
copy_handler_ref,
)
.await
});
Expand Down
5 changes: 5 additions & 0 deletions examples/duckdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use futures::stream;
use futures::Stream;
use pgwire::api::auth::md5pass::{hash_md5_password, MakeMd5PasswordAuthStartupHandler};
use pgwire::api::auth::{AuthSource, DefaultServerParameterProvider, LoginInfo, Password};
use pgwire::api::copy::NoopCopyHandler;
use pgwire::api::portal::{Format, Portal};
use pgwire::api::query::{ExtendedQueryHandler, SimpleQueryHandler};
use pgwire::api::results::{
Expand Down Expand Up @@ -350,6 +351,7 @@ pub async fn main() {
Arc::new(parameters),
));
let processor = Arc::new(MakeDuckDBBackend::new());
let noop_copy_handler = Arc::new(NoopCopyHandler);

let server_addr = "127.0.0.1:5432";
let listener = TcpListener::bind(server_addr).await.unwrap();
Expand All @@ -358,13 +360,16 @@ pub async fn main() {
let incoming_socket = listener.accept().await.unwrap();
let authenticator_ref = authenticator.make();
let processor_ref = processor.make();
let copy_handler_ref = noop_copy_handler.clone();

tokio::spawn(async move {
process_socket(
incoming_socket.0,
None,
authenticator_ref,
processor_ref.clone(),
processor_ref,
copy_handler_ref,
)
.await
});
Expand Down
5 changes: 5 additions & 0 deletions examples/gluesql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use tokio::net::TcpListener;

use gluesql::prelude::*;
use pgwire::api::auth::noop::NoopStartupHandler;
use pgwire::api::copy::NoopCopyHandler;
use pgwire::api::query::{PlaceholderExtendedQueryHandler, SimpleQueryHandler};
use pgwire::api::results::{DataRowEncoder, FieldFormat, FieldInfo, QueryResponse, Response, Tag};
use pgwire::api::{ClientInfo, MakeHandler, StatelessMakeHandler, Type};
Expand Down Expand Up @@ -170,6 +171,7 @@ pub async fn main() {
PlaceholderExtendedQueryHandler,
)));
let authenticator = Arc::new(StatelessMakeHandler::new(Arc::new(NoopStartupHandler)));
let noop_copy_handler = Arc::new(NoopCopyHandler);

let server_addr = "127.0.0.1:5432";
let listener = TcpListener::bind(server_addr).await.unwrap();
Expand All @@ -179,13 +181,16 @@ pub async fn main() {
let authenticator_ref = authenticator.make();
let processor_ref = processor.make();
let placeholder_ref = placeholder.make();
let copy_handler_ref = noop_copy_handler.clone();

tokio::spawn(async move {
process_socket(
incoming_socket.0,
None,
authenticator_ref,
processor_ref,
placeholder_ref,
copy_handler_ref,
)
.await
});
Expand Down
5 changes: 5 additions & 0 deletions examples/scram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use tokio_rustls::TlsAcceptor;

use pgwire::api::auth::scram::{gen_salted_password, MakeSASLScramAuthStartupHandler};
use pgwire::api::auth::{AuthSource, DefaultServerParameterProvider, LoginInfo, Password};
use pgwire::api::copy::NoopCopyHandler;
use pgwire::api::query::{PlaceholderExtendedQueryHandler, SimpleQueryHandler};
use pgwire::api::results::{Response, Tag};

Expand Down Expand Up @@ -83,6 +84,7 @@ pub async fn main() {
let placeholder = Arc::new(StatelessMakeHandler::new(Arc::new(
PlaceholderExtendedQueryHandler,
)));
let noop_copy_handler = Arc::new(NoopCopyHandler);
let mut authenticator = MakeSASLScramAuthStartupHandler::new(
Arc::new(DummyAuthDB),
Arc::new(DefaultServerParameterProvider::default()),
Expand All @@ -103,13 +105,16 @@ pub async fn main() {
let authenticator_ref = authenticator.make();
let processor_ref = processor.make();
let placeholder_ref = placeholder.make();
let copy_handler_ref = noop_copy_handler.clone();

tokio::spawn(async move {
process_socket(
incoming_socket.0,
Some(tls_acceptor_ref),
authenticator_ref,
processor_ref,
placeholder_ref,
copy_handler_ref,
)
.await
});
Expand Down
4 changes: 4 additions & 0 deletions examples/secure_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use tokio_rustls::rustls::ServerConfig;
use tokio_rustls::TlsAcceptor;

use pgwire::api::auth::noop::NoopStartupHandler;
use pgwire::api::copy::NoopCopyHandler;
use pgwire::api::query::{PlaceholderExtendedQueryHandler, SimpleQueryHandler};
use pgwire::api::results::{DataRowEncoder, FieldFormat, FieldInfo, QueryResponse, Response, Tag};
use pgwire::api::{ClientInfo, MakeHandler, StatelessMakeHandler, Type};
Expand Down Expand Up @@ -84,6 +85,7 @@ pub async fn main() {
PlaceholderExtendedQueryHandler,
)));
let authenticator = Arc::new(StatelessMakeHandler::new(Arc::new(NoopStartupHandler)));
let noop_copy_handler = Arc::new(NoopCopyHandler);

let server_addr = "127.0.0.1:5433";
let tls_acceptor = Arc::new(setup_tls().unwrap());
Expand All @@ -96,13 +98,15 @@ pub async fn main() {
let authenticator_ref = authenticator.make();
let processor_ref = processor.make();
let placeholder_ref = placeholder.make();
let copy_handler_ref = noop_copy_handler.clone();
tokio::spawn(async move {
process_socket(
incoming_socket.0,
Some(tls_acceptor_ref),
authenticator_ref,
processor_ref,
placeholder_ref,
copy_handler_ref,
)
.await
});
Expand Down
4 changes: 4 additions & 0 deletions examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use futures::{stream, Sink, SinkExt, StreamExt};
use tokio::net::TcpListener;

use pgwire::api::auth::noop::NoopStartupHandler;
use pgwire::api::copy::NoopCopyHandler;
use pgwire::api::query::{PlaceholderExtendedQueryHandler, SimpleQueryHandler};
use pgwire::api::results::{DataRowEncoder, FieldFormat, FieldInfo, QueryResponse, Response, Tag};
use pgwire::api::{ClientInfo, MakeHandler, StatelessMakeHandler, Type};
Expand Down Expand Up @@ -76,6 +77,7 @@ pub async fn main() {
PlaceholderExtendedQueryHandler,
)));
let authenticator = Arc::new(StatelessMakeHandler::new(Arc::new(NoopStartupHandler)));
let noop_copy_handler = Arc::new(NoopCopyHandler);

let server_addr = "127.0.0.1:5432";
let listener = TcpListener::bind(server_addr).await.unwrap();
Expand All @@ -85,13 +87,15 @@ pub async fn main() {
let authenticator_ref = authenticator.make();
let processor_ref = processor.make();
let placeholder_ref = placeholder.make();
let copy_handler_ref = noop_copy_handler.clone();
tokio::spawn(async move {
process_socket(
incoming_socket.0,
None,
authenticator_ref,
processor_ref,
placeholder_ref,
copy_handler_ref,
)
.await
});
Expand Down
6 changes: 6 additions & 0 deletions examples/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use std::sync::{Arc, Mutex};
use async_trait::async_trait;
use futures::stream;
use futures::Stream;

use pgwire::api::auth::md5pass::{hash_md5_password, MakeMd5PasswordAuthStartupHandler};
use pgwire::api::auth::{AuthSource, DefaultServerParameterProvider, LoginInfo, Password};
use pgwire::api::copy::NoopCopyHandler;
use pgwire::api::portal::{Format, Portal};
use pgwire::api::query::{ExtendedQueryHandler, SimpleQueryHandler};
use pgwire::api::results::{
Expand Down Expand Up @@ -306,6 +308,7 @@ pub async fn main() {
Arc::new(parameters),
));
let processor = Arc::new(MakeSqliteBackend::new());
let noop_copy_handler = Arc::new(NoopCopyHandler);

let server_addr = "127.0.0.1:5432";
let listener = TcpListener::bind(server_addr).await.unwrap();
Expand All @@ -314,13 +317,16 @@ pub async fn main() {
let incoming_socket = listener.accept().await.unwrap();
let authenticator_ref = authenticator.make();
let processor_ref = processor.make();
let copy_handler_ref = noop_copy_handler.clone();

tokio::spawn(async move {
process_socket(
incoming_socket.0,
None,
authenticator_ref,
processor_ref.clone(),
processor_ref,
copy_handler_ref,
)
.await
});
Expand Down
101 changes: 101 additions & 0 deletions src/api/copy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use async_trait::async_trait;
use futures::sink::{Sink, SinkExt};
use std::fmt::Debug;

use crate::error::{PgWireError, PgWireResult};
use crate::messages::copy::{
CopyBothResponse, CopyData, CopyDone, CopyFail, CopyInResponse, CopyOutResponse,
};
use crate::messages::PgWireBackendMessage;

use super::ClientInfo;

/// handler for copy messages
#[async_trait]
pub trait CopyHandler: Send + Sync {
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>,
{
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>,
{
Ok(())
}

async fn on_copy_fail<C>(&self, _client: &mut C, _fail: CopyFail) -> PgWireResult<()>
where
C: ClientInfo + Sink<PgWireBackendMessage> + Unpin + Send + Sync,
C::Error: Debug,
PgWireError: From<<C as Sink<PgWireBackendMessage>>::Error>,
{
Ok(())
}
}

pub async fn send_copy_in_response<C>(
client: &mut C,
overall_format: i8,
columns: usize,
column_formats: Vec<i16>,
) -> PgWireResult<()>
where
C: ClientInfo + Sink<PgWireBackendMessage> + Unpin + Send + Sync,
C::Error: Debug,
PgWireError: From<<C as Sink<PgWireBackendMessage>>::Error>,
{
let resp = CopyInResponse::new(overall_format, columns as i16, column_formats);
client
.send(PgWireBackendMessage::CopyInResponse(resp))
.await?;
Ok(())
}

pub async fn send_copy_out_response<C>(
client: &mut C,
overall_format: i8,
columns: usize,
column_formats: Vec<i16>,
) -> PgWireResult<()>
where
C: ClientInfo + Sink<PgWireBackendMessage> + Unpin + Send + Sync,
C::Error: Debug,
PgWireError: From<<C as Sink<PgWireBackendMessage>>::Error>,
{
let resp = CopyOutResponse::new(overall_format, columns as i16, column_formats);
client
.send(PgWireBackendMessage::CopyOutResponse(resp))
.await?;
Ok(())
}

pub async fn send_copy_both_response<C>(
client: &mut C,
overall_format: i8,
columns: usize,
column_formats: Vec<i16>,
) -> PgWireResult<()>
where
C: ClientInfo + Sink<PgWireBackendMessage> + Unpin + Send + Sync,
C::Error: Debug,
PgWireError: From<<C as Sink<PgWireBackendMessage>>::Error>,
{
let resp = CopyBothResponse::new(overall_format, columns as i16, column_formats);
client
.send(PgWireBackendMessage::CopyBothResponse(resp))
.await?;
Ok(())
}

#[derive(Clone, Copy, Debug, Default)]
pub struct NoopCopyHandler;

impl CopyHandler for NoopCopyHandler {}
2 changes: 2 additions & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::sync::Arc;
pub use postgres_types::Type;

pub mod auth;
pub mod copy;
pub mod portal;
pub mod query;
pub mod results;
Expand All @@ -22,6 +23,7 @@ pub enum PgWireConnectionState {
AuthenticationInProgress,
ReadyForQuery,
QueryInProgress,
CopyInProgress,
AwaitingSync,
}

Expand Down
Loading