11 releases (5 breaking)
0.12.1 | Aug 26, 2024 |
---|---|
0.12.0 | Aug 26, 2024 |
0.11.3 | Jul 22, 2024 |
0.10.0 | Jun 9, 2024 |
0.7.0 | Feb 4, 2024 |
#1 in #accompany
58 downloads per month
Used in 3 crates
(via derive-sql)
29KB
429 lines
Procedural macro to automatically generate an Sqlable
trait for the
provided struct that is compatible with an MySQL database connected using the mysql
crate
https://crates.io/crates/mysql.
How to use
You write:
#[derive(DeriveMysql)]
pub struct Person {
name: String,
age: u32,
}
And you can use:
let pool = ::mysql::Pool::new("mysql://test@localhost/simpledb").unwrap();
let mut connection = pool.get_conn().unwrap();
let mut db: PersonMysql<_> = derive_sql::proxy::mysql::Conn::from(connection).into();
// initialise
db.create_table().unwrap();
// Insert entries
db.insert(Person {name: "Abi".to_string(), age: 31 }).unwrap();
db.insert(Person {name: "Bert".to_string(), age: 32 }).unwrap();
db.insert(Person {name: "Charlie".to_string(), age: 33 }).unwrap();
// Query
let persons: Vec<Person> = db.select(Box::new(SimpleFilter::try_from(("age", 32)).unwrap())).unwrap();
assert!(persons[0].name.eq("Bert"));
// Update
db.update(Box::new(SimpleFilter::try_from(("name", "Abi")).unwrap()), Person { name: "Abi".to_string(), age: 32 }).unwrap();
// Delete
db.delete(Box::new(SimpleFilter::try_from(("name", "Abi")).unwrap())).unwrap();
// Clear the table
db.delete_table().unwrap();
Container attributes:
#[derive_sqlite(ident = ...)]
overwrite the name of therusqlite
wrapper from{class}Mysql
;#[derive_sqlite(table_name = "...")]
specify the name of the table (default to the container name in lower case);
Field attributes:
#[derive_sqlite(is_primary_key = true)]
nominate that one of the field is a primary key. Only one primary key can be specified. primary key fields are unique in the table. Primary key can NOT be a String - the following will not compile:
#[derive(DeriveMysql)]
pub struct Person {
#[derive_sqlite(is_primary_key = true)]
name: String,
age: u32,
}
#[derive_sqlite(on_insert = ...)]
nominate a function of the typefn() -> {type}
with{type}
corresponding to the type of the field. The function is called when the item is inserted and the value returned by the function is assigned to the field before the item is inserted. Typical use is to assign a creation date.#[derive_sqlite(on_update = ...)]
nominate a function of the typefn() -> {type}
with{type}
corresponding to the type of the field. The function is called when the item is updated and the value returned by the function is assigned to the field before the item is updated. Typical use is to assign a last modified date.
Dependencies
~0.7–1.2MB
~24K SLoC