surrealdb-core 3.2.3

A scalable, distributed, collaborative, document-graph database, for the realtime web
Documentation
use surrealdb_strand::Strand;
use surrealdb_types::{SqlFormat, ToSql, write_sql};

use crate::fmt::EscapeKwFreeIdent;

#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct Param(Strand);

impl Param {
	/// Create a new identifier
	///
	/// This function checks if the string has a null byte, returns None if it
	/// has.
	pub fn new(str: impl Into<Strand>) -> Self {
		Self(str.into())
	}

	/// Returns the identifier without the leading `$`.
	pub fn as_str(&self) -> &str {
		self.0.as_str()
	}

	// Convert into a string.
	pub fn into_string(self) -> String {
		self.0.into_string()
	}

	/// Convert into the underlying `Strand`.
	pub fn into_strand(self) -> Strand {
		self.0
	}
}

impl ToSql for Param {
	fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
		write_sql!(f, fmt, "${}", EscapeKwFreeIdent(self.as_str()))
	}
}

impl From<Param> for crate::expr::Param {
	fn from(v: Param) -> Self {
		crate::expr::Param::from(v.0)
	}
}

impl From<crate::expr::Param> for Param {
	fn from(v: crate::expr::Param) -> Self {
		Self(v.into_strand())
	}
}