Skip to content

Commit e6ffb26

Browse files
committed
refactor(odbc): remove run_blocking helper and directly use spawn_blocking
This commit eliminates the run_blocking function from the ODBC module, replacing its usage with the spawn_blocking function directly in the OdbcConnection implementation. This change simplifies the code and enhances clarity in handling blocking tasks.
1 parent b7277b0 commit e6ffb26

File tree

4 files changed

+20
-30
lines changed

4 files changed

+20
-30
lines changed

sqlx-core/src/odbc/blocking.rs

Lines changed: 0 additions & 19 deletions
This file was deleted.

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
use crate::connection::Connection;
22
use crate::error::Error;
3-
use crate::odbc::blocking::run_blocking;
43
use crate::odbc::{
54
Odbc, OdbcArguments, OdbcColumn, OdbcConnectOptions, OdbcQueryResult, OdbcRow, OdbcTypeInfo,
65
};
76
use crate::transaction::Transaction;
87
use either::Either;
8+
use sqlx_rt::spawn_blocking;
99
mod odbc_bridge;
10+
use crate::odbc::{OdbcStatement, OdbcStatementMetadata};
1011
use futures_core::future::BoxFuture;
1112
use futures_util::future;
1213
use odbc_api::ConnectionTransitions;
13-
use odbc_bridge::{establish_connection, execute_sql};
14-
// no direct spawn_blocking here; use run_blocking helper
15-
use crate::odbc::{OdbcStatement, OdbcStatementMetadata};
1614
use odbc_api::{handles::StatementConnection, Prepared, ResultSetMetadata, SharedConnection};
15+
use odbc_bridge::{establish_connection, execute_sql};
1716
use std::borrow::Cow;
1817
use std::collections::HashMap;
1918
use std::sync::{Arc, Mutex};
@@ -70,7 +69,7 @@ impl OdbcConnection {
7069
S: std::fmt::Display + Send + 'static,
7170
{
7271
let conn = Arc::clone(&self.conn);
73-
run_blocking(move || {
72+
spawn_blocking(move || {
7473
let mut conn_guard = conn.lock().map_err(|_| {
7574
Error::Protocol(format!("ODBC {}: failed to lock connection", operation))
7675
})?;
@@ -80,7 +79,7 @@ impl OdbcConnection {
8079
}
8180

8281
pub(crate) async fn establish(options: &OdbcConnectOptions) -> Result<Self, Error> {
83-
let shared_conn = run_blocking({
82+
let shared_conn = spawn_blocking({
8483
let options = options.clone();
8584
move || {
8685
let conn = establish_connection(&options)?;
@@ -174,13 +173,13 @@ impl OdbcConnection {
174173
let conn = Arc::clone(&self.conn);
175174
let sql_arc = Arc::from(sql.to_string());
176175
let sql_clone = Arc::clone(&sql_arc);
177-
let (prepared, metadata) = run_blocking(move || {
176+
let (prepared, metadata) = spawn_blocking(move || {
178177
let mut prepared = conn.into_prepared(&sql_clone)?;
179178
let metadata = OdbcStatementMetadata {
180179
columns: collect_columns(&mut prepared),
181180
parameters: usize::from(prepared.num_params().unwrap_or(0)),
182181
};
183-
Ok((prepared, metadata))
182+
Ok::<_, Error>((prepared, metadata))
184183
})
185184
.await?;
186185
self.stmt_cache

sqlx-core/src/odbc/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
use crate::executor::Executor;
2525

2626
mod arguments;
27-
mod blocking;
2827
mod column;
2928
mod connection;
3029
mod database;

sqlx-rt/src/rt_tokio.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
pub use tokio::{
22
self, fs, io::AsyncRead, io::AsyncReadExt, io::AsyncWrite, io::AsyncWriteExt, io::ReadBuf,
3-
net::TcpStream, runtime::Handle, sync::Mutex as AsyncMutex, task::spawn, task::spawn_blocking,
4-
task::yield_now, time::sleep, time::timeout,
3+
net::TcpStream, runtime::Handle, sync::Mutex as AsyncMutex, task::spawn, task::yield_now,
4+
time::sleep, time::timeout,
55
};
66

77
#[cfg(unix)]
@@ -45,3 +45,14 @@ pub fn test_block_on<F: std::future::Future>(future: F) -> F::Output {
4545
.expect("failed to initialize Tokio test runtime")
4646
.block_on(future)
4747
}
48+
49+
/// Spawn a blocking task. Panics if the task panics.
50+
pub async fn spawn_blocking<F, R>(f: F) -> R
51+
where
52+
F: FnOnce() -> R + Send + 'static,
53+
R: Send + 'static,
54+
{
55+
tokio::task::spawn_blocking(f)
56+
.await
57+
.expect("blocking task panicked")
58+
}

0 commit comments

Comments
 (0)