Skip to main content

surql_parser/upstream/sql/statements/
insert.rs

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