Skip to main content

surrealdb_sql/statements/
insert.rs

1use surrealdb_types::{SqlFormat, ToSql, write_sql};
2
3use crate::{CoverStmts, Data, Expr, Literal, Output};
4
5#[derive(Clone, Debug, PartialEq, Eq)]
6pub struct InsertStatement {
7	pub into: Option<Expr>,
8	pub data: Data,
9	/// Does the statement have the ignore clause.
10	pub ignore: bool,
11	pub update: Option<Data>,
12	pub output: Option<Output>,
13	pub timeout: Expr,
14	pub relation: bool,
15}
16
17impl ToSql for InsertStatement {
18	fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
19		f.push_str("INSERT");
20		if self.relation {
21			f.push_str(" RELATION");
22		}
23		if self.ignore {
24			f.push_str(" IGNORE");
25		}
26		if let Some(ref v) = self.into {
27			write_sql!(f, fmt, " INTO {}", CoverStmts(v));
28		}
29		write_sql!(f, fmt, " {}", self.data);
30		if let Some(ref v) = self.update {
31			write_sql!(f, fmt, " {v}");
32		}
33		if let Some(ref v) = self.output {
34			write_sql!(f, fmt, " {v}");
35		}
36		if !matches!(self.timeout, Expr::Literal(Literal::None)) {
37			write_sql!(f, fmt, " TIMEOUT {}", CoverStmts(&self.timeout));
38		}
39	}
40}