1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use postgres_types::ToSql;

pub enum Join {
  Inner(String, String),
  Left(String, String),
  LeftOuter(String, String),
}

impl Join {
  pub fn to_string(&self) -> String {
    match self {
      Join::Inner(table, constraint) => format!("INNER JOIN {} ON {}", table, constraint),
      Join::Left(table, constraint) => format!("LEFT JOIN {} ON {}", table, constraint),
      Join::LeftOuter(table, constraint) => format!("LEFT OUTER JOIN {} ON {}", table, constraint),
    }
  }
}

pub trait QueryBuilder {
  fn has_params(&self) -> bool;
  fn next_index(&self) -> usize;
  fn get_query(&self) -> String;
  fn get_ref_params(self) -> Vec<&'static (dyn ToSql + Sync)>;
}

pub trait QueryBuilderWithWhere {
  fn where_eq<T: 'static + ToSql + Sync + Clone>(&mut self, field: &str, value: T);
  fn where_ne<T: 'static + ToSql + Sync + Clone>(&mut self, field: &str, value: T);
}

pub trait QueryBuilderWithGroupBy {
  fn group_by(&mut self, field: &str);
}

pub trait QueryBuilderWithLimit {
  fn limit(&mut self, limit: i64);
}

pub trait QueryBuilderWithOffset {
  fn offset(&mut self, offset: i64);
}

pub trait QueryBuilderWithJoin {
  fn inner_join(&mut self, table_name: &str, relation: &str);
  fn left_join(&mut self, table_name: &str, relation: &str);
  fn left_outer_join(&mut self, table_name: &str, relation: &str);
}

pub trait QueryBuilderWithSet {
  fn set<T: 'static + ToSql + Sync + Clone>(&mut self, field: &str, value: T);
  fn set_computed(&mut self, field: &str, value: &str);
}

pub enum Order {
  Asc(String),
  Desc(String),
}

impl Order {
  pub fn to_string(&self) -> String {
    match self {
      Order::Asc(column) => format!("{} ASC", column),
      Order::Desc(column) => format!("{} DESC", column),
    }
  }
}

pub trait QueryBuilderWithOrder {
  fn order_by(&mut self, field: Order);
}