pub use tokio_postgres::{Statement, ToStatement};
use crate::ToSql;
use af_core::prelude::*;
use smallstr::SmallString;
use smallvec::SmallVec;
#[derive(Debug)]
pub struct StatementBuilder<'a> {
text: SmallString<[u8; 2048]>,
params: SmallVec<[&'a (dyn ToSql + Sync); 32]>,
}
impl<'a> StatementBuilder<'a> {
pub fn new() -> Self {
Self { text: SmallString::new(), params: SmallVec::new() }
}
pub fn append(&mut self, text: impl Display) {
write!(self.text, "{}", text).unwrap();
}
pub fn add_param(&mut self, param: &'a (dyn ToSql + Sync)) -> usize {
self.params.push(param);
self.params.len()
}
pub fn params(&self) -> &[&'a (dyn ToSql + Sync)] {
&self.params
}
pub fn text(&self) -> &str {
&self.text
}
}
impl Default for StatementBuilder<'_> {
fn default() -> Self {
Self::new()
}
}
impl<'a> fmt::Write for StatementBuilder<'a> {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.text.write_str(s)
}
}