Skip to content

Instantly share code, notes, and snippets.

@wqfish
Created March 27, 2020 20:43
Show Gist options
  • Save wqfish/c4a69477b90880c2c5731ce8f90d4747 to your computer and use it in GitHub Desktop.
Save wqfish/c4a69477b90880c2c5731ce8f90d4747 to your computer and use it in GitHub Desktop.
use mysql_async::prelude::*;
#[derive(Debug, PartialEq, Eq, Clone)]
struct MyStruct {
b: Vec<u8>,
c: Vec<u8>,
d: u64,
e: Vec<u8>,
}
fn p1() -> MyStruct {
MyStruct {
b: vec![
67, 48, 54, 54, 70, 57, 54, 54, 66, 48, 56, 54, 48, 48, 48, 48,
],
c: vec![
121, 57, 218, 152, 229, 36, 197, 249, 105, 252, 45, 232, 217, 5, 253, 149, 1, 235, 198,
242, 0, 1, 176, 169, 201, 65, 224, 190, 109, 80, 207, 68,
],
d: 0,
e: vec![
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
],
}
}
fn p2() -> MyStruct {
MyStruct {
b: vec![],
c: vec![
7, 99, 17, 223, 77, 64, 123, 8, 84, 55, 27, 161, 58, 95, 63, 177, 164, 85, 90, 194, 43,
54, 19, 117, 253, 71, 178, 99, 243, 24, 34, 242,
],
d: 0,
e: vec![
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
],
}
}
#[tokio::main]
async fn main() -> Result<(), mysql_async::error::Error> {
let mut builder = mysql_async::OptsBuilder::new();
builder
.ip_or_hostname("localhost")
.user(Some("wqfish"))
.pass(Some("password"));
let pool = mysql_async::Pool::new(builder);
let conn = pool.get_conn().await.unwrap();
let conn = conn.drop_query(r"USE mydb").await.unwrap();
let q = "SELECT b, c, d, e FROM my_table";
// let result = conn.query(q).await.unwrap();
let result = conn.prep_exec(q, ()).await.unwrap();
let (_ /* conn */, loaded_structs) = result
.map_and_drop(|row| {
let (b, c, d, e) = mysql_async::from_row(row);
MyStruct { b, c, d, e }
})
.await
.unwrap();
pool.disconnect().await.unwrap();
assert_eq!(loaded_structs[0], p1());
assert_eq!(loaded_structs[1], p2());
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment