Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Queryable, Debug)]
- pub struct DownloadAnswer {
- pub timestamp: NaiveDateTime,
- pub email: String,
- pub requester: String,
- pub answers: Vec<String>
- }
- impl FromSql<(diesel::types::Timestamp, diesel::types::Text, diesel::types::Text, diesel::types::Array<diesel::types::Text>), diesel::pg::Pg> for DownloadAnswer {
- fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error + Send + Sync>> {
- println!("{:?}", bytes);
- // Not really sure how to parse this
- Ok(DownloadAnswer {
- timestamp: UTC::now().naive_local(),
- email: "bob@squarepants.com".to_owned(),
- requester: "Patrick".to_owned(),
- answers: vec!("foo".to_owned(), "bar".to_owned())
- })
- }
- }
- let sql = format!(r"select created_at as timestamp,
- a.email as email,
- r.name as requester,
- answer as answers
- from advice_requests
- join people as r on advice_requests.requester_id = r.id
- join people as a on advice_requests.advisor_id = a.id
- join answers on answers.advice_request_id = advice_requests.id
- where advice_requests.questionnaire_id = '{the_id}'::uuid;", the_id = questionnaire_id);
- println!("The sql was: {}", sql);
- let query = diesel::expression::sql(&sql);
- let result = query.get_results::<Vec<DownloadAnswer>>(self.connection);
- println!("{:?}", result);
- Example result from running that query in PostgreSQL cli:
- 2017-04-12 17:34:53.250539 | a@example.com | Bob Sample | First answer from A
- 2017-04-12 17:34:53.250539 | a@example.com | Bob Sample | Second answer from A
- 2017-04-12 17:35:04.718267 | b@example.com | Bob Sample | First answer from B
- 2017-04-12 17:35:04.718267 | b@example.com | Bob Sample | Second answer from B
- 2017-04-12 17:35:20.284857 | c@example.com | Bob Sample | First answer from C
- 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