6 releases
0.12.1 | Aug 26, 2024 |
---|---|
0.12.0 | Aug 26, 2024 |
0.11.3 | Jul 22, 2024 |
#87 in #statement
34 downloads per month
Used in 4 crates
(via derive-sql)
30KB
398 lines
Procedural macro to automatically generate SQL statements traits TableStatement
, SelectStatement
, InsertStatement
, DeleteStatement
for the
provided struct as well as conversion to SQL parameters and from SQL rows.
How to use
You write:
#[derive(derive_sql::DeriveSqlStatement)]
pub struct Person {
name: String,
age: u32,
}
And you can use:
use derive_sql::structs::*;
fn handle<Con, Row>(s: &mut Con)
where Con: traits::Connection<Row>,
Row: traits::Row,
{
use derive_sql::traits::{Table, SelectV2, Insert, Delete, Update};
let db = SqlPerson::default();
// initialise
db.create(s).unwrap();
// Insert entries
db.insert(s, &Person {name: "Abi".to_string(), age: 31 }).unwrap();
db.insert(s, &Person {name: "Bert".to_string(), age: 32 }).unwrap();
db.insert(s, &Person {name: "Charlie".to_string(), age: 33 }).unwrap();
// Query
let persons: Vec<Person> = db.select_with_filter(s, &Field::from("age").eq(32)).unwrap();
assert!(persons[0].name.eq("Bert"));
// Update
db.update_with_filter(s, &Field::from("name").eq("Abi"), &Person { name: "Abi".to_string(), age: 32 }).unwrap();
// Delete
db.delete_with_filter(s, &Field::from("name").eq("Abi")).unwrap();
// Clear the table
db.drop(s).unwrap();
}
let pool = ::mysql::Pool::new("mysql://test@localhost/simpledb").unwrap();
let mut connection = pool.get_conn().unwrap();
handle(&mut connection);
Container attributes:
#[derive_sqlite(ident = ...)]
overwrite the name of the wrapper fromSql{class}
;#[derive_sqlite(table_name = "...")]
specify the name of the table (default to the container name in lower case);#[derive_sqlite(read_only = true/false)]
specify whether to implement read/write (ie table, select, insert, update, delete, to params conversion and from row conversion) or read only statements (ie select and from row conversion)
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,
}
Dependencies
~0.7–1.2MB
~24K SLoC