Skip to main content

surql_parser/upstream/sql/
view.rs

1use crate::compat::val::TableName;
2use crate::upstream::fmt::{EscapeKwFreeIdent, Fmt};
3use crate::upstream::sql::{Cond, Fields, Groups};
4use surrealdb_types::{SqlFormat, ToSql, write_sql};
5#[derive(Clone, Debug, PartialEq, Eq)]
6pub struct View {
7	pub expr: Fields,
8	pub what: Vec<String>,
9	pub cond: Option<Cond>,
10	pub group: Option<Groups>,
11}
12impl ToSql for View {
13	fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
14		write_sql!(
15			f,
16			fmt,
17			"AS SELECT {} FROM {}",
18			self.expr,
19			Fmt::comma_separated(self.what.iter().map(|x| EscapeKwFreeIdent(x.as_ref())))
20		);
21		if let Some(ref v) = self.cond {
22			write_sql!(f, fmt, " {v}");
23		}
24		if let Some(ref v) = self.group {
25			write_sql!(f, fmt, " {v}");
26		}
27	}
28}