use crate::expr::{Expr, NonNull, Scalar};
use crate::traits::{SQLParam, ToSQL};
use crate::types::Any;
use crate::{Param, SQL};
use core::fmt;
#[derive(Default, Debug, Clone, Hash, Copy, PartialEq, Eq)]
pub struct Placeholder {
pub name: Option<&'static str>,
}
impl Placeholder {
pub const fn named(name: &'static str) -> Self {
Placeholder { name: Some(name) }
}
pub const fn anonymous() -> Self {
Placeholder { name: None }
}
}
impl<'a, V: SQLParam + 'a> ToSQL<'a, V> for Placeholder {
fn to_sql(&self) -> SQL<'a, V> {
SQL {
chunks: smallvec::smallvec![crate::SQLChunk::Param(Param {
value: None,
placeholder: *self,
})],
}
}
}
impl<'a, V: SQLParam + 'a> Expr<'a, V> for Placeholder {
type SQLType = Any;
type Nullable = NonNull;
type Aggregate = Scalar;
}
impl fmt::Display for Placeholder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.name {
Some(name) => write!(f, ":{}", name),
None => write!(f, "?"),
}
}
}