Skip to content

Commit

Permalink
Documentation improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Werner Braun committed Jun 4, 2024
1 parent 91618b3 commit 6f34574
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org).

## Unreleased

## 0.1.2 - 2024-06-04

- Documentation improvements

## 0.1.1 - 2024-06-03

- small description change
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wb_sqlite"
version = "0.1.1"
version = "0.1.2"
authors = ["Werner Braun <[email protected]>"]
edition = "2021"
#rust-version = "1.78"
Expand Down
63 changes: 34 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,40 @@ All derived items are saved to `target/generated/wb_sqlite` thanks to [virtue](h

### Create table
```rust
# use wb_sqlite::CreateTableSql;
#[derive(CreateTableSql)]
#[derive(wb_sqlite::CreateTableSql)]
struct Dog {
name: String,
}
assert_eq!(
Dog::CREATE_TABLE_SQL,
"CREATE TABLE IF NOT EXISTS dog (name TEXT NOT NULL) STRICT;"
);
# assert!(rusqlite::Connection::open_in_memory().unwrap().execute_batch(Dog::CREATE_TABLE_SQL).is_ok())
assert!(rusqlite::Connection::open_in_memory().unwrap()
.execute_batch(Dog::CREATE_TABLE_SQL).is_ok())
```

column constraint
```rust
# use wb_sqlite::CreateTableSql;
#[derive(CreateTableSql)]
#[derive(wb_sqlite::CreateTableSql)]
struct Cat {
#[sql(constraint = "UNIQUE")]
name: String,
weight: Option<f64>
}
assert_eq!(
Cat::CREATE_TABLE_SQL,
"CREATE TABLE IF NOT EXISTS cat (name TEXT NOT NULL UNIQUE, weight REAL) STRICT;"
concat!(
"CREATE TABLE IF NOT EXISTS cat ",
"(name TEXT NOT NULL UNIQUE, weight REAL) STRICT;"
)
);
# assert!(rusqlite::Connection::open_in_memory().unwrap().execute_batch(Cat::CREATE_TABLE_SQL).is_ok())
assert!(rusqlite::Connection::open_in_memory().unwrap()
.execute_batch(Cat::CREATE_TABLE_SQL).is_ok())
```

column datatype override
```rust
# use wb_sqlite::CreateTableSql;
#[derive(CreateTableSql)]
#[derive(wb_sqlite::CreateTableSql)]
struct Human {
#[sql(constraint = "PRIMARY KEY")]
id: i64,
Expand All @@ -63,17 +65,19 @@ struct Human {
assert_eq!(
Human::CREATE_TABLE_SQL,
concat!(
"CREATE TABLE IF NOT EXISTS human (id INTEGER NOT NULL PRIMARY KEY, ",
"name TEXT NOT NULL CHECK(name != ''), image BLOB, data ANY) STRICT;"
"CREATE TABLE IF NOT EXISTS human (",
"id INTEGER NOT NULL PRIMARY KEY, ",
"name TEXT NOT NULL CHECK(name != ''), ",
"image BLOB, data ANY) STRICT;"
)
);
# assert!(rusqlite::Connection::open_in_memory().unwrap().execute_batch(Human::CREATE_TABLE_SQL).is_ok())
assert!(rusqlite::Connection::open_in_memory().unwrap()
.execute_batch(Human::CREATE_TABLE_SQL).is_ok())
```

table constraint
```rust
# use wb_sqlite::CreateTableSql;
#[derive(CreateTableSql)]
#[derive(wb_sqlite::CreateTableSql)]
#[sql(constraint = "UNIQUE(owner,name)")]
struct Pet {
#[sql(constraint = "PRIMARY KEY")]
Expand All @@ -90,15 +94,16 @@ assert_eq!(
"name TEXT NOT NULL, UNIQUE(owner,name)) STRICT;"
)
);
# assert!(rusqlite::Connection::open_in_memory().unwrap().execute_batch(Pet::CREATE_TABLE_SQL).is_ok())
assert!(rusqlite::Connection::open_in_memory().unwrap()
.execute_batch(Pet::CREATE_TABLE_SQL).is_ok())
```


### Insert + Update

sync rusqlite
```rust
# use wb_sqlite::{CreateTableSql,InsertSync,UpdateSync};
use wb_sqlite::{CreateTableSql,InsertSync,UpdateSync};
#[derive(CreateTableSql,InsertSync,UpdateSync)]
struct Person {
#[sql(constraint = "PRIMARY KEY")]
Expand All @@ -116,13 +121,13 @@ fn main() -> Result<(), rusqlite::Error> {
name: "me".to_owned(),
};
let id = p.insert_sync(&conn)?;
assert!(id > 0);
assert!(id == 1);

let p2 = Person {
id: id,
let p = Person {
id: 1,
name: "you".to_owned()
};
let ok = p2.update_sync(&conn)?;
let ok = p.update_sync(&conn)?;
assert!(ok);

Ok(())
Expand All @@ -131,7 +136,7 @@ fn main() -> Result<(), rusqlite::Error> {

async sqlx
```rust
# use wb_sqlite::{CreateTableSql,Get,Insert,Update};
use wb_sqlite::{CreateTableSql,Get,Insert,Update};
#[derive(CreateTableSql,Get,Insert,Update,sqlx::FromRow)]
struct Person {
#[sql(constraint = "PRIMARY KEY")]
Expand All @@ -151,20 +156,20 @@ async fn main() -> Result<(), sqlx::Error> {
name: "me".to_owned(),
};
let id = p.insert(&mut conn).await?;
assert!(id > 0);
assert!(id == 1);

let p2 = Person {
id: id,
let p = Person {
id: 1,
name: "you".to_owned()
};
let ok = p2.update(&mut conn).await?;
let ok = p.update(&mut conn).await?;
assert!(ok);

let p3 = Person::get_by_id(1,&mut conn).await?;
assert_eq!(p3.name,"you");
let p = Person::get_by_id(1,&mut conn).await?;
assert_eq!(p.name,"you");

let p4 = Person::get_by_name("you",&mut conn).await?;
assert_eq!(p4.id,1);
let p = Person::get_by_name("you",&mut conn).await?;
assert_eq!(p.id,1);

Ok(())
}
Expand Down

0 comments on commit 6f34574

Please sign in to comment.