Advertisement
Guest User

Untitled

a guest
Apr 13th, 2017
633
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. #[derive(Queryable, Debug)]
  2. pub struct DownloadAnswer {
  3. pub timestamp: NaiveDateTime,
  4. pub email: String,
  5. pub requester: String,
  6. pub answers: Vec<String>
  7. }
  8.  
  9. impl FromSql<(diesel::types::Timestamp, diesel::types::Text, diesel::types::Text, diesel::types::Array<diesel::types::Text>), diesel::pg::Pg> for DownloadAnswer {
  10. fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error + Send + Sync>> {
  11.  
  12. println!("{:?}", bytes);
  13.  
  14. // Not really sure how to parse this
  15. Ok(DownloadAnswer {
  16. timestamp: UTC::now().naive_local(),
  17. email: "bob@squarepants.com".to_owned(),
  18. requester: "Patrick".to_owned(),
  19. answers: vec!("foo".to_owned(), "bar".to_owned())
  20. })
  21. }
  22. }
  23.  
  24. let sql = format!(r"select created_at as timestamp,
  25. a.email as email,
  26. r.name as requester,
  27. answer as answers
  28. from advice_requests
  29. join people as r on advice_requests.requester_id = r.id
  30. join people as a on advice_requests.advisor_id = a.id
  31. join answers on answers.advice_request_id = advice_requests.id
  32. where advice_requests.questionnaire_id = '{the_id}'::uuid;", the_id = questionnaire_id);
  33. println!("The sql was: {}", sql);
  34.  
  35. let query = diesel::expression::sql(&sql);
  36.  
  37. let result = query.get_results::<Vec<DownloadAnswer>>(self.connection);
  38.  
  39. println!("{:?}", result);
  40.  
  41. Example result from running that query in PostgreSQL cli:
  42.  
  43. 2017-04-12 17:34:53.250539 | a@example.com | Bob Sample | First answer from A
  44. 2017-04-12 17:34:53.250539 | a@example.com | Bob Sample | Second answer from A
  45. 2017-04-12 17:35:04.718267 | b@example.com | Bob Sample | First answer from B
  46. 2017-04-12 17:35:04.718267 | b@example.com | Bob Sample | Second answer from B
  47. 2017-04-12 17:35:20.284857 | c@example.com | Bob Sample | First answer from C
  48. 2017-04-12 17:35:20.284857 | c@example.com | Bob Sample | Second answer from C
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement