From c196e7e2577af9d31351ec19680ca604d0f8dae3 Mon Sep 17 00:00:00 2001 From: Mike Cronce Date: Fri, 16 Aug 2024 11:09:41 -0400 Subject: [PATCH] diesel/src/mysql/connection/mod.rs: impl ConnectionWithReturningId for MysqlConnection --- diesel/src/mysql/connection/mod.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/diesel/src/mysql/connection/mod.rs b/diesel/src/mysql/connection/mod.rs index d43c21ad2642..c5e677558ba8 100644 --- a/diesel/src/mysql/connection/mod.rs +++ b/diesel/src/mysql/connection/mod.rs @@ -211,6 +211,34 @@ impl Connection for MysqlConnection { } } +impl ConnectionWithReturningId for MysqlConnection { + type ReturnedId = u64; + + fn execute_returning_id(&mut self, source: &T) -> QueryResult + where + T: QueryFragment + QueryId, + { + #[allow(unsafe_code)] // call to unsafe function + update_transaction_manager_status( + prepared_query( + &source, + &mut self.statement_cache, + &mut self.raw_connection, + &mut *self.instrumentation, + ) + .and_then(|stmt| { + // we have not called result yet, so calling `execute` is + // fine + let stmt_use = unsafe { stmt.execute() }?; + Ok(unsafe { stmt_use.insert_id() }) + }), + &mut self.transaction_state, + &mut self.instrumentation, + &crate::debug_query(source), + ) + } +} + #[inline(always)] fn update_transaction_manager_status( query_result: QueryResult,