-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from skytable/0.8/dynlists
query, resp: Add dynamic list examples
- Loading branch information
Showing
8 changed files
with
142 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
assume the model is created using: | ||
> create space myapp | ||
> create model myapp.mydb(username: string, password: string, data: [string]) | ||
------------- | ||
This is an example just like `dynamic_lists_simple.rs` but using a struct instead | ||
*/ | ||
|
||
use skytable::{ | ||
query::{QList, SQParam}, | ||
response::{FromResponse, RList}, | ||
Config, | ||
}; | ||
|
||
#[derive(Debug, PartialEq)] | ||
struct User { | ||
username: String, | ||
password: String, | ||
data: Vec<String>, | ||
} | ||
|
||
impl User { | ||
fn new(username: String, password: String, data: Vec<String>) -> Self { | ||
Self { | ||
username, | ||
password, | ||
data, | ||
} | ||
} | ||
} | ||
|
||
impl SQParam for User { | ||
fn append_param(&self, q: &mut Vec<u8>) -> usize { | ||
self.username.append_param(q) | ||
+ self.password.append_param(q) | ||
+ QList::new(&self.data).append_param(q) | ||
} | ||
} | ||
|
||
impl FromResponse for User { | ||
fn from_response(resp: skytable::response::Response) -> skytable::ClientResult<Self> { | ||
let (username, password, data) = resp.parse::<(_, _, RList<String>)>()?; | ||
Ok(Self::new(username, password, data.into_values())) | ||
} | ||
} | ||
|
||
fn get_data_from_api() -> User { | ||
User { | ||
username: "sayan".to_owned(), | ||
password: "ulw06afuMCAg+1gh2lh1Y9xTIr/dUv2vqGLeZ39cVrE=".to_owned(), | ||
data: vec![ | ||
"stuff".to_owned(), | ||
"from".to_owned(), | ||
"the".to_owned(), | ||
"api".to_owned(), | ||
], | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut db = Config::new_default("root", "password12345678") | ||
.connect() | ||
.unwrap(); | ||
let data_from_api = get_data_from_api(); | ||
db.query_parse::<()>(&skytable::query!( | ||
"insert into myapp.mydb { username: ?, password: ?, data: ? }", | ||
&data_from_api | ||
)) | ||
.unwrap(); | ||
let fetched_user: User = db | ||
.query_parse(&skytable::query!( | ||
"select * from myapp.mydb where username = ?", | ||
"sayan" | ||
)) | ||
.unwrap(); | ||
assert_eq!(data_from_api, fetched_user); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
assume the model is created using: | ||
> create space myapp | ||
> create model myapp.mydb(username: string, password: string, data: [string]) | ||
*/ | ||
|
||
use skytable::{query::QList, response::RList, Config}; | ||
|
||
fn get_list_data_from_api() -> Vec<String> { | ||
vec![ | ||
"stuff".to_owned(), | ||
"from".to_owned(), | ||
"the".to_owned(), | ||
"api".to_owned(), | ||
] | ||
} | ||
|
||
fn main() { | ||
let mut db = Config::new_default("root", "password12345678") | ||
.connect() | ||
.unwrap(); | ||
let data_from_api = get_list_data_from_api(); | ||
let q = skytable::query!( | ||
"insert into myapp.mydb { username: ?, password: ?, data: ? }", | ||
"sayan", | ||
"ulw06afuMCAg+1gh2lh1Y9xTIr/dUv2vqGLeZ39cVrE=", | ||
QList::new(&data_from_api) | ||
); | ||
db.query_parse::<()>(&q).unwrap(); // expect this data to be inserted correctly | ||
// now fetch this data | ||
let (username, password, data): (String, String, RList<String>) = db | ||
.query_parse(&skytable::query!( | ||
"select * from myapp.mydb where username = ?", | ||
"sayan" | ||
)) | ||
.unwrap(); | ||
assert_eq!(username, "sayan"); | ||
assert_eq!(password, "ulw06afuMCAg+1gh2lh1Y9xTIr/dUv2vqGLeZ39cVrE="); | ||
assert_eq!(data.into_values(), data_from_api); | ||
} |
This file contains 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
This file contains 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
This file contains 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