Skip to content

Commit bb735db

Browse files
committed
Add derive support
1 parent df099a1 commit bb735db

File tree

6 files changed

+104
-26
lines changed

6 files changed

+104
-26
lines changed

CHANGELOG.md

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

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

5+
### 0.8.2
6+
7+
Support deriving queries and responses.
8+
59
### 0.8.1
610

711
Fixed issues with documentation

Cargo.toml

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

1414
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1515
[dependencies]
16+
# internal deps
17+
sky-derive = "0.2.0"
18+
# external deps
1619
tokio = { version = "1.34.0", features = ["full"] }
1720
native-tls = "0.2.11"
1821
tokio-native-tls = "0.3.1"

README.md

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,9 @@
22

33
## Introduction
44

5-
This library is the official client for the free and open-source NoSQL database
6-
[Skytable](https://github.com/skytable/skytable). First, go ahead and install Skytable by
7-
following the instructions [here](https://docs.skytable.io/getting-started). This library supports
8-
all Skytable versions that work with the [Skyhash 2 Protocol](https://docs.skytable.io/protocol/overview).
9-
This version of the library was tested with the latest Skytable release
10-
(release [0.8.0-beta](https://github.com/skytable/skytable/releases/v0.8.0-beta)).
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.0-beta](https://github.com/skytable/skytable/releases/v0.8.0-beta)).
116

12-
## Features
13-
14-
- Sync API
15-
- Async API
16-
- TLS in both sync/async APIs
17-
- Connection pooling for sync/async
18-
- Use both sync/async APIs at the same time
19-
- Always up-to-date
20-
21-
## Using this library
7+
## Definitive example
228

239
This library only ships with the bare minimum that is required for interacting with Skytable. Once you have
2410
Skytable installed and running, you're ready to follow this guide!
@@ -41,22 +27,45 @@ skytable = "0.8"
4127
You're ready to go!
4228

4329
```rust
44-
use skytable::{Config, query};
30+
use skytable::{Query, Response, Config, query};
31+
32+
#[derive(Query, Response)]
33+
#[derive(Clone, PartialEq, Debug)] // we just do these for the assert (they are not needed)
34+
struct User {
35+
userid: String,
36+
pass: String,
37+
followers: u64,
38+
email: Option<String>
39+
}
40+
41+
let our_user = User { userid: "user".into(), pass: "pass".into(), followers: 120, email: None };
4542

4643
let mut db = Config::new_default("username", "password").connect().unwrap();
47-
let query = query!("select username, password from myspace.mytbl WHERE username = ?", your_fn_to_get_user());
48-
let (username, password) = db.query_parse::<(String, String)>(&query).unwrap();
44+
45+
// insert data
46+
let q = query!("insert into myspace.mymodel(?, ?, ?, ?)", our_user.clone());
47+
db.query_parse::<()>(&q).unwrap();
48+
49+
// select data
50+
let user: User = db.query_parse(&query!("select * from myspace.mymodel where username = ?", &our_user.userid)).unwrap();
51+
assert_eq!(user, our_user);
4952
```
5053

5154
> **Read [docs here to learn BlueQL](https://docs.skytable.io/)**
5255
56+
## Features
57+
58+
- Sync API
59+
- Async API
60+
- TLS in both sync/async APIs
61+
- Connection pooling for sync/async
62+
- Use both sync/async APIs at the same time
63+
- Always up-to-date
64+
5365
## Contributing
5466

55-
Open-source, and contributions ... &mdash; they're always welcome! For ideas and suggestions,
56-
[create an issue on GitHub](https://github.com/skytable/client-rust/issues/new) and for patches,
57-
fork and open those pull requests [here](https://github.com/skytable/client-rust)!
67+
Open-source, and contributions ... &mdash; they're always welcome! For ideas and suggestions, [create an issue on GitHub](https://github.com/skytable/client-rust/issues/new) and for patches, fork and open those pull requests [here](https://github.com/skytable/client-rust)!
5868

5969
## License
6070

61-
This client library is distributed under the permissive
62-
[Apache-2.0 License](https://github.com/skytable/client-rust/blob/next/LICENSE). Now go build great apps!
71+
This client library is distributed under the permissive [Apache-2.0 License](https://github.com/skytable/client-rust/blob/next/LICENSE). Now go build great apps!

src/lib.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,34 @@
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+
//! ## Definitive example
25+
//!
26+
//! ```no_run
27+
//! use skytable::{Query, Response, Config, query};
28+
//!
29+
//! #[derive(Query, Response)]
30+
//! #[derive(Clone, PartialEq, Debug)] // we just do these for the assert (they are not needed)
31+
//! struct User {
32+
//! userid: String,
33+
//! pass: String,
34+
//! followers: u64,
35+
//! email: Option<String>
36+
//! }
37+
//!
38+
//! let our_user = User { userid: "user".into(), pass: "pass".into(), followers: 120, email: None };
39+
//!
40+
//! let mut db = Config::new_default("username", "password").connect().unwrap();
41+
//!
42+
//! // insert data
43+
//! let q = query!("insert into myspace.mymodel(?, ?, ?, ?)", our_user.clone());
44+
//! db.query_parse::<()>(&q).unwrap();
45+
//!
46+
//! // select data
47+
//! let user: User = db.query_parse(&query!("select * from myspace.mymodel where username = ?", &our_user.userid)).unwrap();
48+
//! assert_eq!(user, our_user);
49+
//!
50+
//! ```
51+
//!
2452
//! ## Getting started
2553
//!
2654
//! Make sure you have Skytable set up. If not, follow the [official installation guide here](https://docs.skytable.io/installation)
@@ -65,6 +93,9 @@
6593
//!
6694
//! ## Going advanced
6795
//!
96+
//! You can use the [`macro@Query`] and [`macro@Response`] derive macros to directly pass complex types as parameters
97+
//! and read as responses. This should cover most of the general use-cases (otherwise you can manually implement them).
98+
//!
6899
//! - Custom [`mod@query`] generation
69100
//! - Custom [`response`] parsing
70101
//! - [`Connection pooling`](pool)
@@ -88,6 +119,10 @@ pub mod query;
88119
pub mod response;
89120
pub mod syncio;
90121
// re-exports
122+
/// The `Query` derive macro enables you to directly pass complex types as parameters into queries
123+
pub use sky_derive::Query;
124+
/// The `Response` derive macro enables you to directly pass complex types as parameters into queries
125+
pub use sky_derive::Response;
91126
pub use {
92127
aio::{ConnectionAsync, ConnectionTlsAsync},
93128
config::Config,

src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
#[macro_export]
18-
/// This macro can be used to create a [`Query`](crate::Query), almost like a variadic function
18+
/// This macro can be used to create a [`Query`](struct@crate::Query), almost like a variadic function
1919
///
2020
/// ## Examples
2121
/// ```

tests/derive.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use skytable::{query, Query, Response};
2+
3+
#[derive(Query, Response)]
4+
struct User {
5+
username: String,
6+
password: String,
7+
email: Option<String>,
8+
}
9+
10+
impl User {
11+
fn new(username: String, password: String, email: Option<String>) -> Self {
12+
Self {
13+
username,
14+
password,
15+
email,
16+
}
17+
}
18+
}
19+
20+
#[test]
21+
fn test_params() {
22+
let q = query!(
23+
"insert into myspace.mymodel(?, ?, ?)",
24+
User::new("sayan".into(), "pass".into(), None)
25+
);
26+
assert_eq!(q.param_cnt(), 3);
27+
}

0 commit comments

Comments
 (0)