Refactor connectparams

This commit is contained in:
Steven Fackler
2017-02-15 21:42:27 -08:00
parent 4c91a68dcc
commit c59799e376
7 changed files with 179 additions and 104 deletions

View File

@@ -142,8 +142,8 @@ pub fn cancel_query<T>(params: T,
{
let params = match params.into_connect_params() {
Ok(params) => {
Either::A(stream::connect(params.target.clone(),
params.port.unwrap_or(5432),
Either::A(stream::connect(params.host().clone(),
params.port(),
tls_mode,
handle))
}
@@ -264,8 +264,8 @@ impl Connection {
{
let fut = match params.into_connect_params() {
Ok(params) => {
Either::A(stream::connect(params.target.clone(),
params.port.unwrap_or(5432),
Either::A(stream::connect(params.host().clone(),
params.port(),
tls_mode,
handle)
.map(|s| (s, params)))
@@ -301,9 +301,9 @@ impl Connection {
let result = {
let options = [("client_encoding", "UTF8"), ("timezone", "GMT")];
let options = options.iter().cloned();
let options = options.chain(params.user.as_ref().map(|u| ("user", &*u.user)));
let options = options.chain(params.database.as_ref().map(|d| ("database", &**d)));
let options = options.chain(params.options.iter().map(|e| (&*e.0, &*e.1)));
let options = options.chain(params.user().map(|u| ("user", u.name())));
let options = options.chain(params.database().map(|d| ("database", d)));
let options = options.chain(params.options().iter().map(|e| (&*e.0, &*e.1)));
frontend::startup_message(options, &mut buf)
};
@@ -323,7 +323,7 @@ impl Connection {
let response = match m {
backend::Message::AuthenticationOk => Ok(None),
backend::Message::AuthenticationCleartextPassword => {
match params.user.as_ref().and_then(|u| u.password.as_ref()) {
match params.user().and_then(|u| u.password()) {
Some(pass) => {
let mut buf = vec![];
frontend::password_message(pass, &mut buf)
@@ -337,7 +337,7 @@ impl Connection {
}
}
backend::Message::AuthenticationMd5Password(body) => {
match params.user.as_ref().and_then(|u| u.password.as_ref().map(|p| (&u.user, p))) {
match params.user().and_then(|u| u.password().map(|p| (u.name(), p))) {
Some((user, pass)) => {
let pass = authentication::md5_hash(user.as_bytes(),
pass.as_bytes(),

View File

@@ -1,6 +1,6 @@
use futures::{BoxFuture, Future, IntoFuture, Async, Sink, Stream as FuturesStream};
use futures::future::Either;
use postgres_shared::params::ConnectTarget;
use postgres_shared::params::Host;
use postgres_protocol::message::backend::{self, ParseResult};
use postgres_protocol::message::frontend;
use std::io::{self, Read, Write};
@@ -16,17 +16,17 @@ use tls::TlsStream;
pub type PostgresStream = Framed<Box<TlsStream>, PostgresCodec>;
pub fn connect(host: ConnectTarget,
pub fn connect(host: Host,
port: u16,
tls_mode: TlsMode,
handle: &Handle)
-> BoxFuture<PostgresStream, ConnectError> {
let inner = match host {
ConnectTarget::Tcp(ref host) => {
Host::Tcp(ref host) => {
Either::A(tokio_dns::tcp_connect((&**host, port), handle.remote().clone())
.map(|s| Stream(InnerStream::Tcp(s))))
}
ConnectTarget::Unix(ref host) => {
Host::Unix(ref host) => {
let addr = host.join(format!(".s.PGSQL.{}", port));
Either::B(UnixStream::connect(addr, handle)
.map(|s| Stream(InnerStream::Unix(s)))
@@ -68,8 +68,8 @@ pub fn connect(host: ConnectTarget,
(None, _) => Either::A(Err(ConnectError::Io(io::Error::new(io::ErrorKind::UnexpectedEof, "unexpected EOF"))).into_future()),
_ => {
let host = match host {
ConnectTarget::Tcp(ref host) => host,
ConnectTarget::Unix(_) => unreachable!(),
Host::Tcp(ref host) => host,
Host::Unix(_) => unreachable!(),
};
Either::B(handshaker.handshake(host, s).map_err(ConnectError::Tls))
}

View File

@@ -7,7 +7,7 @@ use tokio_core::reactor::{Core, Interval};
use super::*;
use error::{Error, ConnectError, SqlState};
use params::{ConnectParams, ConnectTarget, UserInfo};
use params::{ConnectParams, Host};
use types::{ToSql, FromSql, Type, IsNull, Kind};
#[test]
@@ -182,16 +182,9 @@ fn unix_socket() {
.and_then(|(s, c)| c.query(&s, &[]).collect())
.then(|r| {
let r = r.unwrap().0;
let params = ConnectParams {
target: ConnectTarget::Unix(PathBuf::from(r[0].get::<String, _>(0))),
port: None,
user: Some(UserInfo {
user: "postgres".to_owned(),
password: None,
}),
database: None,
options: vec![],
};
let params = ConnectParams::builder()
.user("postgres", None)
.build(Host::Unix(PathBuf::from(r[0].get::<String, _>(0))));
Connection::connect(params, TlsMode::None, &handle)
})
.then(|c| c.unwrap().batch_execute(""));