Skip to main content

surrealdb_expr/expr/
param.rs

1use std::ops::Deref;
2use std::str;
3
4use common::fmt::EscapeKwFreeIdent;
5use surrealdb_strand::Strand;
6use surrealdb_types::{SqlFormat, ToSql};
7
8#[derive(Clone, Debug, Default, Eq, PartialEq, Hash)]
9pub struct Param(Strand);
10
11impl Param {
12	/// Convert into the underlying `Strand`.
13	pub fn into_strand(self) -> Strand {
14		self.0
15	}
16
17	/// returns the identifier section of the parameter,
18	/// i.e. `$foo` without the `$` so: `foo`
19	pub fn as_str(&self) -> &str {
20		self.0.as_str()
21	}
22}
23
24impl From<String> for Param {
25	fn from(v: String) -> Self {
26		Self(v.into())
27	}
28}
29
30impl From<Strand> for Param {
31	fn from(v: Strand) -> Self {
32		Self(v)
33	}
34}
35
36impl Deref for Param {
37	type Target = str;
38	fn deref(&self) -> &Self::Target {
39		self.0.as_str()
40	}
41}
42
43impl ToSql for Param {
44	fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
45		f.push('$');
46		EscapeKwFreeIdent(self.as_str()).fmt_sql(f, fmt);
47	}
48}