Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion mssql-odbc/src/api/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::api::odbc_types::{
SQL_ERROR, SQL_INVALID_HANDLE, SQL_NO_DATA, SQL_SUCCESS, SQL_SUCCESS_WITH_INFO, SqlHandle,
SqlReturn,
};
use crate::api::util::resolve_cursor_poll;
use crate::error::free_errors;
use crate::handles::stmt::STMT_STATE_CURSOR_OPEN;
use crate::handles::{HandleType, StmtHandle, handle_from_raw};
Expand Down Expand Up @@ -135,7 +136,10 @@ fn fetch_rows_next(statement_handle: SqlHandle, stmt: &StmtHandle) -> SqlReturn
// `drain_active_row` in tds_client), so a failure here may originate from
// the prior row rather than the new one. Columns of the new row are pulled
// lazily by subsequent SQLGetData calls via `read_row_column`.
let fetch_result = dbc.runtime.block_on(client.next_row_cursor());
let cursor_poll = client.try_next_row_cursor();
let fetch_result = resolve_cursor_poll(cursor_poll, || {
dbc.runtime.block_on(client.next_row_cursor())
});

match fetch_result {
Ok(true) => {
Expand Down
7 changes: 5 additions & 2 deletions mssql-odbc/src/api/get_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use super::odbc_types::{
};
use super::sqlstate::*;
use crate::api::odbc_types::SqlWChar;
use crate::api::util::{copy_with_nul, write_if_some};
use crate::api::util::{copy_with_nul, resolve_cursor_poll, write_if_some};
use crate::error::{free_errors, post_sql_error};
use crate::handles::stmt::{ActivePlpStream, STMT_STATE_CURSOR_OPEN};
use crate::handles::{HandleType, StmtHandle, handle_from_raw};
Expand Down Expand Up @@ -427,7 +427,10 @@ fn resume_row_to_column(
};

let target = column_number - 1; // 0-based
let cursor_result = dbc.runtime.block_on(client.read_row_column(target));
let cursor_poll = client.try_read_row_column(target);
let cursor_result = resolve_cursor_poll(cursor_poll, || {
dbc.runtime.block_on(client.read_row_column(target))
});

let Ok(mut dbc_state) = dbc.inner.lock() else {
error!("SQLGetData: dbc mutex poisoned after row resume");
Expand Down
52 changes: 51 additions & 1 deletion mssql-odbc/src/api/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
use std::slice;

use crate::api::odbc_types::{SQL_NTS, SqlSmallInt, SqlWChar};
use mssql_tds::connection::tds_client::CursorPoll;

pub(crate) fn resolve_cursor_poll<T, E>(
poll: Result<CursorPoll<T>, E>,
fallback: impl FnOnce() -> Result<T, E>,
) -> Result<T, E> {
match poll? {
CursorPoll::Ready(value) => Ok(value),
CursorPoll::Pending => fallback(),
}
}

/// Write `value` to `ptr` if non-null. Every ODBC out-parameter pointer may
/// legitimately be null (caller opting out of that value), so the
Expand Down Expand Up @@ -231,8 +242,47 @@ pub(crate) fn rewrite_param_markers(sql: &str) -> (String, usize) {

#[cfg(test)]
mod tests {
use super::{copy_with_nul, read_utf16, rewrite_param_markers, write_if_some};
use super::{
copy_with_nul, read_utf16, resolve_cursor_poll, rewrite_param_markers, write_if_some,
};
use crate::api::odbc_types::{SQL_NTS, SqlWChar};
use mssql_tds::connection::tds_client::CursorPoll;

#[test]
fn cursor_ready_skips_fallback() {
let mut calls = 0;
let value = resolve_cursor_poll(Ok::<_, ()>(CursorPoll::Ready(7)), || {
calls += 1;
Ok(9)
});

assert_eq!(value, Ok(7));
assert_eq!(calls, 0);
}

#[test]
fn cursor_pending_calls_fallback_once() {
let mut calls = 0;
let value = resolve_cursor_poll(Ok::<_, ()>(CursorPoll::Pending), || {
calls += 1;
Ok(9)
});

assert_eq!(value, Ok(9));
assert_eq!(calls, 1);
}

#[test]
fn cursor_error_skips_fallback() {
let mut calls = 0;
let value = resolve_cursor_poll::<u8, _>(Err("cursor failed"), || {
calls += 1;
Ok(9)
});

assert_eq!(value, Err("cursor failed"));
assert_eq!(calls, 0);
}

#[test]
fn rewrite_no_markers_is_unchanged() {
Expand Down