Skip to main content

sea_orm/executor/
execute.rs

1/// Result of a non-`SELECT` statement: an `INSERT`, `UPDATE`, `DELETE`, or
2/// DDL execution. Carries the row count
3/// ([`rows_affected`](Self::rows_affected)) and, where the backend supports
4/// it, the last auto-generated primary key
5/// ([`last_insert_id`](Self::last_insert_id)).
6#[derive(Debug)]
7pub struct ExecResult {
8    pub(crate) result: ExecResultHolder,
9}
10
11/// Holds a result depending on the database backend chosen by the feature flag
12#[allow(clippy::enum_variant_names)]
13#[derive(Debug)]
14pub(crate) enum ExecResultHolder {
15    /// Holds the result of executing an operation on a MySQL database
16    #[cfg(feature = "sqlx-mysql")]
17    SqlxMySql(sqlx::mysql::MySqlQueryResult),
18    /// Holds the result of executing an operation on a PostgreSQL database
19    #[cfg(feature = "sqlx-postgres")]
20    SqlxPostgres(sqlx::postgres::PgQueryResult),
21    /// Holds the result of executing an operation on a SQLite database
22    #[cfg(feature = "sqlx-sqlite")]
23    SqlxSqlite(sqlx::sqlite::SqliteQueryResult),
24    /// Holds the result of executing an operation on a SQLite database
25    #[cfg(feature = "rusqlite")]
26    Rusqlite(crate::driver::rusqlite::RusqliteExecResult),
27    /// Holds the result of executing an operation on the Mock database
28    #[cfg(feature = "mock")]
29    Mock(crate::MockExecResult),
30    /// Holds the result of executing an operation on the Proxy database
31    #[cfg(feature = "proxy")]
32    Proxy(crate::ProxyExecResult),
33}
34
35// ExecResult //
36
37impl ExecResult {
38    /// The auto-increment primary key value assigned by the database on the
39    /// most recent `INSERT`.
40    ///
41    /// # Panics
42    ///
43    /// PostgreSQL does not expose `last_insert_id` directly — use
44    /// `exec_with_returning` / `exec_with_returning_keys` instead.
45    pub fn last_insert_id(&self) -> u64 {
46        match &self.result {
47            #[cfg(feature = "sqlx-mysql")]
48            ExecResultHolder::SqlxMySql(result) => result.last_insert_id(),
49            #[cfg(feature = "sqlx-postgres")]
50            ExecResultHolder::SqlxPostgres(_) => {
51                panic!("Should not retrieve last_insert_id this way")
52            }
53            #[cfg(feature = "sqlx-sqlite")]
54            ExecResultHolder::SqlxSqlite(result) => {
55                let last_insert_rowid = result.last_insert_rowid();
56                if last_insert_rowid < 0 {
57                    unreachable!("negative last_insert_rowid")
58                } else {
59                    last_insert_rowid as u64
60                }
61            }
62            #[cfg(feature = "rusqlite")]
63            ExecResultHolder::Rusqlite(result) => {
64                let last_insert_rowid = result.last_insert_rowid;
65                if last_insert_rowid < 0 {
66                    unreachable!("negative last_insert_rowid")
67                } else {
68                    last_insert_rowid as u64
69                }
70            }
71            #[cfg(feature = "mock")]
72            ExecResultHolder::Mock(result) => result.last_insert_id,
73            #[cfg(feature = "proxy")]
74            ExecResultHolder::Proxy(result) => result.last_insert_id,
75            #[allow(unreachable_patterns)]
76            _ => unreachable!(),
77        }
78    }
79
80    /// Number of rows affected by the statement.
81    pub fn rows_affected(&self) -> u64 {
82        match &self.result {
83            #[cfg(feature = "sqlx-mysql")]
84            ExecResultHolder::SqlxMySql(result) => result.rows_affected(),
85            #[cfg(feature = "sqlx-postgres")]
86            ExecResultHolder::SqlxPostgres(result) => result.rows_affected(),
87            #[cfg(feature = "sqlx-sqlite")]
88            ExecResultHolder::SqlxSqlite(result) => result.rows_affected(),
89            #[cfg(feature = "rusqlite")]
90            ExecResultHolder::Rusqlite(result) => result.rows_affected,
91            #[cfg(feature = "mock")]
92            ExecResultHolder::Mock(result) => result.rows_affected,
93            #[cfg(feature = "proxy")]
94            ExecResultHolder::Proxy(result) => result.rows_affected,
95            #[allow(unreachable_patterns)]
96            _ => unreachable!(),
97        }
98    }
99}