Skip to main content

toasty_sql/serializer/
params.rs

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