Skip to main content

surrealdb_core/sql/statements/
upsert.rs

1use std::fmt;
2
3use crate::sql::fmt::Fmt;
4use crate::sql::{Cond, Data, Explain, Expr, Output, Timeout, With};
5
6#[derive(Clone, Debug, Default, Eq, PartialEq)]
7#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
8pub struct UpsertStatement {
9	pub only: bool,
10	pub what: Vec<Expr>,
11	pub with: Option<With>,
12	pub data: Option<Data>,
13	pub cond: Option<Cond>,
14	pub output: Option<Output>,
15	pub timeout: Option<Timeout>,
16	pub parallel: bool,
17	pub explain: Option<Explain>,
18}
19
20impl fmt::Display for UpsertStatement {
21	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22		write!(f, "UPSERT")?;
23		if self.only {
24			f.write_str(" ONLY")?
25		}
26		write!(f, " {}", Fmt::comma_separated(self.what.iter()))?;
27		if let Some(ref v) = self.with {
28			write!(f, " {v}")?
29		}
30		if let Some(ref v) = self.data {
31			write!(f, " {v}")?
32		}
33		if let Some(ref v) = self.cond {
34			write!(f, " {v}")?
35		}
36		if let Some(ref v) = self.output {
37			write!(f, " {v}")?
38		}
39		if let Some(ref v) = self.timeout {
40			write!(f, " {v}")?
41		}
42		if self.parallel {
43			f.write_str(" PARALLEL")?
44		}
45		if let Some(ref v) = self.explain {
46			write!(f, " {v}")?
47		}
48		Ok(())
49	}
50}
51
52impl From<UpsertStatement> for crate::expr::statements::UpsertStatement {
53	fn from(v: UpsertStatement) -> Self {
54		Self {
55			only: v.only,
56			what: v.what.into_iter().map(From::from).collect(),
57			with: v.with.map(Into::into),
58			data: v.data.map(Into::into),
59			cond: v.cond.map(Into::into),
60			output: v.output.map(Into::into),
61			timeout: v.timeout.map(Into::into),
62			parallel: v.parallel,
63			explain: v.explain.map(Into::into),
64		}
65	}
66}
67
68impl From<crate::expr::statements::UpsertStatement> for UpsertStatement {
69	fn from(v: crate::expr::statements::UpsertStatement) -> Self {
70		Self {
71			only: v.only,
72			what: v.what.into_iter().map(From::from).collect(),
73			with: v.with.map(Into::into),
74			data: v.data.map(Into::into),
75			cond: v.cond.map(Into::into),
76			output: v.output.map(Into::into),
77			timeout: v.timeout.map(Into::into),
78			parallel: v.parallel,
79			explain: v.explain.map(Into::into),
80		}
81	}
82}