Skip to main content

clickhouse/sql/
bind.rs

1use std::fmt;
2
3use super::{escape, ser};
4use serde::Serialize;
5
6pub trait Bind: sealed::Sealed {
7    #[doc(hidden)]
8    fn write(&self, dst: &mut impl fmt::Write) -> Result<(), String>;
9}
10
11impl<S: Serialize> sealed::Sealed for S {}
12
13impl<S: Serialize> Bind for S {
14    #[inline]
15    fn write(&self, mut dst: &mut impl fmt::Write) -> Result<(), String> {
16        ser::write_arg(&mut dst, self)
17    }
18}
19
20/// Bound the provided string as an identifier.
21/// It can be used for table names, for instance.
22#[derive(Copy, Clone)]
23pub struct Identifier<'a>(pub &'a str);
24
25impl sealed::Sealed for Identifier<'_> {}
26
27impl Bind for Identifier<'_> {
28    #[inline]
29    fn write(&self, dst: &mut impl fmt::Write) -> Result<(), String> {
30        escape::identifier(self.0, dst).map_err(|err| err.to_string())
31    }
32}
33
34mod sealed {
35    pub trait Sealed {}
36}