Skip to main content

sql_builder/query/
ordered.rs

1use std::borrow::Cow;
2
3use crate::{Expr, IntoColumnRef, NullOrdering, Order, OrderExpr};
4
5pub trait OrderedStatement {
6    #[doc(hidden)]
7    // Implementation for the trait.
8    fn add_order_by(&mut self, order: OrderExpr) -> &mut Self;
9
10    /// Clear order expressions
11    fn clear_order_by(&mut self) -> &mut Self;
12
13    /// Order by column.
14    ///
15    /// # Examples
16    ///
17    /// Order by ASC and DESC
18    /// ```
19    /// use sql_builder::*;
20    ///
21    /// let query = Query::select()
22    ///     .column("aspect")
23    ///     .from("glyph")
24    ///     .and_where(Expr::col("aspect").if_null(0).gt(2))
25    ///     .order_by("image", Order::Desc)
26    ///     .order_by(("glyph", "aspect"), Order::Asc)
27    ///     .to_owned();
28    ///
29    /// assert_eq!(
30    ///     query.to_string(MysqlQueryBuilder).unwrap(),
31    ///     r#"SELECT `aspect` FROM `glyph` WHERE IFNULL(`aspect`, 0) > 2 ORDER BY `image` DESC, `glyph`.`aspect` ASC"#
32    /// );
33    /// ```
34    ///
35    /// Order by custom field ordering
36    /// ```
37    /// use sql_builder::*;
38    ///
39    /// let query = Query::select()
40    ///     .columns(["aspect"])
41    ///     .from("glyph")
42    ///     .order_by(
43    ///         "id",
44    ///         Order::Field(Values(vec![4.into(), 5.into(), 1.into(), 3.into()])),
45    ///     )
46    ///     .to_owned();
47    ///
48    /// assert_eq!(
49    ///     query.to_string(MysqlQueryBuilder).unwrap(),
50    ///     [
51    ///         r#"SELECT `aspect`"#,
52    ///         r#"FROM `glyph`"#,
53    ///         r#"ORDER BY CASE"#,
54    ///         r#"WHEN `id`=4 THEN 0"#,
55    ///         r#"WHEN `id`=5 THEN 1"#,
56    ///         r#"WHEN `id`=1 THEN 2"#,
57    ///         r#"WHEN `id`=3 THEN 3"#,
58    ///         r#"ELSE 4 END"#,
59    ///     ]
60    ///     .join(" ")
61    /// );
62    ///
63    /// assert_eq!(
64    ///     query.to_string(PostgresQueryBuilder).unwrap(),
65    ///     [
66    ///         r#"SELECT "aspect""#,
67    ///         r#"FROM "glyph""#,
68    ///         r#"ORDER BY CASE"#,
69    ///         r#"WHEN "id"=4 THEN 0"#,
70    ///         r#"WHEN "id"=5 THEN 1"#,
71    ///         r#"WHEN "id"=1 THEN 2"#,
72    ///         r#"WHEN "id"=3 THEN 3"#,
73    ///         r#"ELSE 4 END"#,
74    ///     ]
75    ///     .join(" ")
76    /// );
77    ///
78    /// assert_eq!(
79    ///     query.to_string(SqliteQueryBuilder).unwrap(),
80    ///     [
81    ///         r#"SELECT "aspect""#,
82    ///         r#"FROM "glyph""#,
83    ///         r#"ORDER BY CASE"#,
84    ///         r#"WHEN "id"=4 THEN 0"#,
85    ///         r#"WHEN "id"=5 THEN 1"#,
86    ///         r#"WHEN "id"=1 THEN 2"#,
87    ///         r#"WHEN "id"=3 THEN 3"#,
88    ///         r#"ELSE 4 END"#,
89    ///     ]
90    ///     .join(" ")
91    /// );
92    /// ```
93    fn order_by<T>(&mut self, col: T, order: Order) -> &mut Self
94    where
95        T: IntoColumnRef,
96    {
97        self.add_order_by(OrderExpr {
98            expr: Expr::Column(col.into_column_ref()),
99            order,
100            nulls: None,
101        })
102    }
103
104    /// Order by [`Expr`].
105    fn order_by_expr(&mut self, expr: Expr, order: Order) -> &mut Self {
106        self.add_order_by(OrderExpr {
107            expr,
108            order,
109            nulls: None,
110        })
111    }
112
113    /// Order by custom string.
114    fn order_by_customs<I, T>(&mut self, cols: I) -> &mut Self
115    where
116        T: Into<Cow<'static, str>>,
117        I: IntoIterator<Item = (T, Order)>,
118    {
119        cols.into_iter().for_each(|(c, order)| {
120            self.add_order_by(OrderExpr {
121                expr: Expr::Custom(c.into()),
122                order,
123                nulls: None,
124            });
125        });
126        self
127    }
128
129    /// Order by vector of columns.
130    fn order_by_columns<I, T>(&mut self, cols: I) -> &mut Self
131    where
132        T: IntoColumnRef,
133        I: IntoIterator<Item = (T, Order)>,
134    {
135        cols.into_iter().for_each(|(c, order)| {
136            self.add_order_by(OrderExpr {
137                expr: Expr::Column(c.into_column_ref()),
138                order,
139                nulls: None,
140            });
141        });
142        self
143    }
144
145    /// Order by column with nulls order option.
146    ///
147    /// # Examples
148    ///
149    /// ```
150    /// use sql_builder::*;
151    ///
152    /// let query = Query::select()
153    ///     .column("aspect")
154    ///     .from("glyph")
155    ///     .order_by_with_nulls("image", Order::Desc, NullOrdering::Last)
156    ///     .order_by_with_nulls(("glyph", "aspect"), Order::Asc, NullOrdering::First)
157    ///     .to_owned();
158    ///
159    /// assert_eq!(
160    ///     query.to_string(PostgresQueryBuilder).unwrap(),
161    ///     r#"SELECT "aspect" FROM "glyph" ORDER BY "image" DESC NULLS LAST, "glyph"."aspect" ASC NULLS FIRST"#
162    /// );
163    /// assert_eq!(
164    ///     query.to_string(MysqlQueryBuilder).unwrap(),
165    ///     r#"SELECT `aspect` FROM `glyph` ORDER BY `image` IS NULL ASC, `image` DESC, `glyph`.`aspect` IS NULL DESC, `glyph`.`aspect` ASC"#
166    /// );
167    /// ```
168    fn order_by_with_nulls<T>(&mut self, col: T, order: Order, nulls: NullOrdering) -> &mut Self
169    where
170        T: IntoColumnRef,
171    {
172        self.add_order_by(OrderExpr {
173            expr: Expr::Column(col.into_column_ref()),
174            order,
175            nulls: Some(nulls),
176        })
177    }
178
179    /// Order by [`Expr`] with nulls order option.
180    fn order_by_expr_with_nulls(
181        &mut self,
182        expr: Expr,
183        order: Order,
184        nulls: NullOrdering,
185    ) -> &mut Self {
186        self.add_order_by(OrderExpr {
187            expr,
188            order,
189            nulls: Some(nulls),
190        })
191    }
192
193    /// Order by custom string with nulls order option.
194    fn order_by_customs_with_nulls<I, T>(&mut self, cols: I) -> &mut Self
195    where
196        T: Into<Cow<'static, str>>,
197        I: IntoIterator<Item = (T, Order, NullOrdering)>,
198    {
199        cols.into_iter().for_each(|(c, order, nulls)| {
200            self.add_order_by(OrderExpr {
201                expr: Expr::Custom(c.into()),
202                order,
203                nulls: Some(nulls),
204            });
205        });
206        self
207    }
208
209    /// Order by vector of columns with nulls order option.
210    fn order_by_columns_with_nulls<I, T>(&mut self, cols: I) -> &mut Self
211    where
212        T: IntoColumnRef,
213        I: IntoIterator<Item = (T, Order, NullOrdering)>,
214    {
215        cols.into_iter().for_each(|(c, order, nulls)| {
216            self.add_order_by(OrderExpr {
217                expr: Expr::Column(c.into_column_ref()),
218                order,
219                nulls: Some(nulls),
220            });
221        });
222        self
223    }
224}
225
226/// Implements [`OrderedStatement`] over the statement's `Vec<OrderExpr>` field `$orders`, with
227/// inherent copies of the trait's methods so callers need not import it.
228macro_rules! ordered_statement {
229    ($statement:ty, $orders:ident) => {
230        impl $crate::OrderedStatement for $statement {
231            fn add_order_by(&mut self, order: $crate::OrderExpr) -> &mut Self {
232                self.$orders.push(order);
233                self
234            }
235
236            fn clear_order_by(&mut self) -> &mut Self {
237                self.$orders = Vec::new();
238                self
239            }
240        }
241
242        impl $statement {
243            pub fn add_order_by(&mut self, order: $crate::OrderExpr) -> &mut Self {
244                <Self as $crate::OrderedStatement>::add_order_by(self, order)
245            }
246
247            pub fn clear_order_by(&mut self) -> &mut Self {
248                <Self as $crate::OrderedStatement>::clear_order_by(self)
249            }
250
251            pub fn order_by<T: $crate::IntoColumnRef>(
252                &mut self,
253                col: T,
254                order: $crate::Order,
255            ) -> &mut Self {
256                <Self as $crate::OrderedStatement>::order_by(self, col, order)
257            }
258
259            pub fn order_by_expr(&mut self, expr: $crate::Expr, order: $crate::Order) -> &mut Self {
260                <Self as $crate::OrderedStatement>::order_by_expr(self, expr, order)
261            }
262
263            pub fn order_by_customs<I, T>(&mut self, cols: I) -> &mut Self
264            where
265                T: Into<::std::borrow::Cow<'static, str>>,
266                I: IntoIterator<Item = (T, $crate::Order)>,
267            {
268                <Self as $crate::OrderedStatement>::order_by_customs(self, cols)
269            }
270
271            pub fn order_by_columns<I, T>(&mut self, cols: I) -> &mut Self
272            where
273                T: $crate::IntoColumnRef,
274                I: IntoIterator<Item = (T, $crate::Order)>,
275            {
276                <Self as $crate::OrderedStatement>::order_by_columns(self, cols)
277            }
278
279            pub fn order_by_with_nulls<T: $crate::IntoColumnRef>(
280                &mut self,
281                col: T,
282                order: $crate::Order,
283                nulls: $crate::NullOrdering,
284            ) -> &mut Self {
285                <Self as $crate::OrderedStatement>::order_by_with_nulls(self, col, order, nulls)
286            }
287
288            pub fn order_by_expr_with_nulls(
289                &mut self,
290                expr: $crate::Expr,
291                order: $crate::Order,
292                nulls: $crate::NullOrdering,
293            ) -> &mut Self {
294                <Self as $crate::OrderedStatement>::order_by_expr_with_nulls(
295                    self, expr, order, nulls,
296                )
297            }
298
299            pub fn order_by_customs_with_nulls<I, T>(&mut self, cols: I) -> &mut Self
300            where
301                T: Into<::std::borrow::Cow<'static, str>>,
302                I: IntoIterator<Item = (T, $crate::Order, $crate::NullOrdering)>,
303            {
304                <Self as $crate::OrderedStatement>::order_by_customs_with_nulls(self, cols)
305            }
306
307            pub fn order_by_columns_with_nulls<I, T>(&mut self, cols: I) -> &mut Self
308            where
309                T: $crate::IntoColumnRef,
310                I: IntoIterator<Item = (T, $crate::Order, $crate::NullOrdering)>,
311            {
312                <Self as $crate::OrderedStatement>::order_by_columns_with_nulls(self, cols)
313            }
314        }
315    };
316}
317pub(crate) use ordered_statement;