sea-orm-ffi 0.1.3

Compatibility layer for Sea-ORM when crossing a Rust-to-Rust FFI boundary
Documentation
use sea_orm::{DatabaseBackend, ExecResult, ProxyExecResult};
use std::panic::catch_unwind;

#[repr(C)]
pub(crate) struct FfiExecResult {
	last_insert_id: u64,
	rows_affected: u64
}

impl FfiExecResult {
	/// We need to special-case postgres because postgres is stupid and panic's when
	/// calling [`last_insert_id()`](ExecResult::last_insert_id).
	pub(crate) fn new(backend: DatabaseBackend, exec_result: ExecResult) -> Self {
		// exec_result.last_insert_id() can panic, we'd rather have an Option
		let last_insert_id = match backend {
			DatabaseBackend::Postgres => None,
			_ => catch_unwind(|| exec_result.last_insert_id()).ok()
		};

		Self {
			last_insert_id: last_insert_id.unwrap_or(0xBAD_C0DE),
			rows_affected: exec_result.rows_affected()
		}
	}
}

impl From<FfiExecResult> for ExecResult {
	fn from(exec_result: FfiExecResult) -> Self {
		ProxyExecResult::new(exec_result.last_insert_id, exec_result.rows_affected).into()
	}
}