Skip to content

Commit

Permalink
refactor: remove dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
pacman82 committed Jun 29, 2024
1 parent 3274efe commit 30db68c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 78 deletions.
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ use arrow_odbc::odbc_api::{escape_attribute_value, Connection, ConnectionOptions
pub use error::{arrow_odbc_error_free, arrow_odbc_error_message, ArrowOdbcError};
use log::debug;
pub use logging::arrow_odbc_log_to_stderr;
pub use reader::{
arrow_odbc_reader_free, arrow_odbc_reader_make, arrow_odbc_reader_next, ArrowOdbcReader,
};
pub use reader::{arrow_odbc_reader_free, arrow_odbc_reader_next, ArrowOdbcReader};
pub use writer::{
arrow_odbc_writer_free, arrow_odbc_writer_make, arrow_odbc_writer_write_batch, ArrowOdbcWriter,
};
Expand Down Expand Up @@ -76,7 +74,10 @@ pub unsafe extern "C" fn arrow_odbc_connect_with_connection_string(

let connection = try_!(env.connect_with_connection_string(
&connection_string,
ConnectionOptions { login_timeout_sec, packet_size }
ConnectionOptions {
login_timeout_sec,
packet_size
}
));

// Log dbms name to ease debugging of issues.
Expand Down
62 changes: 0 additions & 62 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,73 +11,11 @@ use std::{

use arrow::ffi::{FFI_ArrowArray, FFI_ArrowSchema};
use arrow_odbc::OdbcReaderBuilder;
use log::debug;

use crate::{parameter::ArrowOdbcParameter, try_, ArrowOdbcError, OdbcConnection};

pub use self::arrow_odbc_reader::ArrowOdbcReader;

/// Creates an Arrow ODBC reader instance.
///
/// Takes ownership of connection even in case of an error.
///
/// # Safety
///
/// * `connection` must point to a valid OdbcConnection. This function takes ownership of the
/// connection, even in case of an error. So The connection must not be freed explicitly
/// afterwards.
/// * `query_buf` must point to a valid utf-8 string
/// * `query_len` describes the len of `query_buf` in bytes.
/// * `parameters` must contain only valid pointers. This function takes ownership of all of them
/// independent if the function succeeds or not. Yet it does not take ownership of the array
/// itself.
/// * `parameters_len` number of elements in parameters.
/// * `max_text_size` optional upper bound for the size of text columns. Use `0` to indicate that no
/// uppper bound applies.
/// * `max_binary_size` optional upper bound for the size of binary columns. Use `0` to indicate
/// that no uppper bound applies.
/// * `fallibale_allocations`: `TRUE` if allocations should return an error, `FALSE` if it is fine
/// to abort the process. Enabling might have a performance overhead, so it might be desirable to
/// disable it, if you know there is enough memory available.
/// * `schema`: Optional input arrow schema. NULL means no input schema is supplied. Should a
/// schema be supplied `schema` Rust will take ownership of it an the `schema` will be
/// overwritten with an empty one. This means the Python code, must only deallocate the memory
/// directly pointed to by `schema`, but not freeing the resources of the passed schema.
/// * `reader_out` in case of success this will point to an instance of `ArrowOdbcReader`.
/// Ownership is transferred to the caller.
#[no_mangle]
pub unsafe extern "C" fn arrow_odbc_reader_make(
connection: NonNull<OdbcConnection>,
query_buf: *const u8,
query_len: usize,
parameters: *const *mut ArrowOdbcParameter,
parameters_len: usize,
reader_out: *mut *mut ArrowOdbcReader,
) -> *mut ArrowOdbcError {
// Transtlate C Args into more idiomatic rust representations
let query = slice::from_raw_parts(query_buf, query_len);
let query = str::from_utf8(query).unwrap();

let connection = *Box::from_raw(connection.as_ptr());

let parameters = if parameters.is_null() {
Vec::new()
} else {
slice::from_raw_parts(parameters, parameters_len)
.iter()
.map(|&p| Box::from_raw(p).unwrap())
.collect()
};

// Use database managment system name to see if we need to apply workarounds
let dbms_name = try_!(connection.0.database_management_system_name());
debug!("Database managment system name as reported by ODBC: {dbms_name}");

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(())
}

/// Creates an Arrow ODBC reader instance.
///
Expand Down
23 changes: 11 additions & 12 deletions src/reader/arrow_odbc_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ pub enum ArrowOdbcReader {
}

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

pub fn empty() -> Self {
Self::Empty
}
Expand All @@ -59,7 +54,7 @@ impl ArrowOdbcReader {
&mut self,
) -> Result<Option<(FFI_ArrowArray, FFI_ArrowSchema)>, ArrowOdbcError> {
let next = match self {
ArrowOdbcReader::Empty | ArrowOdbcReader:: Connection(_)=> None,
ArrowOdbcReader::Empty | ArrowOdbcReader::Connection(_) => None,
ArrowOdbcReader::Cursor(_) => {
unreachable!("Python code must not allow to call next_batch from cursor state")
}
Expand Down Expand Up @@ -90,7 +85,7 @@ impl ArrowOdbcReader {
let cursor = match tmp_self {
// In case there has been a query without a result set, we could be in an empty state.
// Let's just keep it, there is simply nothing to bind a buffer to.
ArrowOdbcReader::Empty | ArrowOdbcReader::Connection(_)=> return Ok(()),
ArrowOdbcReader::Empty | ArrowOdbcReader::Connection(_) => return Ok(()),
ArrowOdbcReader::Cursor(cursor) => cursor,
ArrowOdbcReader::Reader(_) | ArrowOdbcReader::ConcurrentReader(_) => {
unreachable!("Python part must ensure to only promote cursors to readers.")
Expand All @@ -110,7 +105,11 @@ impl ArrowOdbcReader {

/// 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> {
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.
let mut tmp_self = ArrowOdbcReader::Empty;
Expand All @@ -129,11 +128,11 @@ impl ArrowOdbcReader {
Ok(None) => (),
Ok(Some(cursor)) => {
*self = ArrowOdbcReader::Cursor(cursor);
},
}
Err(error) => {
*self = ArrowOdbcReader::Connection(error.connection);
return Err(error.error.into())
},
return Err(error.error.into());
}
}
Ok(())
}
Expand Down Expand Up @@ -198,7 +197,7 @@ impl ArrowOdbcReader {

*self = match tmp_self {
// Nothing to do. There is nothing left to fetch.
reader@ (ArrowOdbcReader::Empty | ArrowOdbcReader::Connection(_))=> reader,
reader @ (ArrowOdbcReader::Empty | ArrowOdbcReader::Connection(_)) => reader,
ArrowOdbcReader::Cursor(_) => {
unreachable!("Python code must not allow to call into_concurrent from cursor state")
}
Expand Down

0 comments on commit 30db68c

Please sign in to comment.