surql_parser/upstream/sql/
order.rs1use crate::upstream::fmt::Fmt;
2use crate::upstream::sql::Idiom;
3use std::ops::Deref;
4use surrealdb_types::{SqlFormat, ToSql, write_sql};
5#[derive(Clone, Debug, PartialEq, Eq)]
6#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
7pub enum Ordering {
8 Random,
9 Order(OrderList),
10}
11impl surrealdb_types::ToSql for Ordering {
12 fn fmt_sql(&self, f: &mut String, fmt: surrealdb_types::SqlFormat) {
13 match self {
14 Ordering::Random => f.push_str("ORDER BY RAND()"),
15 Ordering::Order(list) => {
16 write_sql!(f, fmt, "ORDER BY {}", list);
17 }
18 }
19 }
20}
21#[derive(Clone, Debug, PartialEq, Eq)]
22#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
23pub struct OrderList(
24 #[cfg_attr(
25 feature = "arbitrary",
26 arbitrary(with = crate::upstream::sql::arbitrary::atleast_one)
27 )]
28 pub Vec<Order>,
29);
30impl Deref for OrderList {
31 type Target = Vec<Order>;
32 fn deref(&self) -> &Self::Target {
33 &self.0
34 }
35}
36impl ToSql for OrderList {
37 fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
38 write_sql!(f, fmt, "{}", Fmt::comma_separated(&self.0))
39 }
40}
41#[derive(Clone, Debug, Default, Eq, PartialEq)]
42#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
43pub struct Order {
44 #[cfg_attr(
46 feature = "arbitrary",
47 arbitrary(with = crate::upstream::sql::arbitrary::basic_idiom)
48 )]
49 pub value: Idiom,
50 pub collate: bool,
51 pub numeric: bool,
52 pub direction: bool,
54}
55impl ToSql for Order {
56 fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
57 self.value.fmt_sql(f, fmt);
58 if self.collate {
59 f.push_str(" COLLATE");
60 }
61 if self.numeric {
62 f.push_str(" NUMERIC");
63 }
64 if !self.direction {
65 f.push_str(" DESC");
66 }
67 }
68}