Macro dysql::insert

source ·
insert!() { /* proc-macro */ }
Expand description

Insert data Note: if you use this macro under postgres database, you should add “returning id” at the end of sql statement by yourself.

§Examples

Basic usage:

let mut tran = get_transaction().await.unwrap();
let dto = UserDto{ id: Some(4), name: Some("lisi".to_owned()), age: Some(50) };
let last_insert_id = insert!(|&mut *tran, dto| -> (_, mysql) {
    r#"insert into test_user (id, name, age) values (4, 'aa', 1)"#  // works for mysql and sqlite
    // r#"insert into test_user (id, name, age) values (4, 'aa', 1) returning id"#  // works for postgres
}).unwrap();
assert_eq!(4, last_insert_id);
 
tran.rollback().await?;