Crates.io | db-derive-impl |
lib.rs | db-derive-impl |
version | 0.1.8 |
source | src |
created_at | 2019-11-13 21:36:00.940905 |
updated_at | 2019-12-27 23:37:14.87956 |
description | Derive proc-macro for db-derive |
homepage | |
repository | https://gitlab.com/Txuritan/db-derive |
max_upload_size | |
id | 181054 |
size | 34,139 |
db-derive is a wrapper around PostgreSQL and SQLite using r2d2, postgres and rusqlite, made for the stry story hosting server.
MSRV: 1.40.0
Until db-derive gets out of v0.1.x
the api is subject to change. This can and will happen.
#[derive(db_derive::Table)]
#[table(exists, schema)]
struct Tag {
#[table(rename = "Id", primary, length = 10)]
id: String,
#[table(rename = "Name", unique, length = 256)]
name: Option<String>,
}
fn main() -> Result<(), db_derive::Error> {
let pool = Pool::sqlite("example.db")?;
pool.transaction(|mut trans| {
trans.schema::<Tag>()?;
Ok(())
})?;
Ok(())
}
#[derive(db_derive::Execute)]
#[execute(sql = "INSERT INTO Tag (Id, Name) VALUES ($id, $name);")]
struct AddTag<'a> {
id: &'a str,
name: Option<&'a str>,
}
fn main() -> Result<(), db_derive::Error> {
let pool = Pool::sqlite("example.db")?;
pool.transaction(|trans| {
let add = AddTag {
id: "example",
name: Some("Example"),
};
add.execute(trans)?;
Ok(())
})?;
Ok(())
}
#[derive(db_derive::Query)]
#[query(sql = "SELECT Id, Name FROM Tag WHERE Id = $id;")]
struct GetTagById<'a> {
id: &'a str,
}
fn main() -> Result<(), db_derive::Error> {
let pool = Pool::sqlite("example.db")?;
let get = GetTagById { id: "example" };
let tag = get.query_row::<_, Tag>(&pool)?;
println!("Tag Name: {}", tag.name);
Ok(())
}
#[derive(db_derive::Execute)]
#[execute(sql = "SELECT Id, Name FROM Tag WHERE Id = $id;")]
struct GetTagById<'a> {
id: &'a str,
}
fn main() -> Result<(), db_derive::Error> {
let pool = Pool::sqlite("example.db")?;
let get = GetTagById { id: "example" };
let tag = get.query_row::<_, Tag>(&pool)?;
println!("Tag Name: {}", tag.name);
Ok(())
}
#[derive(db_derive::Kind)]
enum State {
#[kind(rename = "running")]
Running,
#[kind(rename = "stopped")]
Stopped,
}