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}
30
31impl ToSql for SelectStatement {
32	fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
33		write_sql!(f, fmt, "SELECT {}", self.fields);
34		if !self.omit.is_empty() {
35			write_sql!(f, fmt, " OMIT {}", Fmt::comma_separated(self.omit.iter().map(CoverStmts)));
36		}
37		write_sql!(f, fmt, " FROM");
38		if self.only {
39			write_sql!(f, fmt, " ONLY");
40		}
41		write_sql!(f, fmt, " {}", Fmt::comma_separated(self.what.iter().map(CoverStmts)));
42		if let Some(ref v) = self.with {
43			write_sql!(f, fmt, " {v}");
44		}
45		if let Some(ref v) = self.cond {
46			write_sql!(f, fmt, " {v}");
47		}
48		if let Some(ref v) = self.split {
49			write_sql!(f, fmt, " {v}");
50		}
51		if let Some(ref v) = self.group {
52			write_sql!(f, fmt, " {v}");
53		}
54		if let Some(ref v) = self.order {
55			write_sql!(f, fmt, " {v}");
56		}
57		if let Some(ref v) = self.limit {
58			write_sql!(f, fmt, " {v}");
59		}
60		if let Some(ref v) = self.start {
61			write_sql!(f, fmt, " {v}");
62		}
63		if let Some(ref v) = self.fetch {
64			write_sql!(f, fmt, " {v}");
65		}
66		if !matches!(self.version, Expr::Literal(Literal::None)) {
67			write_sql!(f, fmt, " VERSION {}", CoverStmts(&self.version));
68		}
69		if !matches!(self.timeout, Expr::Literal(Literal::None)) {
70			write_sql!(f, fmt, " TIMEOUT {}", CoverStmts(&self.timeout));
71		}
72		if let Some(ref v) = self.explain {
73			write_sql!(f, fmt, " {v}");
74		}
75	}
76}