79722126

Date: 2025-08-01 06:24:10
Score: 0.5
Natty:
Report link

This is not an answer to the question, yet a hint as to how to solve the original problem which triggered it. I.e. how to get a neat iterator over rows of a query result set. Disclaimer I am the author of odbc-api and a main contributer to the odbc crate, back then it had been maintained.

I would suggest not going with odbc_iter. It is based on the no longer maintained odbc crate. Its iterator interface may be what you want, yet it actually translates it naively to a row wise fetch in the background which in most cases triggers and individual round-trip to the database for each row. It does this, because the odbc crate never supported bulk fetch.

For a fast bulk fetch with minimal IO overhead, and a type-safe fetching of row fields, I would suggest using odbc-apis RowVec together with the Fetch derive macro. For this the derive feature has to be active.

use odbc_api_derive::Fetch;
use odbc_api::{Connection, Error, Cursor, parameter::VarCharArray, buffers::RowVec};

#[derive(Default, Clone, Copy, Fetch)]
struct Person {
    first_name: VarCharArray<255>,
    last_name: VarCharArray<255>,
}

fn send_greetings(conn: &mut Connection) -> Result<(), Error> {
    let max_rows_in_batch = 250;
    let buffer = RowVec::<Person>::new(max_rows_in_batch);
    let mut cursor = conn.execute("SELECT first_name, last_name FROM Persons", (), None)?
        .expect("SELECT must yield a result set");
    let mut block_cursor = cursor.bind_buffer(buffer)?;

    while let Some(batch) = block_cursor.fetch()? {
        for person in batch.iter() {
            let first = person.first_name.as_str()
                .expect("First name must be UTF-8")
                .expect("First Name must not be NULL");
            let last = person.last_name.as_str()
                .expect("Last name must be UTF-8")
                .expect("Last Name must not be NULL");
            println!("Hello {first} {last}!")
        }
    }
    Ok(())
}

See: https://docs.rs/odbc-api/latest/odbc_api/derive.Fetch.html

Reasons:
  • Blacklisted phrase (1): not an answer
  • Blacklisted phrase (1): how to solve
  • Long answer (-1):
  • Has code block (-0.5):
Posted by: Markus Klein