Skip to content

Commit 9e71ab3

Browse files
cursoragentlovasoa
andcommitted
Refactor: Use with_conn_map for blocking ODBC operations
Co-authored-by: contact <[email protected]>
1 parent c2fefbc commit 9e71ab3

File tree

1 file changed

+24
-20
lines changed
  • sqlx-core/src/odbc/connection

1 file changed

+24
-20
lines changed

sqlx-core/src/odbc/connection/mod.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,18 @@ impl OdbcConnection {
3939
}
4040

4141
#[inline]
42-
fn odbc_err<T, E: std::fmt::Display>(res: Result<T, E>, ctx: &'static str) -> Result<T, Error> {
43-
res.map_err(|e| Error::Protocol(format!("{}: {}", ctx, e)))
42+
async fn with_conn_map<T, E, F>(&self, ctx: &'static str, f: F) -> Result<T, Error>
43+
where
44+
T: Send + 'static,
45+
E: std::fmt::Display,
46+
F: FnOnce(&mut OdbcConn) -> Result<T, E> + Send + 'static,
47+
{
48+
let inner = self.inner.clone();
49+
run_blocking(move || {
50+
let mut conn = inner.lock().unwrap();
51+
f(&mut conn).map_err(|e| Error::Protocol(format!("{}: {}", ctx, e)))
52+
})
53+
.await
4454
}
4555

4656
pub(crate) async fn establish(options: &OdbcConnectOptions) -> Result<Self, Error> {
@@ -59,44 +69,38 @@ impl OdbcConnection {
5969
/// Returns the name of the actual Database Management System (DBMS) this
6070
/// connection is talking to as reported by the ODBC driver.
6171
pub async fn dbms_name(&mut self) -> Result<String, Error> {
62-
self.with_conn(|conn| {
63-
Self::odbc_err(
64-
conn.database_management_system_name(),
65-
"Failed to get DBMS name",
66-
)
72+
self.with_conn_map::<_, _, _>("Failed to get DBMS name", |conn| {
73+
conn.database_management_system_name()
6774
})
6875
.await
6976
}
7077

7178
pub(crate) async fn ping_blocking(&mut self) -> Result<(), Error> {
72-
self.with_conn(|conn| {
73-
Self::odbc_err(conn.execute("SELECT 1", (), None), "Ping failed")?;
74-
Ok(())
79+
self.with_conn_map::<_, _, _>("Ping failed", |conn| {
80+
conn.execute("SELECT 1", (), None).map(|_| ())
7581
})
7682
.await
7783
}
7884

7985
pub(crate) async fn begin_blocking(&mut self) -> Result<(), Error> {
80-
self.with_conn(|conn| {
81-
Self::odbc_err(conn.set_autocommit(false), "Failed to begin transaction")
86+
self.with_conn_map::<_, _, _>("Failed to begin transaction", |conn| {
87+
conn.set_autocommit(false)
8288
})
8389
.await
8490
}
8591

8692
pub(crate) async fn commit_blocking(&mut self) -> Result<(), Error> {
87-
self.with_conn(|conn| {
88-
Self::odbc_err(conn.commit(), "Failed to commit transaction")?;
89-
Self::odbc_err(conn.set_autocommit(true), "Failed to commit transaction")?;
90-
Ok(())
93+
self.with_conn_map::<_, _, _>("Failed to commit transaction", |conn| {
94+
conn.commit()?;
95+
conn.set_autocommit(true)
9196
})
9297
.await
9398
}
9499

95100
pub(crate) async fn rollback_blocking(&mut self) -> Result<(), Error> {
96-
self.with_conn(|conn| {
97-
Self::odbc_err(conn.rollback(), "Failed to rollback transaction")?;
98-
Self::odbc_err(conn.set_autocommit(true), "Failed to rollback transaction")?;
99-
Ok(())
101+
self.with_conn_map::<_, _, _>("Failed to rollback transaction", |conn| {
102+
conn.rollback()?;
103+
conn.set_autocommit(true)
100104
})
101105
.await
102106
}

0 commit comments

Comments
 (0)