Skip to main content

toasty_sql/serializer/
params.rs

1use crate::serializer::ExprContext;
2
3use super::{Formatter, ToSql};
4
5/// A positional bind-parameter placeholder.
6///
7/// The inner `usize` is the 1-based parameter index. The serializer renders
8/// it in the target dialect's format (`$1`, `?1`, or `?`).
9///
10/// # Example
11///
12/// ```
13/// use toasty_sql::serializer::Placeholder;
14///
15/// let p = Placeholder(3);
16/// assert_eq!(p.0, 3);
17/// ```
18pub struct Placeholder(pub usize);
19
20impl ToSql for Placeholder {
21    fn to_sql(self, _cx: &ExprContext<'_>, f: &mut Formatter<'_>) {
22        use std::fmt::Write;
23
24        match f.serializer.flavor {
25            super::Flavor::Mysql => write!(&mut f.dst, "?").unwrap(),
26            super::Flavor::Postgresql => write!(&mut f.dst, "${}", self.0).unwrap(),
27            super::Flavor::Sqlite => write!(&mut f.dst, "?{}", self.0).unwrap(),
28        }
29    }
30}