|
| 1 | +/* |
| 2 | + assume the model is created using: |
| 3 | +
|
| 4 | + > create space myapp |
| 5 | + > create model myapp.mydb(username: string, password: string, data: [string]) |
| 6 | +
|
| 7 | + ------------- |
| 8 | +
|
| 9 | + This is an example just like `dynamic_lists_simple.rs` but using a struct instead |
| 10 | +*/ |
| 11 | + |
| 12 | +use skytable::{ |
| 13 | + query::{QList, SQParam}, |
| 14 | + response::{FromResponse, RList}, |
| 15 | + Config, |
| 16 | +}; |
| 17 | + |
| 18 | +#[derive(Debug, PartialEq)] |
| 19 | +struct User { |
| 20 | + username: String, |
| 21 | + password: String, |
| 22 | + data: Vec<String>, |
| 23 | +} |
| 24 | + |
| 25 | +impl User { |
| 26 | + fn new(username: String, password: String, data: Vec<String>) -> Self { |
| 27 | + Self { |
| 28 | + username, |
| 29 | + password, |
| 30 | + data, |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl SQParam for User { |
| 36 | + fn append_param(&self, q: &mut Vec<u8>) -> usize { |
| 37 | + self.username.append_param(q) |
| 38 | + + self.password.append_param(q) |
| 39 | + + QList::new(&self.data).append_param(q) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl FromResponse for User { |
| 44 | + fn from_response(resp: skytable::response::Response) -> skytable::ClientResult<Self> { |
| 45 | + let (username, password, data) = resp.parse::<(_, _, RList<String>)>()?; |
| 46 | + Ok(Self::new(username, password, data.into_values())) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +fn get_data_from_api() -> User { |
| 51 | + User { |
| 52 | + username: "sayan".to_owned(), |
| 53 | + password: "ulw06afuMCAg+1gh2lh1Y9xTIr/dUv2vqGLeZ39cVrE=".to_owned(), |
| 54 | + data: vec![ |
| 55 | + "stuff".to_owned(), |
| 56 | + "from".to_owned(), |
| 57 | + "the".to_owned(), |
| 58 | + "api".to_owned(), |
| 59 | + ], |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +fn main() { |
| 64 | + let mut db = Config::new_default("root", "password12345678") |
| 65 | + .connect() |
| 66 | + .unwrap(); |
| 67 | + let data_from_api = get_data_from_api(); |
| 68 | + db.query_parse::<()>(&skytable::query!( |
| 69 | + "insert into myapp.mydb { username: ?, password: ?, data: ? }", |
| 70 | + &data_from_api |
| 71 | + )) |
| 72 | + .unwrap(); |
| 73 | + let fetched_user: User = db |
| 74 | + .query_parse(&skytable::query!( |
| 75 | + "select * from myapp.mydb where username = ?", |
| 76 | + "sayan" |
| 77 | + )) |
| 78 | + .unwrap(); |
| 79 | + assert_eq!(data_from_api, fetched_user); |
| 80 | +} |
0 commit comments