typed-sql icon indicating copy to clipboard operation
typed-sql copied to clipboard

Add `RETURNING` clause support.

Open npatsakula opened this issue 4 years ago • 0 comments

To insert the increasing IDs can be useful to be able to get the result of the insertion.

This is a very crude example, but I would like to have an interface similar to:

#[derive(Default, Debug, typed_sql::Table)]
struct SomeStruct {
    // It can be represent like NULL if it None.
    // ..or it can just be ignored.
    id: Option<u64>,

    some_useful_data: u128,
}

let insert = SomeStruct::table()
    .insert(SomeStruct::default())
    .returns(|some_struct| some_struct.id)
    .to_sql();

assert_eq!(
    insert,
    "INSERT into somestruct(some_useful_data) VALUES (0) RETURNING id;"
);

npatsakula avatar Jun 01 '21 16:06 npatsakula