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				if rel.lightweight {
46					write_sql!(f, sql_fmt, " LIGHTWEIGHT");
47				}
48			}
49			TableType::Any => {
50				write_sql!(f, sql_fmt, " ANY");
51			}
52		}
53	}
54}
55
56#[derive(Debug, Default, Clone, PartialEq, Eq)]
57#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
58pub struct Relation {
59	#[cfg_attr(feature = "arbitrary", arbitrary(with = crate::arbitrary::atleast_one))]
60	pub from: Vec<TableName>,
61	#[cfg_attr(feature = "arbitrary", arbitrary(with = crate::arbitrary::atleast_one))]
62	pub to: Vec<TableName>,
63	pub enforced: bool,
64	/// Store edges as vertex-side adjacency keys alone, with no records.
65	pub lightweight: bool,
66}