rust-postgres/tokio-postgres/src/lib.rs

134 lines
2.9 KiB
Rust
Raw Normal View History

2017-04-23 22:39:07 +00:00
extern crate bytes;
2016-12-21 03:50:44 +00:00
extern crate fallible_iterator;
2018-06-17 04:29:27 +00:00
extern crate futures_cpupool;
2016-12-20 23:42:28 +00:00
extern crate postgres_protocol;
2018-01-10 05:15:35 +00:00
extern crate postgres_shared;
2018-06-17 04:29:27 +00:00
extern crate tokio_codec;
2017-04-23 22:39:07 +00:00
extern crate tokio_io;
2018-06-17 04:29:27 +00:00
extern crate tokio_tcp;
extern crate tokio_timer;
#[macro_use]
extern crate futures;
2018-06-17 04:29:27 +00:00
#[macro_use]
extern crate lazy_static;
#[macro_use]
2018-06-19 02:34:25 +00:00
extern crate log;
#[macro_use]
2018-06-17 04:29:27 +00:00
extern crate state_machine_future;
#[cfg(unix)]
2016-12-20 23:42:28 +00:00
extern crate tokio_uds;
2018-06-17 04:29:27 +00:00
use futures::{Async, Future, Poll};
2016-12-20 23:42:28 +00:00
use std::io;
2018-06-19 02:34:25 +00:00
use std::sync::atomic::{AtomicUsize, Ordering};
2016-12-20 23:42:28 +00:00
2017-07-20 04:22:27 +00:00
#[doc(inline)]
2018-06-18 12:18:04 +00:00
pub use postgres_shared::stmt::Column;
#[doc(inline)]
pub use postgres_shared::{error, params, types};
2018-04-15 21:38:01 +00:00
#[doc(inline)]
2018-06-17 04:29:27 +00:00
pub use postgres_shared::{CancelData, Notification};
2016-12-25 17:51:12 +00:00
2018-06-17 04:29:27 +00:00
use error::Error;
use params::ConnectParams;
2018-06-19 02:34:25 +00:00
use types::Type;
2016-12-20 23:42:28 +00:00
2018-06-17 04:29:27 +00:00
mod proto;
2017-07-20 04:22:27 +00:00
2018-06-19 02:34:25 +00:00
static NEXT_STATEMENT_ID: AtomicUsize = AtomicUsize::new(0);
2018-06-17 04:29:27 +00:00
fn bad_response() -> Error {
Error::from(io::Error::new(
io::ErrorKind::InvalidInput,
"the server returned an unexpected response",
))
2016-12-20 23:42:28 +00:00
}
2018-06-17 04:29:27 +00:00
fn disconnected() -> Error {
Error::from(io::Error::new(
io::ErrorKind::UnexpectedEof,
"server disconnected",
))
2016-12-20 23:42:28 +00:00
}
2018-06-19 02:34:25 +00:00
pub fn connect(params: ConnectParams) -> Handshake {
Handshake(proto::HandshakeFuture::new(params))
}
2018-06-18 12:18:04 +00:00
pub struct Client(proto::Client);
2016-12-20 23:42:28 +00:00
2018-06-17 04:29:27 +00:00
impl Client {
2018-06-19 02:34:25 +00:00
pub fn prepare(&mut self, query: &str) -> Prepare {
self.prepare_typed(query, &[])
}
pub fn prepare_typed(&mut self, query: &str, param_types: &[Type]) -> Prepare {
let name = format!("s{}", NEXT_STATEMENT_ID.fetch_add(1, Ordering::SeqCst));
Prepare(self.0.prepare(name, query, param_types))
}
2016-12-20 23:42:28 +00:00
}
2018-06-19 23:54:29 +00:00
#[must_use = "futures do nothing unless polled"]
2018-06-17 04:29:27 +00:00
pub struct Connection(proto::Connection);
2016-12-21 03:50:44 +00:00
2016-12-20 23:42:28 +00:00
impl Connection {
pub fn cancel_data(&self) -> CancelData {
2018-06-17 04:29:27 +00:00
self.0.cancel_data()
2016-12-20 23:42:28 +00:00
}
2016-12-26 21:21:20 +00:00
2018-06-17 04:29:27 +00:00
pub fn parameter(&self, name: &str) -> Option<&str> {
self.0.parameter(name)
}
2016-12-20 23:42:28 +00:00
}
2018-06-17 04:29:27 +00:00
impl Future for Connection {
type Item = ();
type Error = Error;
2018-06-17 04:29:27 +00:00
fn poll(&mut self) -> Poll<(), Error> {
self.0.poll()
2016-12-21 03:50:44 +00:00
}
}
2018-06-19 23:54:29 +00:00
#[must_use = "futures do nothing unless polled"]
2018-06-17 04:29:27 +00:00
pub struct Handshake(proto::HandshakeFuture);
2017-09-30 21:56:15 +00:00
2018-06-17 04:29:27 +00:00
impl Future for Handshake {
type Item = (Client, Connection);
type Error = Error;
2017-09-30 21:56:15 +00:00
2018-06-17 04:29:27 +00:00
fn poll(&mut self) -> Poll<(Client, Connection), Error> {
2018-06-18 12:18:04 +00:00
let (client, connection) = try_ready!(self.0.poll());
2017-09-30 21:56:15 +00:00
2018-06-18 12:18:04 +00:00
Ok(Async::Ready((Client(client), Connection(connection))))
2017-09-30 21:56:15 +00:00
}
}
2018-06-19 02:34:25 +00:00
2018-06-19 23:54:29 +00:00
#[must_use = "futures do nothing unless polled"]
2018-06-19 02:34:25 +00:00
pub struct Prepare(proto::PrepareFuture);
impl Future for Prepare {
type Item = Statement;
type Error = Error;
fn poll(&mut self) -> Poll<Statement, Error> {
let statement = try_ready!(self.0.poll());
Ok(Async::Ready(Statement(statement)))
}
}
pub struct Statement(proto::Statement);
impl Statement {
pub fn params(&self) -> &[Type] {
self.0.params()
}
pub fn columns(&self) -> &[Column] {
self.0.columns()
}
}