Created
December 29, 2016 18:55
-
-
Save AlexPikalov/2a7a26af6ef2732c5cc99bfed5dcb766 to your computer and use it in GitHub Desktop.
CDRS query and map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// include AsRust and IntoRustByName traits to provide an ability to represent | |
// column values as simple Rust values. | |
use cdrs::types::{AsRust, IntoRustByName}; | |
struct Author { | |
first_name: String, | |
second_name: String | |
} | |
struct Message { | |
author: Author, | |
text: String, | |
tags: Vec<String> | |
} | |
// ... establish session | |
// ... query all messages | |
// get frame's body | |
let result_frame_body = res_frame.get_body(); | |
// then convert a body into Vec<Row> | |
let rows = result_frame_body.into_rows().unwrap(); | |
let messages: Vec<Message> = rows | |
.iter() | |
.map(|row| { | |
// get author as cdrs::types::udt::UDT | |
let author_udt = row.get_by_name("author").unwrap(); | |
// and construct Author struct from the UDT | |
let author = Author { | |
first_name: author_udt.get_by_name("first_name").unwrap(), | |
second_name: author_udt.get_by_name("second_name").unwrap() | |
}; | |
// get text as String | |
let text: String = row.get_by_name("text").unwrap(); | |
// get tags as cdrs::types::list::List and then convert it into Vec<String> | |
let tags: Vec<String> = row.get_by_name("tags").as_rust().unwrap(); | |
return Message { | |
author: author, | |
text: text, | |
tags: tags | |
}; | |
}) | |
.collect(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment