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 a SQLite database
23    #[cfg(feature = "rusqlite")]
24    Rusqlite(crate::driver::rusqlite::RusqliteExecResult),
25    /// Holds the result of executing an operation on the Mock database
26    #[cfg(feature = "mock")]
27    Mock(crate::MockExecResult),
28    /// Holds the result of executing an operation on the Proxy database
29    #[cfg(feature = "proxy")]
30    Proxy(crate::ProxyExecResult),
31}
32
33// ExecResult //
34
35impl ExecResult {
36    /// Get the last id after `AUTOINCREMENT` is done on the primary key
37    ///
38    /// # Panics
39    ///
40    /// Postgres does not support retrieving last insert id this way except through `RETURNING` clause
41    pub fn last_insert_id(&self) -> u64 {
42        match &self.result {
43            #[cfg(feature = "sqlx-mysql")]
44            ExecResultHolder::SqlxMySql(result) => result.last_insert_id(),
45            #[cfg(feature = "sqlx-postgres")]
46            ExecResultHolder::SqlxPostgres(_) => {
47                panic!("Should not retrieve last_insert_id this way")
48            }
49            #[cfg(feature = "sqlx-sqlite")]
50            ExecResultHolder::SqlxSqlite(result) => {
51                let last_insert_rowid = result.last_insert_rowid();
52                if last_insert_rowid < 0 {
53                    unreachable!("negative last_insert_rowid")
54                } else {
55                    last_insert_rowid as u64
56                }
57            }
58            #[cfg(feature = "rusqlite")]
59            ExecResultHolder::Rusqlite(result) => {
60                let last_insert_rowid = result.last_insert_rowid;
61                if last_insert_rowid < 0 {
62                    unreachable!("negative last_insert_rowid")
63                } else {
64                    last_insert_rowid as u64
65                }
66            }
67            #[cfg(feature = "mock")]
68            ExecResultHolder::Mock(result) => result.last_insert_id,
69            #[cfg(feature = "proxy")]
70            ExecResultHolder::Proxy(result) => result.last_insert_id,
71            #[allow(unreachable_patterns)]
72            _ => unreachable!(),
73        }
74    }
75
76    /// Get the number of rows affected by the operation
77    pub fn rows_affected(&self) -> u64 {
78        match &self.result {
79            #[cfg(feature = "sqlx-mysql")]
80            ExecResultHolder::SqlxMySql(result) => result.rows_affected(),
81            #[cfg(feature = "sqlx-postgres")]
82            ExecResultHolder::SqlxPostgres(result) => result.rows_affected(),
83            #[cfg(feature = "sqlx-sqlite")]
84            ExecResultHolder::SqlxSqlite(result) => result.rows_affected(),
85            #[cfg(feature = "rusqlite")]
86            ExecResultHolder::Rusqlite(result) => result.rows_affected,
87            #[cfg(feature = "mock")]
88            ExecResultHolder::Mock(result) => result.rows_affected,
89            #[cfg(feature = "proxy")]
90            ExecResultHolder::Proxy(result) => result.rows_affected,
91            #[allow(unreachable_patterns)]
92            _ => unreachable!(),
93        }
94    }
95}