Skip to content

Commit 6a51ba6

Browse files
authored
Merge pull request #33 from skytable/0.8/dynlists
query, resp: Add dynamic list examples
2 parents 75e1355 + 918d9b6 commit 6a51ba6

8 files changed

+142
-22
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
All changes in this project will be noted in this file.
44

5+
## 0.8.11
6+
7+
### Additions
8+
9+
- Added `QList` and `RList` for using dynamic lists
10+
- Allow using references for custom types in `query!` macro
11+
512
## 0.8.10
613

714
### Fixes

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ license = "Apache-2.0"
99
name = "skytable"
1010
readme = "README.md"
1111
repository = "https://github.com/skytable/client-rust"
12-
version = "0.8.10"
12+
version = "0.8.11"
1313

1414
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1515
[dependencies]

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
## Introduction
44

5-
This library is the official client for the free and open-source NoSQL database [Skytable](https://github.com/skytable/skytable). First, go ahead and install Skytable by following the instructions [here](https://docs.skytable.io/getting-started). This library supports all Skytable versions that work with the [Skyhash 2 Protocol](https://docs.skytable.io/protocol/overview). This version of the library was tested with the latest Skytable release (release [0.8.3](https://github.com/skytable/skytable/releases/v0.8.3)). [Read more about supported versions here](#version-support).
5+
This library is the official client for the free and open-source NoSQL database [Skytable](https://github.com/skytable/skytable). First, go ahead and install Skytable by following the instructions [here](https://docs.skytable.io/getting-started). This library supports all Skytable versions that work with the [Skyhash 2 Protocol](https://docs.skytable.io/protocol/overview). This version of the library was tested with the latest Skytable release (release [0.8.4](https://github.com/skytable/skytable/releases/v0.8.4)). [Read more about supported versions here](#version-support).
6+
7+
**📁 You can [find some usage examples in this folder here](/examples)**.
68

79
## Definitive example
810

examples/dynamic_lists_advanced.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}

examples/dynamic_lists_simple.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
use skytable::{query::QList, response::RList, Config};
9+
10+
fn get_list_data_from_api() -> Vec<String> {
11+
vec![
12+
"stuff".to_owned(),
13+
"from".to_owned(),
14+
"the".to_owned(),
15+
"api".to_owned(),
16+
]
17+
}
18+
19+
fn main() {
20+
let mut db = Config::new_default("root", "password12345678")
21+
.connect()
22+
.unwrap();
23+
let data_from_api = get_list_data_from_api();
24+
let q = skytable::query!(
25+
"insert into myapp.mydb { username: ?, password: ?, data: ? }",
26+
"sayan",
27+
"ulw06afuMCAg+1gh2lh1Y9xTIr/dUv2vqGLeZ39cVrE=",
28+
QList::new(&data_from_api)
29+
);
30+
db.query_parse::<()>(&q).unwrap(); // expect this data to be inserted correctly
31+
// now fetch this data
32+
let (username, password, data): (String, String, RList<String>) = db
33+
.query_parse(&skytable::query!(
34+
"select * from myapp.mydb where username = ?",
35+
"sayan"
36+
))
37+
.unwrap();
38+
assert_eq!(username, "sayan");
39+
assert_eq!(password, "ulw06afuMCAg+1gh2lh1Y9xTIr/dUv2vqGLeZ39cVrE=");
40+
assert_eq!(data.into_values(), data_from_api);
41+
}

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
//! The client-driver is distributed under the liberal [Apache-2.0 License](https://www.apache.org/licenses/LICENSE-2.0) and hence
2222
//! you can use it in your applications without any licensing issues.
2323
//!
24+
//! **📁 You can [find some usage examples in this folder here](https://github.com/skytable/client-rust/tree/v0.8.11/examples)**.
25+
//!
2426
//! ## Definitive example
2527
//!
2628
//! ```no_run
@@ -35,7 +37,7 @@
3537
//! userid: String,
3638
//! pass: String,
3739
//! followers: u64,
38-
//! email: Option<String>
40+
//! email: Option<String>,
3941
//! }
4042
//!
4143
//! let our_user = User { userid: "user".into(), pass: "pass".into(), followers: 120, email: None };

src/query.rs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,12 @@ where
290290
}
291291
}
292292

293+
impl<'a, T: SQParam> SQParam for &'a T {
294+
fn append_param(&self, q: &mut Vec<u8>) -> usize {
295+
T::append_param(self, q)
296+
}
297+
}
298+
293299
/// Use this when you need to use `null`
294300
pub struct Null;
295301
impl SQParam for Null {
@@ -350,14 +356,6 @@ impl<const N: usize> SQParam for [u8; N] {
350356
1
351357
}
352358
}
353-
impl<'a, const N: usize> SQParam for &'a [u8; N] {
354-
fn append_param(&self, buf: &mut Vec<u8>) -> usize {
355-
buf.push(5);
356-
pushlen!(buf, self.len());
357-
buf.extend(*self);
358-
1
359-
}
360-
}
361359
impl SQParam for Vec<u8> {
362360
fn append_param(&self, buf: &mut Vec<u8>) -> usize {
363361
buf.push(5);
@@ -366,11 +364,6 @@ impl SQParam for Vec<u8> {
366364
1
367365
}
368366
}
369-
impl<'a> SQParam for &'a Vec<u8> {
370-
fn append_param(&self, q: &mut Vec<u8>) -> usize {
371-
self.as_slice().append_param(q)
372-
}
373-
}
374367
// str
375368
impl<'a> SQParam for &'a str {
376369
fn append_param(&self, buf: &mut Vec<u8>) -> usize {
@@ -380,11 +373,6 @@ impl<'a> SQParam for &'a str {
380373
1
381374
}
382375
}
383-
impl<'a> SQParam for &'a String {
384-
fn append_param(&self, q: &mut Vec<u8>) -> usize {
385-
self.as_str().append_param(q)
386-
}
387-
}
388376
impl SQParam for String {
389377
fn append_param(&self, buf: &mut Vec<u8>) -> usize {
390378
self.as_str().append_param(buf)

src/response.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ impl<T: FromRow> Deref for Rows<T> {
414414

415415
/// A list received from a response
416416
#[derive(Debug, PartialEq, Clone)]
417-
pub struct RList<T: FromValue = Value>(Vec<T>);
417+
pub struct RList<T = Value>(Vec<T>);
418418

419419
impl<T: FromValue> From<Vec<T>> for RList<T> {
420420
fn from(values: Vec<T>) -> Self {

0 commit comments

Comments
 (0)