Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
Merge pull request #82 from prisma/yeet-debug-stub
Browse files Browse the repository at this point in the history
Yeet debug-stub dependency
  • Loading branch information
tomhoule authored Feb 13, 2020
2 parents add363a + 0836b1e commit 6f8a78e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ serde-support = ["serde", "chrono/serde"]
url = "2.1"
metrics = "0.12"
percent-encoding = "2"
debug_stub_derive = "0.3"
lazy_static = "1.4"
num_cpus = "1.12"
rust_decimal = "=1.1.0"
Expand Down
59 changes: 36 additions & 23 deletions src/connector/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,27 @@ use url::Url;

pub(crate) const DEFAULT_SCHEMA: &str = "public";

#[derive(Clone)]
struct Hidden<T>(T);

impl<T> std::fmt::Debug for Hidden<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "<HIDDEN>")
}
}

struct PostgresClient(Mutex<Client>);

impl std::fmt::Debug for PostgresClient {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "PostgresClient")
}
}

/// A connector interface for the PostgreSQL database.
#[derive(DebugStub)]
#[derive(Debug)]
pub struct PostgreSql {
#[debug_stub = "postgres::Client"]
client: Mutex<Client>,
client: PostgresClient,
socket_timeout: Option<Duration>,
}

Expand All @@ -37,42 +53,39 @@ pub enum SslAcceptMode {
AcceptInvalidCerts,
}

#[derive(DebugStub, Clone)]
#[derive(Debug, Clone)]
pub struct SslParams {
certificate_file: Option<String>,
identity_file: Option<String>,
#[debug_stub = "<HIDDEN>"]
identity_password: Option<String>,
identity_password: Hidden<Option<String>>,
ssl_accept_mode: SslAcceptMode,
}

#[derive(DebugStub)]
#[derive(Debug)]
struct SslAuth {
#[debug_stub = "<HIDDEN>"]
certificate: Option<Certificate>,
#[debug_stub = "<HIDDEN>"]
identity: Option<Identity>,
certificate: Hidden<Option<Certificate>>,
identity: Hidden<Option<Identity>>,
ssl_accept_mode: SslAcceptMode,
}

impl Default for SslAuth {
fn default() -> Self {
Self {
certificate: None,
identity: None,
certificate: Hidden(None),
identity: Hidden(None),
ssl_accept_mode: SslAcceptMode::AcceptInvalidCerts,
}
}
}

impl SslAuth {
fn certificate(&mut self, certificate: Certificate) -> &mut Self {
self.certificate = Some(certificate);
self.certificate = Hidden(Some(certificate));
self
}

fn identity(&mut self, identity: Identity) -> &mut Self {
self.identity = Some(identity);
self.identity = Hidden(Some(identity));
self
}

Expand All @@ -94,7 +107,7 @@ impl SslParams {

if let Some(ref identity_file) = self.identity_file {
let db = fs::read(identity_file)?;
let password = self.identity_password.as_ref().map(|s| s.as_str()).unwrap_or("");
let password = self.identity_password.0.as_ref().map(|s| s.as_str()).unwrap_or("");
let identity = Identity::from_pkcs12(&db, &password)?;

auth.identity(identity);
Expand Down Expand Up @@ -283,7 +296,7 @@ impl PostgresUrl {
certificate_file,
identity_file,
ssl_accept_mode,
identity_password,
identity_password: Hidden(identity_password),
},
connection_limit,
schema,
Expand Down Expand Up @@ -340,13 +353,13 @@ impl PostgreSql {
let ssl_params = url.ssl_params();
let auth = ssl_params.to_owned().into_auth().await?;

if let Some(certificate) = auth.certificate {
if let Some(certificate) = auth.certificate.0 {
tls_builder.add_root_certificate(certificate);
}

tls_builder.danger_accept_invalid_certs(auth.ssl_accept_mode == SslAcceptMode::AcceptInvalidCerts);

if let Some(identity) = auth.identity {
if let Some(identity) = auth.identity.0 {
tls_builder.identity(identity);
}
}
Expand All @@ -361,7 +374,7 @@ impl PostgreSql {
client.simple_query(path.as_str()).await?;

Ok(Self {
client: Mutex::new(client),
client: PostgresClient(Mutex::new(client)),
socket_timeout: url.query_params.socket_timeout,
})
}
Expand Down Expand Up @@ -400,7 +413,7 @@ impl Queryable for PostgreSql {

fn query_raw<'a>(&'a self, sql: &'a str, params: &'a [ParameterizedValue<'a>]) -> DBIO<'a, ResultSet> {
metrics::query("postgres.query_raw", sql, params, move || async move {
let client = self.client.lock().await;
let client = self.client.0.lock().await;
let stmt = self.timeout(client.prepare(sql)).await?;

let rows = self
Expand All @@ -418,7 +431,7 @@ impl Queryable for PostgreSql {

fn execute_raw<'a>(&'a self, sql: &'a str, params: &'a [ParameterizedValue<'a>]) -> DBIO<'a, u64> {
metrics::query("postgres.execute_raw", sql, params, move || async move {
let client = self.client.lock().await;
let client = self.client.0.lock().await;
let stmt = self.timeout(client.prepare(sql)).await?;

let changes = self
Expand All @@ -431,7 +444,7 @@ impl Queryable for PostgreSql {

fn raw_cmd<'a>(&'a self, cmd: &'a str) -> DBIO<'a, ()> {
metrics::query("postgres.raw_cmd", cmd, &[], move || async move {
let client = self.client.lock().await;
let client = self.client.0.lock().await;
self.timeout(client.simple_query(cmd)).await?;

Ok(())
Expand Down
4 changes: 0 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ extern crate log;
#[cfg(any(feature = "sqlite", feature = "mysql", feature = "postgresql"))]
extern crate metrics;

#[macro_use]
#[cfg(all(feature = "array", feature = "postgresql"))]
extern crate debug_stub_derive;

pub mod ast;
#[cfg(any(feature = "sqlite", feature = "mysql", feature = "postgresql"))]
pub mod connector;
Expand Down

0 comments on commit 6f8a78e

Please sign in to comment.