use std::{future::Future, pin::Pin, sync::Arc};
pub mod batch;
mod column;
pub mod connection;
mod error;
pub mod params;
pub mod protocol;
mod rows;
mod session;
mod statement;
pub mod transaction;
pub mod value;
pub use batch::{BatchResult, BatchStatement, IntoBatchStatement};
pub use column::Column;
pub use connection::Connection;
pub use error::{BoxError, Error, Result};
pub use params::{params_from_iter, IntoParams, IntoValue, Params};
pub use protocol::ENCRYPTION_KEY_HEADER;
pub use rows::{Row, Rows};
pub use statement::Statement;
pub use transaction::{Transaction, TransactionBehavior};
pub use value::{FromValue, Value};
pub type AuthTokenFut = Pin<Box<dyn Future<Output = Result<String>> + Send + 'static>>;
pub type AuthTokenFn = Arc<dyn Fn() -> AuthTokenFut + Send + Sync + 'static>;
pub struct Builder {
url: String,
auth_token: Option<AuthTokenFn>,
remote_encryption_key: Option<String>,
}
impl Builder {
pub fn new_remote(url: impl Into<String>) -> Self {
Self {
url: url.into(),
auth_token: None,
remote_encryption_key: None,
}
}
pub fn with_auth_token(mut self, token: impl Into<String>) -> Self {
let token = token.into();
self.auth_token = Some(Arc::new(move || {
let token = token.clone();
Box::pin(async move { Ok(token) })
}));
self
}
pub fn with_auth_token_fn<F, Fut>(mut self, f: F) -> Self
where
F: Fn() -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<String>> + Send + 'static,
{
self.auth_token = Some(Arc::new(move || Box::pin(f())));
self
}
pub fn with_remote_encryption_key(mut self, base64_key: impl Into<String>) -> Self {
self.remote_encryption_key = Some(base64_key.into());
self
}
pub async fn build(self) -> Result<Database> {
Ok(Database {
url: self.url,
auth_token: self.auth_token,
remote_encryption_key: self.remote_encryption_key,
})
}
}
#[derive(Clone)]
pub struct Database {
url: String,
auth_token: Option<AuthTokenFn>,
remote_encryption_key: Option<String>,
}
impl std::fmt::Debug for Database {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Database").field("url", &self.url).finish()
}
}
impl Database {
pub fn connect(&self) -> Result<Connection> {
Ok(Connection::new(
&self.url,
self.auth_token.clone(),
self.remote_encryption_key.clone(),
))
}
}