surql_parser/upstream/sql/statements/
relate.rs1use crate::upstream::fmt::CoverStmts;
2use crate::upstream::sql::{Data, Expr, Literal, Output, RecordIdKeyLit, RecordIdLit};
3use surrealdb_types::{SqlFormat, ToSql, write_sql};
4#[derive(Clone, Debug, PartialEq, Eq)]
5#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
6pub struct RelateStatement {
7 pub only: bool,
8 pub through: Expr,
10 pub from: Expr,
12 pub to: Expr,
14 pub data: Option<Data>,
16 pub output: Option<Output>,
18 pub timeout: Expr,
20}
21impl ToSql for RelateStatement {
22 fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
23 write_sql!(f, fmt, "RELATE");
24 if self.only {
25 write_sql!(f, fmt, " ONLY");
26 }
27 write_sql!(f, fmt, " ");
28 if matches!(
29 self.from,
30 Expr::Literal(
31 Literal::Array(_)
32 | Literal::RecordId(RecordIdLit {
33 key: RecordIdKeyLit::Number(_)
34 | RecordIdKeyLit::String(_)
35 | RecordIdKeyLit::Generate(_)
36 | RecordIdKeyLit::Array(_)
37 | RecordIdKeyLit::Object(_)
38 | RecordIdKeyLit::Uuid(_),
39 ..
40 })
41 ) | Expr::Param(_)
42 ) {
43 self.from.fmt_sql(f, fmt);
44 } else {
45 write_sql!(f, fmt, "(");
46 self.from.fmt_sql(f, fmt);
47 write_sql!(f, fmt, ")");
48 }
49 write_sql!(f, fmt, " -> ");
50 if matches!(self.through, Expr::Param(_) | Expr::Table(_)) {
51 self.through.fmt_sql(f, fmt);
52 } else {
53 write_sql!(f, fmt, "(");
54 self.through.fmt_sql(f, fmt);
55 write_sql!(f, fmt, ")");
56 }
57 write_sql!(f, fmt, " -> ");
58 if matches!(
59 self.to,
60 Expr::Literal(
61 Literal::Array(_)
62 | Literal::RecordId(RecordIdLit {
63 key: RecordIdKeyLit::Number(_)
64 | RecordIdKeyLit::String(_)
65 | RecordIdKeyLit::Generate(_)
66 | RecordIdKeyLit::Array(_)
67 | RecordIdKeyLit::Object(_)
68 | RecordIdKeyLit::Uuid(_),
69 ..
70 })
71 ) | Expr::Param(_)
72 ) {
73 self.to.fmt_sql(f, fmt);
74 } else {
75 write_sql!(f, fmt, "(");
76 self.to.fmt_sql(f, fmt);
77 write_sql!(f, fmt, ")");
78 }
79 if let Some(ref v) = self.data {
80 write_sql!(f, fmt, " {v}");
81 }
82 if let Some(ref v) = self.output {
83 write_sql!(f, fmt, " {v}");
84 }
85 if !matches!(self.timeout, Expr::Literal(Literal::None)) {
86 write_sql!(f, fmt, " TIMEOUT {}", CoverStmts(&self.timeout));
87 }
88 }
89}