Skip to main content

with_connection

Function with_connection 

Source
pub async fn with_connection<T, F>(
    db_path: Option<String>,
    f: F,
) -> Result<T, ToolError>
where T: Send + 'static, F: FnOnce(&Connection) -> Result<T, SqliteToolError> + Send + 'static,
Expand description

Executes a closure with a database connection in a blocking task.

This helper abstracts the common pattern of:

  1. Spawning a blocking task for SQLite operations
  2. Acquiring a connection from the manager
  3. Locking the connection mutex
  4. Mapping errors to ToolError

§Example

let tables = with_connection(input.db_path, |conn| {
    let mut stmt = conn.prepare("SELECT name FROM sqlite_master")?;
    // ... use the connection
    Ok(result)
}).await?;