4个版本
0.1.3 | 2019年5月22日 |
---|---|
0.1.2 | 2019年5月22日 |
0.1.1 | 2019年5月5日 |
0.1.0 | 2019年4月29日 |
在#generate-table中排名15
32KB
711 行
impl_table: 生成的数据库绑定和工具
![impl_table on docs.rs][docsrs-image] [docsrs-image]: https://docs.rs/chrono/badge.svg
示例
extern crate chrono;
use chrono::{DateTime, NaiveDate, TimeZone, Utc};
use impl_table::{impl_table, Table};
// Optionally generate an id column and two timestamp columns: created_at and
// updated_at.
#[impl_table(name = "books", adaptor = rusqlite, with_columns(id, timestamps))]
#[derive(Table)]
struct Book {
#[column] pub name: String,
#[column] published_at: NaiveDate,
#[column(name = "author_name")] author: String,
}
let book = Book {
id: 1,
name: "The Man in the High Castle".into(),
published_at: NaiveDate::from_ymd(1962, 10, 1),
author: "Philip K. Dick".into(),
created_at: Utc.ymd(2019, 5, 22).and_hms(8, 0, 0),
updated_at: Utc.ymd(2019, 5, 22).and_hms(8, 0, 0),
};
上面的代码生成了以下类似的实现
extern crate chrono;
use chrono::{DateTime, NaiveDate, TimeZone, Utc};
struct Book {
id: i64,
pub name: String,
published_at: NaiveDate,
author: i64,
created_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
}
impl Book {
pub const TABLE_NAME: &'static str = "books";
pub const ADAPTOR_NAME: &'static str = "rusqlite";
fn table_name() -> &'static str {
Self::TABLE_NAME
}
fn all_columns() -> &'static [&'static str] {
&["id", "name", "published_at", "author_name", "created_at", "updated_at"]
}
fn from_row(row: &rusqlite::Row) -> rusqlite::Result<Self> {
Ok(Self {
id: row.get(0)?,
name: row.get(1)?,
published_at: row.get(2)?,
author: row.get(3)?,
created_at: row.get(4)?,
updated_at: row.get(5)?,
})
}
}
更多示例请参阅 test/sample.rs
。
依赖项
~2MB
~46K SLoC