Skip to content

Commit

Permalink
query drivers and dsn with an immutable reference
Browse files Browse the repository at this point in the history
  • Loading branch information
pacman82 committed Apr 2, 2021
1 parent c470bea commit 454c968
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
6 changes: 3 additions & 3 deletions odbc-api/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl Environment {
///
/// # Ok::<_, odbc_api::Error>(())
/// ```
pub fn drivers(&mut self) -> Result<Vec<DriverInfo>, Error> {
pub fn drivers(&self) -> Result<Vec<DriverInfo>, Error> {
// Find required buffer size to avoid truncation.

// Start with first so we are independent of state
Expand Down Expand Up @@ -235,7 +235,7 @@ impl Environment {
///
/// # Ok::<_, odbc_api::Error>(())
/// ```
pub fn data_sources(&mut self) -> Result<Vec<DataSourceInfo>, Error> {
pub fn data_sources(&self) -> Result<Vec<DataSourceInfo>, Error> {
self.data_sources_impl(FetchOrientation::First)
}

Expand Down Expand Up @@ -276,7 +276,7 @@ impl Environment {
}

fn data_sources_impl(
&mut self,
&self,
direction: FetchOrientation,
) -> Result<Vec<DataSourceInfo>, Error> {
// Find required buffer size to avoid truncation.
Expand Down
8 changes: 4 additions & 4 deletions odbc-api/src/handles/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl Environment {
///
/// [1]: https://docs.microsoft.com/sql/odbc/reference/syntax/sqldrivers-function
pub fn drivers_buffer_fill(
&mut self,
&self,
direction: FetchOrientation,
buffer_description: &mut Vec<u16>,
buffer_attributes: &mut Vec<u16>,
Expand Down Expand Up @@ -175,7 +175,7 @@ impl Environment {
///
/// [1]: https://docs.microsoft.com/sql/odbc/reference/syntax/sqldrivers-function
pub fn drivers_buffer_len(
&mut self,
&self,
direction: FetchOrientation,
) -> Result<Option<(i16, i16)>, Error> {
// Lengths in characters minus terminating zero
Expand Down Expand Up @@ -214,7 +214,7 @@ impl Environment {
///
/// `(server name length, description length)`. Length is in characters minus terminating zero.
pub fn data_source_buffer_len(
&mut self,
&self,
direction: FetchOrientation,
) -> Result<Option<(i16, i16)>, Error> {
// Lengths in characters minus terminating zero
Expand Down Expand Up @@ -254,7 +254,7 @@ impl Environment {
///
/// Use [`Environment::data_source_buffer_len`] to determine buffer lengths.
pub fn data_source_buffer_fill(
&mut self,
&self,
direction: FetchOrientation,
buffer_name: &mut Vec<u16>,
buffer_description: &mut Vec<u16>,
Expand Down
22 changes: 22 additions & 0 deletions odbc-api/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2091,6 +2091,28 @@ fn arbitrary_input_parameters(profile: &Profile) {
assert_eq!("Hello, World!,42", actual)
}

#[test]
fn synchronized_access_to_driver_and_data_source_info() {
let expected_drivers = ENV.drivers().unwrap();
let expected_data_sources = ENV.data_sources().unwrap();

const NUM_THREADS: usize = 100;
let handles = (0..NUM_THREADS).map(|_| {
let expected_drivers = expected_drivers.clone();
let expected_data_sources = expected_data_sources.clone();
thread::spawn(move || {
let drivers = ENV.drivers().unwrap();
assert_eq!(expected_drivers, drivers);
let data_sources_for_thread = ENV.data_sources().unwrap();
assert_eq!(expected_data_sources, data_sources_for_thread);
})
});

for handle in handles {
handle.join().unwrap();
}
}

/// This test is inspired by a bug caused from a fetch statement generating a lot of diagnostic
/// messages.
#[test]
Expand Down
2 changes: 1 addition & 1 deletion odbcsv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn main() -> Result<(), Error> {
.init()?;

// We know this is going to be the only ODBC environment in the entire process, so this is safe.
let mut environment = unsafe { Environment::new() }?;
let environment = unsafe { Environment::new() }?;

match opt.command {
Command::Query { query_opt } => {
Expand Down

0 comments on commit 454c968

Please sign in to comment.