use std::borrow::Cow;
use drizzle_core::{
param::{OwnedParam, Param},
prepared::{
OwnedPreparedStatement as CoreOwnedPreparedStatement,
PreparedStatement as CorePreparedStatement,
},
traits::ToSQL,
};
use drizzle_postgres::values::{OwnedPostgresValue, PostgresValue};
use tokio_postgres::{Client, Row, types::ToSql};
use crate::builder::postgres::prepared_common::postgres_prepared_async_impl;
#[derive(Debug, Clone)]
pub struct PreparedStatement<'a> {
pub inner: CorePreparedStatement<'a, PostgresValue<'a>>,
}
impl From<OwnedPreparedStatement> for PreparedStatement<'_> {
fn from(value: OwnedPreparedStatement) -> Self {
let postgres_params = value.inner.params.iter().map(|v| {
Param::new(
v.placeholder,
v.value.clone().map(|v| Cow::Owned(PostgresValue::from(v))),
)
});
let inner = CorePreparedStatement {
text_segments: value.inner.text_segments,
params: postgres_params.collect::<Box<[_]>>(),
sql: value.inner.sql,
};
PreparedStatement { inner }
}
}
impl<'a> PreparedStatement<'a> {
pub fn sql(&self) -> &str {
self.inner.sql()
}
pub fn param_count(&self) -> usize {
self.inner.params.len()
}
pub fn into_owned(self) -> OwnedPreparedStatement {
let owned_params = self
.inner
.params
.into_vec()
.into_iter()
.map(|p| OwnedParam {
placeholder: p.placeholder,
value: p.value.map(|v| OwnedPostgresValue::from(v.into_owned())),
});
let inner = CoreOwnedPreparedStatement {
text_segments: self.inner.text_segments,
params: owned_params.collect::<Box<[_]>>(),
sql: self.inner.sql,
};
OwnedPreparedStatement { inner }
}
}
#[derive(Debug, Clone)]
pub struct OwnedPreparedStatement {
pub inner: CoreOwnedPreparedStatement<OwnedPostgresValue>,
}
impl<'a> From<PreparedStatement<'a>> for OwnedPreparedStatement {
fn from(value: PreparedStatement<'a>) -> Self {
value.into_owned()
}
}
impl OwnedPreparedStatement {
pub fn sql(&self) -> &str {
self.inner.sql()
}
pub fn param_count(&self) -> usize {
self.inner.params.len()
}
}
postgres_prepared_async_impl!(Client, Row, ToSql);
impl<'a> std::fmt::Display for PreparedStatement<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.inner)
}
}
impl std::fmt::Display for OwnedPreparedStatement {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.inner)
}
}
impl<'a> ToSQL<'a, PostgresValue<'a>> for PreparedStatement<'a> {
fn to_sql(&self) -> drizzle_core::sql::SQL<'a, PostgresValue<'a>> {
self.inner.to_sql()
}
}
impl<'a> ToSQL<'a, OwnedPostgresValue> for OwnedPreparedStatement {
fn to_sql(&self) -> drizzle_core::sql::SQL<'a, OwnedPostgresValue> {
self.inner.to_sql()
}
}