surrealdb_sql/
order.rs

1use crate::fmt::Fmt;
2use crate::idiom::Idiom;
3use revision::revisioned;
4use serde::{Deserialize, Serialize};
5use std::fmt;
6use std::ops::Deref;
7
8#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
9#[revisioned(revision = 1)]
10pub struct Orders(pub Vec<Order>);
11
12impl Deref for Orders {
13	type Target = Vec<Order>;
14	fn deref(&self) -> &Self::Target {
15		&self.0
16	}
17}
18
19impl IntoIterator for Orders {
20	type Item = Order;
21	type IntoIter = std::vec::IntoIter<Self::Item>;
22	fn into_iter(self) -> Self::IntoIter {
23		self.0.into_iter()
24	}
25}
26
27impl fmt::Display for Orders {
28	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29		write!(f, "ORDER BY {}", Fmt::comma_separated(&self.0))
30	}
31}
32
33#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
34#[revisioned(revision = 1)]
35pub struct Order {
36	pub order: Idiom,
37	pub random: bool,
38	pub collate: bool,
39	pub numeric: bool,
40	pub direction: bool,
41}
42
43impl Deref for Order {
44	type Target = Idiom;
45	fn deref(&self) -> &Self::Target {
46		&self.order
47	}
48}
49
50impl fmt::Display for Order {
51	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52		write!(f, "{}", self.order)?;
53		if self.random {
54			write!(f, "RAND()")?;
55		}
56		if self.collate {
57			write!(f, " COLLATE")?;
58		}
59		if self.numeric {
60			write!(f, " NUMERIC")?;
61		}
62		match self.direction {
63			false => write!(f, " DESC")?,
64			true => (),
65		};
66		Ok(())
67	}
68}