Skip to main content

surrealdb_sql/statements/
select.rs

1use common::fmt::Fmt;
2use surrealdb_types::{SqlFormat, ToSql, write_sql};
3
4use crate::order::Ordering;
5use crate::{
6	Cond, CoverStmts, Explain, Expr, Fetchs, Fields, Groups, Limit, Literal, Splits, Start, With,
7};
8
9#[derive(Clone, Debug, Eq, PartialEq)]
10pub struct SelectStatement {
11	/// The foo,bar part in SELECT foo,bar FROM baz.
12	pub fields: Fields,
13	pub omit: Vec<Expr>,
14	pub only: bool,
15	/// The baz part in SELECT foo,bar FROM baz.
16	pub what: Vec<Expr>,
17	pub with: Option<With>,
18	pub cond: Option<Cond>,
19	pub split: Option<Splits>,
20	pub group: Option<Groups>,
21	pub order: Option<Ordering>,
22	pub limit: Option<Limit>,
23	pub start: Option<Start>,
24	pub fetch: Option<Fetchs>,
25	pub version: Expr,
26	pub timeout: Expr,
27	pub explain: Option<Explain>,
28	pub tempfiles: bool,
29	pub for_update: bool,
30}
31
32impl ToSql for SelectStatement {
33	fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
34		write_sql!(f, fmt, "SELECT {}", self.fields);
35		if !self.omit.is_empty() {
36			write_sql!(f, fmt, " OMIT {}", Fmt::comma_separated(self.omit.iter().map(CoverStmts)));
37		}
38		write_sql!(f, fmt, " FROM");
39		if self.only {
40			write_sql!(f, fmt, " ONLY");
41		}
42		write_sql!(f, fmt, " {}", Fmt::comma_separated(self.what.iter().map(CoverStmts)));
43		if let Some(ref v) = self.with {
44			write_sql!(f, fmt, " {v}");
45		}
46		if let Some(ref v) = self.cond {
47			write_sql!(f, fmt, " {v}");
48		}
49		if let Some(ref v) = self.split {
50			write_sql!(f, fmt, " {v}");
51		}
52		if let Some(ref v) = self.group {
53			write_sql!(f, fmt, " {v}");
54		}
55		if let Some(ref v) = self.order {
56			write_sql!(f, fmt, " {v}");
57		}
58		if let Some(ref v) = self.limit {
59			write_sql!(f, fmt, " {v}");
60		}
61		if let Some(ref v) = self.start {
62			write_sql!(f, fmt, " {v}");
63		}
64		if let Some(ref v) = self.fetch {
65			write_sql!(f, fmt, " {v}");
66		}
67		if !matches!(self.version, Expr::Literal(Literal::None)) {
68			write_sql!(f, fmt, " VERSION {}", CoverStmts(&self.version));
69		}
70		if !matches!(self.timeout, Expr::Literal(Literal::None)) {
71			write_sql!(f, fmt, " TIMEOUT {}", CoverStmts(&self.timeout));
72		}
73		if self.for_update {
74			write_sql!(f, fmt, " FOR UPDATE");
75		}
76		if self.tempfiles {
77			write_sql!(f, fmt, " TEMPFILES");
78		}
79		if let Some(ref v) = self.explain {
80			write_sql!(f, fmt, " {v}");
81		}
82	}
83}