#![cfg(feature = "postgres-tokio-postgres")]
use std::borrow::Cow;
use std::fmt;
use std::str::FromStr;
use bytes::BytesMut;
use deadpool_postgres::{Manager, ManagerConfig, Object, Pool, RecyclingMethod, Runtime};
use ruprizzle_core::ir::Provider;
use ruprizzle_dialect::dialect_for;
use tokio_postgres::NoTls;
use tokio_postgres::types::{IsNull, ToSql, Type};
pub use tokio_postgres::Row;
use crate::types::chrono::{DateTime, NaiveDate, NaiveTime, Utc};
use crate::types::{Decimal, Uuid};
macro_rules! pg_tokio_array_encode {
($values:expr, $ty:expr, $out:expr, $check:pat, $variant:pat, $rust_ty:ty, $convert:expr) => {
if $values
.iter()
.find(|v| !v.is_null())
.map_or(false, |v| matches!(v, $check))
{
let vec: Vec<Option<$rust_ty>> = $values
.iter()
.map(|val| match val {
Value::Null => Ok(None),
$variant => Ok(Some($convert)),
other => Err(format!(
"array contains mixed or unsupported element: expected {}, found {:?}",
std::any::type_name::<$rust_ty>(),
other
)
.into()),
})
.collect::<Result<Vec<Option<$rust_ty>>, Box<dyn std::error::Error + Sync + Send>>>(
)?;
return <Vec<Option<$rust_ty>> as ToSql>::to_sql_checked(&vec, $ty, $out);
}
};
}
use crate::BoxFuture;
use crate::Error;
use crate::executor::{BoxRowStream, Executor, RawRow, RowBatch};
use crate::pool::PoolConfig;
use crate::value::Value;
use futures_util::StreamExt as _;
#[derive(Clone)]
pub struct TokioPostgresPool {
inner: Pool,
}
impl fmt::Debug for TokioPostgresPool {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TokioPostgresPool")
.field("status", &self.inner.status())
.finish_non_exhaustive()
}
}
impl TokioPostgresPool {
pub async fn connect(url: &str, config: &PoolConfig) -> Result<Self, Error> {
let (uses_native, stripped) = strip_driver_param(url);
if !uses_native {
return Err(Error::Message(
"expected `driver=tokio-postgres` in URL".into(),
));
}
let pg_config =
tokio_postgres::Config::from_str(&stripped).map_err(|e| Error::ConnectionFailure {
reason: e.to_string(),
})?;
tracing::info!(
target: "ruprizzle::connection",
event = "connect",
backend = "tokio-postgres",
"tokio-postgres pool opened"
);
let mgr_config = ManagerConfig {
recycling_method: if config.reset_on_recycle {
RecyclingMethod::Clean
} else if config.test_before_acquire {
RecyclingMethod::Verified
} else {
RecyclingMethod::Fast
},
};
let mgr = Manager::from_config(pg_config, NoTls, mgr_config);
let pool = Pool::builder(mgr)
.max_size(config.max_connections as usize)
.runtime(Runtime::Tokio1)
.wait_timeout(Some(config.acquire_timeout))
.create_timeout(Some(config.acquire_timeout))
.recycle_timeout(Some(config.acquire_timeout))
.build()
.map_err(|e| Error::ConnectionFailure {
reason: e.to_string(),
})?;
for _ in 0..config.min_connections {
let _ = pool.get().await.map_err(tokio_postgres_pool_error)?;
}
Ok(Self { inner: pool })
}
#[must_use]
pub fn size(&self) -> u32 {
self.inner.status().size as u32
}
#[must_use]
pub fn num_idle(&self) -> usize {
self.inner.status().available
}
#[must_use]
pub fn num_waiters(&self) -> usize {
self.inner.status().waiting
}
pub(crate) async fn fetch_all(&self, sql: &str, binds: &[Value]) -> Result<RowBatch, Error> {
let client = self.inner.get().await.map_err(tokio_postgres_pool_error)?;
let params = bind_params(binds)?;
let rows = client.query(sql, ¶ms).await.map_err(Error::from)?;
Ok(RowBatch::PostgresNative(rows))
}
pub(crate) async fn execute(&self, sql: &str, binds: &[Value]) -> Result<u64, Error> {
let client = self.inner.get().await.map_err(tokio_postgres_pool_error)?;
let params = bind_params(binds)?;
client.execute(sql, ¶ms).await.map_err(Error::from)
}
pub(crate) async fn begin(&self) -> Result<TokioPostgresTransaction, Error> {
let client = self.inner.get().await.map_err(tokio_postgres_pool_error)?;
client.execute("BEGIN", &[]).await.map_err(Error::from)?;
Ok(TokioPostgresTransaction {
client: Some(client),
runtime: tokio::runtime::Handle::try_current().ok(),
})
}
}
impl Executor for TokioPostgresPool {
fn dialect(&self) -> &dyn ruprizzle_dialect::DbDialect {
dialect_for(Provider::Postgres)
}
fn fetch_all_raw(
&self,
sql: Cow<'static, str>,
binds: Vec<Value>,
) -> BoxFuture<'_, Result<RowBatch, Error>> {
let pool = self.clone();
Box::pin(async move { pool.fetch_all(sql.as_ref(), &binds).await })
}
fn execute_raw(
&self,
sql: Cow<'static, str>,
binds: Vec<Value>,
) -> BoxFuture<'_, Result<u64, Error>> {
let pool = self.clone();
Box::pin(async move { pool.execute(sql.as_ref(), &binds).await })
}
fn stream_raw(&self, sql: Cow<'static, str>, binds: Vec<Value>) -> BoxRowStream<'_> {
Box::pin(crate::executor::DeferredRowStream::new(Box::pin(
async move { self.fetch_all_raw(sql, binds).await },
)))
}
fn stream_unbuffered_raw(&self, sql: Cow<'static, str>, binds: Vec<Value>) -> BoxRowStream<'_> {
let sql = sql.into_owned();
let pool = self.clone();
Box::pin(futures_util::stream::unfold(
(
sql,
binds,
None::<std::pin::Pin<Box<tokio_postgres::RowStream>>>,
),
move |(sql, binds, mut stream)| {
let pool = pool.clone();
async move {
if stream.is_none() {
let client = match pool.inner.get().await {
Ok(c) => c,
Err(e) => {
return Some((
Err(tokio_postgres_pool_error(e)),
(sql, binds, None),
));
}
};
let row_stream = match client.query_raw(&sql, &binds).await {
Ok(s) => s,
Err(e) => return Some((Err(Error::from(e)), (sql, binds, None))),
};
stream = Some(Box::pin(row_stream));
}
let s = stream.as_mut()?;
match s.next().await {
Some(Ok(row)) => {
Some((Ok(RawRow::PostgresNative(row)), (sql, binds, stream)))
}
Some(Err(e)) => Some((Err(Error::from(e)), (sql, binds, None))),
None => None,
}
}
},
))
}
}
pub(crate) struct TokioPostgresTransaction {
client: Option<Object>,
runtime: Option<tokio::runtime::Handle>,
}
impl fmt::Debug for TokioPostgresTransaction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TokioPostgresTransaction")
.finish_non_exhaustive()
}
}
impl TokioPostgresTransaction {
fn client(&self) -> Result<&Object, Error> {
self.client
.as_ref()
.ok_or_else(|| Error::Message("transaction already finished".into()))
}
pub(crate) async fn commit(mut self) -> Result<(), Error> {
self.finish("COMMIT").await
}
pub(crate) async fn rollback(mut self) -> Result<(), Error> {
self.finish("ROLLBACK").await
}
async fn finish(&mut self, stmt: &'static str) -> Result<(), Error> {
let Some(client) = self.client.take() else {
return Err(Error::Message("transaction already finished".into()));
};
let result = client.execute(stmt, &[]).await.map_err(Error::from);
if result.is_err() && stmt != "ROLLBACK" {
let _ = client.execute("ROLLBACK", &[]).await;
}
result.map(|_| ())
}
pub(crate) async fn execute(&self, sql: &str, binds: &[Value]) -> Result<u64, Error> {
let params = bind_params(binds)?;
self.client()?
.execute(sql, ¶ms)
.await
.map_err(Error::from)
}
pub(crate) async fn fetch_all(&self, sql: &str, binds: &[Value]) -> Result<RowBatch, Error> {
let params = bind_params(binds)?;
let rows = self
.client()?
.query(sql, ¶ms)
.await
.map_err(Error::from)?;
Ok(RowBatch::PostgresNative(rows))
}
}
impl Drop for TokioPostgresTransaction {
fn drop(&mut self) {
let Some(client) = self.client.take() else {
return;
};
tracing::warn!(
target: "ruprizzle::query",
"transaction dropped without commit or rollback; rolling back"
);
let Some(runtime) = self.runtime.take() else {
tracing::warn!(
target: "ruprizzle::query",
"no tokio runtime available to roll back an abandoned transaction; \
discarding the connection"
);
let _ = Object::take(client);
return;
};
runtime.spawn(async move {
if let Err(error) = client.execute("ROLLBACK", &[]).await {
tracing::warn!(
target: "ruprizzle::query",
error = %error,
"failed to roll back an abandoned transaction"
);
}
drop(client);
});
}
}
fn bind_params(binds: &[Value]) -> Result<Vec<&(dyn ToSql + Sync)>, Error> {
let mut params = Vec::with_capacity(binds.len());
for b in binds {
params.push(b as &(dyn ToSql + Sync));
}
Ok(params)
}
pub trait FromTokioPostgresRow: Sized + Send + Sync + 'static {
fn from_tokio_postgres_row(row: &Row) -> Result<Self, Error>;
}
macro_rules! tuple_from_tokio_postgres_row {
($($T:ident $idx:tt),+) => {
impl<$($T: tokio_postgres::types::FromSqlOwned + Send + Sync + 'static),+> FromTokioPostgresRow for ($($T,)+) {
fn from_tokio_postgres_row(row: &Row) -> Result<Self, Error> {
Ok((
$(
row.try_get::<usize, $T>($idx)?
,)+
))
}
}
};
}
tuple_from_tokio_postgres_row! { A 0 }
tuple_from_tokio_postgres_row! { A 0, B 1 }
tuple_from_tokio_postgres_row! { A 0, B 1, C 2 }
tuple_from_tokio_postgres_row! { A 0, B 1, C 2, D 3 }
tuple_from_tokio_postgres_row! { A 0, B 1, C 2, D 3, E 4 }
tuple_from_tokio_postgres_row! { A 0, B 1, C 2, D 3, E 4, F 5 }
tuple_from_tokio_postgres_row! { A 0, B 1, C 2, D 3, E 4, F 5, G 6 }
tuple_from_tokio_postgres_row! { A 0, B 1, C 2, D 3, E 4, F 5, G 6, H 7 }
#[macro_export]
#[cfg(feature = "postgres-tokio-postgres")]
macro_rules! tokio_postgres_default_row {
($t:ty) => {
impl $crate::tokio_postgres::FromTokioPostgresRow for $t {
fn from_tokio_postgres_row(
_: &$crate::tokio_postgres::Row,
) -> Result<Self, $crate::Error> {
Ok(<$t>::default())
}
}
};
}
impl TokioPostgresPool {
pub(crate) async fn close(&self) {
self.inner.close();
}
}
impl ToSql for Value {
fn to_sql(
&self,
ty: &Type,
out: &mut BytesMut,
) -> Result<IsNull, Box<dyn std::error::Error + Sync + Send>> {
self.to_sql_checked(ty, out)
}
fn accepts(_ty: &Type) -> bool
where
Self: Sized,
{
true
}
fn to_sql_checked(
&self,
ty: &Type,
out: &mut BytesMut,
) -> Result<IsNull, Box<dyn std::error::Error + Sync + Send>> {
match self {
Value::Null => Ok(IsNull::Yes),
Value::Bool(b) => <bool as ToSql>::to_sql_checked(b, ty, out),
Value::I32(i) => <i32 as ToSql>::to_sql_checked(i, ty, out),
Value::I64(i) => <i64 as ToSql>::to_sql_checked(i, ty, out),
Value::F64(f) => <f64 as ToSql>::to_sql_checked(f, ty, out),
Value::Decimal(d) => <crate::types::Decimal as ToSql>::to_sql_checked(d, ty, out),
Value::Str(s) => {
let s_ref: &str = s.as_ref();
<&str as ToSql>::to_sql_checked(&s_ref, ty, out)
}
Value::Uuid(u) => <crate::types::Uuid as ToSql>::to_sql_checked(u, ty, out),
Value::DateTime(dt) => {
<chrono::DateTime<chrono::Utc> as ToSql>::to_sql_checked(dt, ty, out)
}
Value::Date(d) => <chrono::NaiveDate as ToSql>::to_sql_checked(d, ty, out),
Value::Time(t) => <chrono::NaiveTime as ToSql>::to_sql_checked(t, ty, out),
Value::Json(v) => <serde_json::Value as ToSql>::to_sql_checked(v, ty, out),
Value::Bytes(b) => {
let bytes: &[u8] = b.as_ref();
<&[u8] as ToSql>::to_sql_checked(&bytes, ty, out)
}
Value::Array(values) => {
if values.iter().any(|v| matches!(v, Value::Array(_))) {
return Err("nested arrays are not supported".into());
}
pg_tokio_array_encode!(values, ty, out, Value::Bool(_), Value::Bool(b), bool, *b);
pg_tokio_array_encode!(values, ty, out, Value::I32(_), Value::I32(i), i32, *i);
pg_tokio_array_encode!(values, ty, out, Value::I64(_), Value::I64(i), i64, *i);
pg_tokio_array_encode!(values, ty, out, Value::F64(_), Value::F64(f), f64, *f);
pg_tokio_array_encode!(
values,
ty,
out,
Value::Decimal(_),
Value::Decimal(d),
Decimal,
*d
);
pg_tokio_array_encode!(
values,
ty,
out,
Value::Str(_),
Value::Str(s),
String,
s.to_string()
);
pg_tokio_array_encode!(values, ty, out, Value::Uuid(_), Value::Uuid(u), Uuid, *u);
pg_tokio_array_encode!(
values,
ty,
out,
Value::DateTime(_),
Value::DateTime(dt),
DateTime<Utc>,
*dt
);
pg_tokio_array_encode!(
values,
ty,
out,
Value::Date(_),
Value::Date(d),
NaiveDate,
*d
);
pg_tokio_array_encode!(
values,
ty,
out,
Value::Time(_),
Value::Time(t),
NaiveTime,
*t
);
pg_tokio_array_encode!(
values,
ty,
out,
Value::Json(_),
Value::Json(v),
serde_json::Value,
v.clone()
);
pg_tokio_array_encode!(
values,
ty,
out,
Value::Bytes(_),
Value::Bytes(b),
Vec<u8>,
b.to_vec()
);
let empty: Vec<Option<String>> = Vec::new();
<Vec<Option<String>> as ToSql>::to_sql_checked(&empty, ty, out)
}
}
}
}
fn strip_driver_param(url: &str) -> (bool, String) {
let Some((base, query)) = url.split_once('?') else {
return (false, url.to_owned());
};
let mut parts = Vec::new();
let mut uses_native = false;
for part in query.split('&') {
if part == "driver=tokio-postgres" || part.starts_with("driver=tokio-postgres&") {
uses_native = true;
continue;
}
parts.push(part);
}
let stripped = if parts.is_empty() {
base.to_owned()
} else {
format!("{base}?{}", parts.join("&"))
};
(uses_native, stripped)
}
fn tokio_postgres_pool_error(e: deadpool_postgres::PoolError) -> Error {
match e {
deadpool_postgres::PoolError::Backend(e) => Error::from(e),
deadpool_postgres::PoolError::Timeout(_) => Error::AcquireTimeout {
reason: e.to_string(),
},
_ => Error::ConnectionFailure {
reason: e.to_string(),
},
}
}