Skip to main content

surrealdb_sql/
table_type.rs

1use common::fmt::EscapeKwFreeIdent;
2use surrealdb_types::{SqlFormat, ToSql, write_sql};
3
4use crate::TableName;
5
6/// The type of records stored by a table
7#[derive(Debug, Default, Clone, PartialEq, Eq)]
8#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
9pub enum TableType {
10	#[default]
11	Any,
12	Normal,
13	Relation(Relation),
14}
15
16impl ToSql for TableType {
17	fn fmt_sql(&self, f: &mut String, sql_fmt: SqlFormat) {
18		match self {
19			TableType::Normal => {
20				write_sql!(f, sql_fmt, " NORMAL");
21			}
22			TableType::Relation(rel) => {
23				write_sql!(f, sql_fmt, " RELATION");
24				if !rel.from.is_empty() {
25					f.push_str(" IN ");
26					for (idx, k) in rel.from.iter().enumerate() {
27						if idx != 0 {
28							f.push_str(" | ");
29						}
30						write_sql!(f, sql_fmt, "{}", EscapeKwFreeIdent(k.as_str()))
31					}
32				}
33				if !rel.to.is_empty() {
34					f.push_str(" OUT ");
35					for (idx, k) in rel.to.iter().enumerate() {
36						if idx != 0 {
37							f.push_str(" | ");
38						}
39						write_sql!(f, sql_fmt, "{}", EscapeKwFreeIdent(k.as_str()))
40					}
41				}
42				if rel.enforced {
43					write_sql!(f, sql_fmt, " ENFORCED");
44				}
45			}
46			TableType::Any => {
47				write_sql!(f, sql_fmt, " ANY");
48			}
49		}
50	}
51}
52
53#[derive(Debug, Default, Clone, PartialEq, Eq)]
54#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
55pub struct Relation {
56	#[cfg_attr(feature = "arbitrary", arbitrary(with = crate::arbitrary::atleast_one))]
57	pub from: Vec<TableName>,
58	#[cfg_attr(feature = "arbitrary", arbitrary(with = crate::arbitrary::atleast_one))]
59	pub to: Vec<TableName>,
60	pub enforced: bool,
61}