sea_orm/executor/
execute.rs

1/// Defines the result of executing an operation
2#[derive(Debug)]
3pub struct ExecResult {
4    /// The type of result from the execution depending on the feature flag enabled
5    /// to choose a database backend
6    pub(crate) result: ExecResultHolder,
7}
8
9/// Holds a result depending on the database backend chosen by the feature flag
10#[allow(clippy::enum_variant_names)]
11#[derive(Debug)]
12pub(crate) enum ExecResultHolder {
13    /// Holds the result of executing an operation on a MySQL database
14    #[cfg(feature = "sqlx-mysql")]
15    SqlxMySql(sqlx::mysql::MySqlQueryResult),
16    /// Holds the result of executing an operation on a PostgreSQL database
17    #[cfg(feature = "sqlx-postgres")]
18    SqlxPostgres(sqlx::postgres::PgQueryResult),
19    /// Holds the result of executing an operation on a SQLite database
20    #[cfg(feature = "sqlx-sqlite")]
21    SqlxSqlite(sqlx::sqlite::SqliteQueryResult),
22    /// Holds the result of executing an operation on the Mock database
23    #[cfg(feature = "mock")]
24    Mock(crate::MockExecResult),
25    /// Holds the result of executing an operation on the Proxy database
26    #[cfg(feature = "proxy")]
27    Proxy(crate::ProxyExecResult),
28}
29
30// ExecResult //
31
32impl ExecResult {
33    /// Get the last id after `AUTOINCREMENT` is done on the primary key
34    ///
35    /// # Panics
36    ///
37    /// Postgres does not support retrieving last insert id this way except through `RETURNING` clause
38    pub fn last_insert_id(&self) -> u64 {
39        match &self.result {
40            #[cfg(feature = "sqlx-mysql")]
41            ExecResultHolder::SqlxMySql(result) => result.last_insert_id(),
42            #[cfg(feature = "sqlx-postgres")]
43            ExecResultHolder::SqlxPostgres(_) => {
44                panic!("Should not retrieve last_insert_id this way")
45            }
46            #[cfg(feature = "sqlx-sqlite")]
47            ExecResultHolder::SqlxSqlite(result) => {
48                let last_insert_rowid = result.last_insert_rowid();
49                if last_insert_rowid < 0 {
50                    unreachable!("negative last_insert_rowid")
51                } else {
52                    last_insert_rowid as u64
53                }
54            }
55            #[cfg(feature = "mock")]
56            ExecResultHolder::Mock(result) => result.last_insert_id,
57            #[cfg(feature = "proxy")]
58            ExecResultHolder::Proxy(result) => result.last_insert_id,
59            #[allow(unreachable_patterns)]
60            _ => unreachable!(),
61        }
62    }
63
64    /// Get the number of rows affected by the operation
65    pub fn rows_affected(&self) -> u64 {
66        match &self.result {
67            #[cfg(feature = "sqlx-mysql")]
68            ExecResultHolder::SqlxMySql(result) => result.rows_affected(),
69            #[cfg(feature = "sqlx-postgres")]
70            ExecResultHolder::SqlxPostgres(result) => result.rows_affected(),
71            #[cfg(feature = "sqlx-sqlite")]
72            ExecResultHolder::SqlxSqlite(result) => result.rows_affected(),
73            #[cfg(feature = "mock")]
74            ExecResultHolder::Mock(result) => result.rows_affected,
75            #[cfg(feature = "proxy")]
76            ExecResultHolder::Proxy(result) => result.rows_affected,
77            #[allow(unreachable_patterns)]
78            _ => unreachable!(),
79        }
80    }
81}