Skip to content

Commit

Permalink
strong exception safety for promote_to_cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
pacman82 committed Jun 26, 2024
1 parent dba2f72 commit 304c038
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 31 deletions.
36 changes: 18 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,8 @@ pub unsafe extern "C" fn arrow_odbc_reader_make(
let dbms_name = try_!(connection.0.database_management_system_name());
debug!("Database managment system name as reported by ODBC: {dbms_name}");

let maybe_cursor = try_!(connection.0.into_cursor(query, &parameters[..]));
let reader = if let Some(cursor) = maybe_cursor {
ArrowOdbcReader::new(cursor)
} else {
ArrowOdbcReader::empty()
};
let mut reader = ArrowOdbcReader::new(connection.0);
try_!(reader.promote_to_cursor(query, &parameters[..]));
*reader_out = Box::into_raw(Box::new(reader));
null_mut() // Ok(())
}
Expand Down
21 changes: 14 additions & 7 deletions src/reader/arrow_odbc_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ pub enum ArrowOdbcReader {
}

impl ArrowOdbcReader {
/// Creates a new reader in Cursor state.
pub fn new(cursor: CursorImpl<StatementConnection<'static>>) -> Self {
Self::Cursor(cursor)
/// Creates a new reader in Connection state.
pub fn new(connection: Connection<'static>) -> Self {
Self::Connection(connection)
}

pub fn empty() -> Self {
Expand Down Expand Up @@ -108,8 +108,8 @@ impl ArrowOdbcReader {
*self = ArrowOdbcReader::Connection(connection)
}

/// Promote Connection to cursor state. If this operation fails, the reader will be in empty
/// state.
/// Promote Connection to cursor state. If this operation fails, the reader will stay in
/// connection state.
pub fn promote_to_cursor(&mut self, query: &str, params: impl ParameterCollectionRef) -> Result<(), ArrowOdbcError> {
// Move self into a temporary instance we own, in order to take ownership of the inner
// reader and move it to a different state.
Expand All @@ -125,8 +125,15 @@ impl ArrowOdbcReader {
}
};

if let Some(cursor) = conn.into_cursor(query, params)? {
*self = ArrowOdbcReader::Cursor(cursor);
match conn.into_cursor(query, params) {
Ok(None) => (),
Ok(Some(cursor)) => {
*self = ArrowOdbcReader::Cursor(cursor);
},
Err(error) => {
*self = ArrowOdbcReader::Connection(error.connection);
return Err(error.error.into())
},
}
Ok(())
}
Expand Down

0 comments on commit 304c038

Please sign in to comment.