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///
20/// # Examples
21///
22/// ```text
23/// LAST_INSERT_ID()
24/// ```
25#[derive(Clone, Debug, PartialEq, Default)]
26pub struct FuncLastInsertId;
27
28impl Expr {
29 /// Creates a `LAST_INSERT_ID()` function expression.
30 pub fn last_insert_id() -> Self {
31 FuncLastInsertId.into()
32 }
33}
34
35impl From<FuncLastInsertId> for Expr {
36 fn from(value: FuncLastInsertId) -> Self {
37 Expr::Func(value.into())
38 }
39}
40
41impl From<FuncLastInsertId> for ExprFunc {
42 fn from(value: FuncLastInsertId) -> Self {
43 ExprFunc::LastInsertId(value)
44 }
45}