surrealdb_core/sql/
table.rs

1use crate::sql::{escape::escape_ident, fmt::Fmt, strand::no_nul_bytes, Id, Ident, Thing};
2use revision::revisioned;
3use serde::{Deserialize, Serialize};
4use std::fmt::{self, Display, Formatter};
5use std::ops::Deref;
6use std::str;
7
8pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Table";
9
10#[revisioned(revision = 1)]
11#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash, Ord)]
12#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
13#[non_exhaustive]
14pub struct Tables(pub Vec<Table>);
15
16impl From<Table> for Tables {
17	fn from(v: Table) -> Self {
18		Tables(vec![v])
19	}
20}
21
22impl Deref for Tables {
23	type Target = Vec<Table>;
24	fn deref(&self) -> &Self::Target {
25		&self.0
26	}
27}
28
29impl Display for Tables {
30	fn fmt(&self, f: &mut Formatter) -> fmt::Result {
31		Display::fmt(&Fmt::comma_separated(&self.0), f)
32	}
33}
34
35#[revisioned(revision = 1)]
36#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash, Ord)]
37#[serde(rename = "$surrealdb::private::sql::Table")]
38#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
39#[non_exhaustive]
40pub struct Table(#[serde(with = "no_nul_bytes")] pub String);
41
42impl From<String> for Table {
43	fn from(v: String) -> Self {
44		Self(v)
45	}
46}
47
48impl From<&str> for Table {
49	fn from(v: &str) -> Self {
50		Self::from(String::from(v))
51	}
52}
53
54impl From<Ident> for Table {
55	fn from(v: Ident) -> Self {
56		Self(v.0)
57	}
58}
59
60impl Deref for Table {
61	type Target = String;
62	fn deref(&self) -> &Self::Target {
63		&self.0
64	}
65}
66
67impl Table {
68	pub fn generate(&self) -> Thing {
69		Thing {
70			tb: self.0.to_owned(),
71			id: Id::rand(),
72		}
73	}
74}
75
76impl Display for Table {
77	fn fmt(&self, f: &mut Formatter) -> fmt::Result {
78		Display::fmt(&escape_ident(&self.0), f)
79	}
80}