Initial sync crate sketch

This commit is contained in:
Steven Fackler
2018-12-21 13:34:09 -08:00
parent 7d20064bd0
commit 759256010d
6 changed files with 165 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
[workspace]
members = [
"codegen",
"postgres",
"postgres-protocol",
"tokio-postgres",
"tokio-postgres-native-tls",

18
postgres/Cargo.toml Normal file
View File

@@ -0,0 +1,18 @@
[package]
name = "postgres"
version = "0.1.0"
authors = ["Steven Fackler <sfackler@gmail.com>"]
edition = "2018"
[features]
default = ["runtime"]
runtime = ["tokio-postgres/runtime", "tokio", "lazy_static", "log"]
[dependencies]
futures = "0.1"
tokio-postgres = { version = "0.3", path = "../tokio-postgres", default-features = false }
tokio = { version = "0.1", optional = true }
lazy_static = { version = "1.0", optional = true }
log = { version = "0.4", optional = true }

50
postgres/src/builder.rs Normal file
View File

@@ -0,0 +1,50 @@
use futures::sync::oneshot;
use futures::Future;
use log::error;
use std::str::FromStr;
use tokio_postgres::{Error, MakeTlsMode, Socket, TlsMode};
use crate::{Client, RUNTIME};
pub struct Builder(tokio_postgres::Builder);
impl Default for Builder {
fn default() -> Builder {
Builder(tokio_postgres::Builder::default())
}
}
impl Builder {
pub fn new() -> Builder {
Builder(tokio_postgres::Builder::new())
}
pub fn param(&mut self, key: &str, value: &str) -> &mut Builder {
self.0.param(key, value);
self
}
pub fn connect<T>(&self, tls_mode: T) -> Result<Client, Error>
where
T: MakeTlsMode<Socket> + 'static + Send,
T::TlsMode: Send,
T::Stream: Send,
T::Future: Send,
<T::TlsMode as TlsMode<Socket>>::Future: Send,
{
let connect = self.0.connect(tls_mode);
let (client, connection) = oneshot::spawn(connect, &RUNTIME.executor()).wait()?;
let connection = connection.map_err(|e| error!("postgres connection error: {}", e));
RUNTIME.executor().spawn(connection);
Ok(Client::from(client))
}
}
impl FromStr for Builder {
type Err = Error;
fn from_str(s: &str) -> Result<Builder, Error> {
s.parse().map(Builder)
}
}

60
postgres/src/client.rs Normal file
View File

@@ -0,0 +1,60 @@
use futures::{Future, Stream};
use tokio_postgres::types::{ToSql, Type};
use tokio_postgres::{Error, Row};
#[cfg(feature = "runtime")]
use tokio_postgres::{MakeTlsMode, Socket, TlsMode};
#[cfg(feature = "runtime")]
use crate::Builder;
use crate::Statement;
pub struct Client(tokio_postgres::Client);
impl Client {
#[cfg(feature = "runtime")]
pub fn connect<T>(params: &str, tls_mode: T) -> Result<Client, Error>
where
T: MakeTlsMode<Socket> + 'static + Send,
T::TlsMode: Send,
T::Stream: Send,
T::Future: Send,
<T::TlsMode as TlsMode<Socket>>::Future: Send,
{
params.parse::<Builder>()?.connect(tls_mode)
}
#[cfg(feature = "runtime")]
pub fn builder() -> Builder {
Builder::new()
}
pub fn prepare(&mut self, query: &str) -> Result<Statement, Error> {
self.0.prepare(query).wait().map(Statement)
}
pub fn prepare_typed(&mut self, query: &str, types: &[Type]) -> Result<Statement, Error> {
self.0.prepare_typed(query, types).wait().map(Statement)
}
pub fn execute(&mut self, statement: &Statement, params: &[&dyn ToSql]) -> Result<u64, Error> {
self.0.execute(&statement.0, params).wait()
}
pub fn query(
&mut self,
statement: &Statement,
params: &[&dyn ToSql],
) -> Result<Vec<Row>, Error> {
self.0.query(&statement.0, params).collect().wait()
}
pub fn batch_execute(&mut self, query: &str) -> Result<(), Error> {
self.0.batch_execute(query).wait()
}
}
impl From<tokio_postgres::Client> for Client {
fn from(c: tokio_postgres::Client) -> Client {
Client(c)
}
}

22
postgres/src/lib.rs Normal file
View File

@@ -0,0 +1,22 @@
#[cfg(feature = "runtime")]
use lazy_static::lazy_static;
#[cfg(feature = "runtime")]
use tokio::runtime::{self, Runtime};
#[cfg(feature = "runtime")]
mod builder;
mod client;
mod statement;
#[cfg(feature = "runtime")]
pub use crate::builder::*;
pub use crate::client::*;
pub use crate::statement::*;
#[cfg(feature = "runtime")]
lazy_static! {
static ref RUNTIME: Runtime = runtime::Builder::new()
.name_prefix("postgres-")
.build()
.unwrap();
}

14
postgres/src/statement.rs Normal file
View File

@@ -0,0 +1,14 @@
use tokio_postgres::types::Type;
use tokio_postgres::Column;
pub struct Statement(pub(crate) tokio_postgres::Statement);
impl Statement {
pub fn params(&self) -> &[Type] {
self.0.params()
}
pub fn columns(&self) -> &[Column] {
self.0.columns()
}
}