sqlxo/
head.rs

1use core::fmt;
2use std::fmt::{
3	Display,
4	Formatter,
5};
6
7pub enum SelectType {
8	Star,
9	Aggregation(AggregationType),
10}
11
12pub enum AggregationType {
13	Max,
14	Min,
15	Count,
16	Avg,
17}
18
19pub enum BuildType {
20	Select(SelectType),
21	Update,
22	Delete,
23	#[cfg(test)]
24	Raw,
25}
26
27pub struct SqlHead<'a> {
28	build: BuildType,
29	table: &'a str,
30}
31
32impl<'a> SqlHead<'a> {
33	pub fn new(table: &'a str, build: BuildType) -> Self {
34		Self { build, table }
35	}
36}
37
38impl Display for AggregationType {
39	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
40		match self {
41			AggregationType::Max => f.write_str("MAX"),
42			AggregationType::Min => f.write_str("MIN"),
43			AggregationType::Count => f.write_str("COUNT"),
44			AggregationType::Avg => f.write_str("AVG"),
45		}
46	}
47}
48
49impl<'a> Display for SqlHead<'a> {
50	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
51		match &self.build {
52			BuildType::Select(SelectType::Star) => {
53				write!(f, "SELECT * FROM {}", self.table)
54			}
55			BuildType::Select(SelectType::Aggregation(agg)) => {
56				write!(f, "SELECT {}(*) FROM {}", agg, self.table)
57			}
58			BuildType::Update => write!(f, "UPDATE {}", self.table),
59			BuildType::Delete => write!(f, "DELETE FROM {}", self.table),
60			#[cfg(test)]
61			BuildType::Raw => write!(f, ""),
62		}
63	}
64}