toasty_core/stmt/
func_last_insert_id.rs

1use crate::stmt::{Expr, ExprFunc};
2
3/// The `LAST_INSERT_ID()` function expression (MySQL-specific).
4///
5/// Returns the first automatically generated value that was set for an AUTO_INCREMENT
6/// column by the most recent INSERT statement. This is primarily used to retrieve
7/// auto-increment IDs after insertion on MySQL, which doesn't support RETURNING clauses.
8///
9/// # Behavior
10///
11/// - Returns the first auto-increment ID from the most recent INSERT
12/// - When multiple rows are inserted, returns the ID of the first row
13/// - Subsequent row IDs can be calculated by adding row offsets (first_id + 1, first_id + 2, etc.)
14/// - Returns 0 if no AUTO_INCREMENT value was generated
15///
16/// # MySQL Documentation
17///
18/// See: https://dev.mysql.com/doc/refman/8.0/en/information-functions.html#function_last-insert-id
19#[derive(Clone, Debug, PartialEq, Default)]
20pub struct FuncLastInsertId;
21
22impl Expr {
23    pub fn last_insert_id() -> Self {
24        FuncLastInsertId.into()
25    }
26}
27
28impl From<FuncLastInsertId> for Expr {
29    fn from(value: FuncLastInsertId) -> Self {
30        Expr::Func(value.into())
31    }
32}
33
34impl From<FuncLastInsertId> for ExprFunc {
35    fn from(value: FuncLastInsertId) -> Self {
36        ExprFunc::LastInsertId(value)
37    }
38}