Skip to content

Instantly share code, notes, and snippets.

@felipesere
Created April 13, 2017 11:34
Show Gist options
  • Save felipesere/959192229791f27ca198f866b643b5c8 to your computer and use it in GitHub Desktop.
Save felipesere/959192229791f27ca198f866b643b5c8 to your computer and use it in GitHub Desktop.
Error I get when using raw sql
#[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: "[email protected]".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 | [email protected] | Bob Sample | First answer from A
2017-04-12 17:34:53.250539 | [email protected] | Bob Sample | Second answer from A
2017-04-12 17:35:04.718267 | [email protected] | Bob Sample | First answer from B
2017-04-12 17:35:04.718267 | [email protected] | Bob Sample | Second answer from B
2017-04-12 17:35:20.284857 | [email protected] | Bob Sample | First answer from C
2017-04-12 17:35:20.284857 | [email protected] | Bob Sample | Second answer from C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment