derive_sql/
selectable.rs

1#[cfg(test)]
2use super::*;
3
4mod value; // use value::Value;
5pub mod filter;
6
7mod simplefilter; pub use simplefilter::{SimpleFilter};
8mod simplelimit; pub use simplelimit::{SimpleLimit};
9mod simpleoffset; pub use simpleoffset::{SimpleOffset};
10mod simpleorder; pub use simpleorder::{SimpleOrder, Order};
11
12/// Definition of trait `Selectable`. This trait outputs filter, limit and offset statement.
13pub trait Selectable {
14  fn statement(&self) -> String {
15    vec! [
16      self.filter().map(|f| format!("WHERE {f}")).unwrap_or_default(),
17      self.order_by().map(|v| format!("ORDER BY {v}")).unwrap_or_default(),
18      self.limit().map(|v|  format!("LIMIT {v}")).unwrap_or_default(),
19      self.offset().map(|v| format!("OFFSET {v}")).unwrap_or_default(),
20    ].into_iter().filter(|v| v.len() > 0).collect::<Vec<_>>().join(" ")
21  }
22
23  /// Output filter statement - which will be appended to the `WHERE` clause - such as:
24  /// * `field='banana'`: item where the entry `field` is equal to `'banana'`;
25  /// * `1=0`: return no items;
26  /// * `1=1`: return all items;
27  ///
28  /// Return `None` if no filtering is requested.
29  fn filter(&self) -> Option<String>;
30
31  /// Output limit statement - which will be appended to the `LIMIT` clause - such as:
32  /// * `10`: returns only 10 entries
33  ///
34  /// Return `None` if no limit is requested.
35  fn limit(&self)  -> Option<usize>;
36
37  /// Output offset statement - which will be appended to the `OFFSET` clause - such as:
38  /// * `5`: skip the first 5 entry
39  ///
40  /// Return `None` if no offset is requested.
41  fn offset(&self) -> Option<usize>;
42
43  /// Output order by statement - which will be appended to the `ORDER BY` clause - such as:
44  /// * `name DESC`: order by descending name
45  ///
46  /// Return `None` if no order is requested
47  fn order_by(&self) -> Option<String>;
48}