db-derive-impl

Crates.iodb-derive-impl
lib.rsdb-derive-impl
version0.1.8
sourcesrc
created_at2019-11-13 21:36:00.940905
updated_at2019-12-27 23:37:14.87956
descriptionDerive proc-macro for db-derive
homepage
repositoryhttps://gitlab.com/Txuritan/db-derive
max_upload_size
id181054
size34,139
Ian Cronkright (Txuritan)

documentation

https://docs.rs/db-derive

README

db-derive

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

WARNING

Until db-derive gets out of v0.1.x the api is subject to change. This can and will happen.

Examples

Schema

#[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(())
}

Execute

#[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(())
}

Query

#[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(())
}

Tables

#[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(())
}

Types

#[derive(db_derive::Kind)]
enum State {
    #[kind(rename = "running")]
    Running,

    #[kind(rename = "stopped")]
    Stopped,
}
Commit count: 21

cargo fmt