use std::sync::Arc;
use tokio_postgres::types::ToSql;
#[must_use]
pub struct Query {
sql: String,
params: Vec<Arc<dyn ToSql + Sync + Send>>,
tag: Option<String>,
}
impl Query {
pub fn new(sql: impl Into<String>) -> Self {
Self {
sql: sql.into(),
params: Vec::new(),
tag: None,
}
}
pub fn tag(mut self, tag: impl Into<String>) -> Self {
self.tag = Some(tag.into());
self
}
pub fn bind<T>(mut self, value: T) -> Self
where
T: ToSql + Sync + Send + 'static,
{
self.params.push(Arc::new(value));
self
}
pub fn sql(&self) -> &str {
&self.sql
}
pub fn params_ref(&self) -> Vec<&(dyn ToSql + Sync)> {
self.params
.iter()
.map(|p| p.as_ref() as &(dyn ToSql + Sync))
.collect()
}
impl_query_exec! {
prepare(self) {
let sql = &self.sql;
let params = self.params_ref();
let tag = self.tag.as_deref();
(sql, params, tag)
}
}
}