daily pastebin goal
13%
SHARE
TWEET

Untitled

a guest Jul 12th, 2018 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #[derive(Debug)]
  2. struct Table {
  3.     table: String,
  4. }
  5.  
  6.  
  7. impl Table {
  8.     pub fn new(table: String) -> Self {
  9.         Table{table: table}
  10.     }
  11.    
  12.     pub fn select(self) -> SelectQuery {
  13.         SelectQuery::new(self)
  14.     }
  15. }
  16.  
  17. #[derive(Debug)]
  18. struct SelectQuery {
  19.     table: Table,
  20.     limit: Option<i64>,
  21.     offset: Option<i64>,
  22. }
  23.  
  24.  
  25. impl SelectQuery {
  26.     pub fn new(table: Table) -> Self {
  27.         SelectQuery{table: table, limit: None, offset: None}
  28.     }
  29.     pub fn limit(&mut self, limit: i64) -> &mut Self {
  30.         self.limit = Some(limit);
  31.         self
  32.     }
  33.  
  34.     pub fn offset(&mut self, offset: i64) -> &mut Self {
  35.         self.offset = Some(offset);
  36.         self
  37.     }
  38.    
  39.     pub fn query(&self) -> String {
  40.         let mut query = format!("SELECT * FROM {}", self.table.table);
  41.         if let Some(limit) = self.limit {
  42.             query.push_str(&format!(" LIMIT {}", limit));
  43.         }
  44.         if let Some(offset) = self.offset {
  45.             query.push_str(&format!(" OFFSET {}", offset));
  46.         }
  47.         query.push_str(";");
  48.         query
  49.     }
  50. }
  51.  
  52. fn main() {
  53.     let table = Table::new("mytable".to_string());
  54.     let mut s = table.select();
  55.     s.offset(50).limit(50);
  56.     println!("{:?}", s);
  57.     println!("{}", s.query());
  58. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top