Environment

Struct Environment 

Source
pub struct Environment<V: VersionOption> { /* private fields */ }
Expand description

An Environment is a global context, in which to access data.

Associated with an Environment is any information that is global in nature, such as:

  • The Environment’s state
  • The current environment-level diagnostics
  • The handles of connections currently allocated on the environment
  • The current stetting of each environment attribute

See: [Environment Handles in the ODBC Reference][1] [1]: https://docs.microsoft.com/sql/odbc/reference/develop-app/environment-handles

Implementations§

Source§

impl<V: VersionOption> Environment<V>

Source

pub fn as_raw(&self) -> SQLHENV

Provides access to the raw ODBC environment handle.

Source§

impl<V: Version> Environment<V>

Source

pub fn data_sources( &mut self, direction: FetchOrientation, server_name: &mut [u8], description: &mut [u8], ) -> ReturnOption<(SQLSMALLINT, SQLSMALLINT)>

Fills buffers with information about the available datasources

A 32 / 64 Bit Application will only return information about either 32 or 64 Bit DataSources.

§Returns

(server_name_length, description_length)

See [SQLDataSources][1] [1]: https://docs.microsoft.com/sql/odbc/reference/syntax/sqldatasources-function

Examples found in repository?
examples/list_datasources.rs (line 20)
8fn main() {
9
10    let env = Environment::new().unwrap();
11    let mut env = env.declare_version_3().unwrap();
12
13    let mut server_name = [0; 512];
14    let mut description = [0; 512];
15
16    println!("ODBC Data Sources:");
17
18    loop {
19        let (name_length, description_length) =
20            match env.data_sources(SQL_FETCH_NEXT, &mut server_name, &mut description) {
21                ReturnOption::Success(v) => v,
22                ReturnOption::Info(_) => panic!("Buffers not large enough. Truncation occurred."),
23                ReturnOption::NoData(()) => break,
24                ReturnOption::Error(()) => {
25                    panic!("Error occurred. Could use diagnostics to learn more")
26                }
27            };
28
29        println!(
30            "\tName: {}\n\tDescription: {}\n",
31            from_utf8(&server_name[..(name_length as usize)]).unwrap(),
32            from_utf8(&description[..(description_length as usize)]).unwrap()
33        );
34    }
35}
Source

pub fn drivers( &mut self, direction: FetchOrientation, description: &mut [u8], attributes: &mut [u8], ) -> ReturnOption<(SQLSMALLINT, SQLSMALLINT)>

Fills buffers with information about the available datasources

A 32 / 64 Bit Application will only return information about either 32 or 64 Bit DataSources.

§Returns

(description_length, attributes_length)

See [SQLDrivers][1] [1]: https://docs.microsoft.com/sql/odbc/reference/syntax/sqldrivers-function

Examples found in repository?
examples/list_drivers.rs (line 20)
8fn main() {
9
10    let env = Environment::new().unwrap();
11    let mut env = env.declare_version_3().unwrap();
12
13    let mut description = [0; 512];
14    let mut attributes = [0; 512];
15
16    println!("ODBC Drivers:");
17
18    loop {
19        let (description_length, attributes_length) =
20            match env.drivers(SQL_FETCH_NEXT, &mut description, &mut attributes) {
21                ReturnOption::Success(v) => v,
22                ReturnOption::Info(_) => panic!("Buffers not large enough. Truncation occurred."),
23                ReturnOption::NoData(()) => break,
24                ReturnOption::Error(()) => {
25                    panic!("Error occurred. Could use diagnostics to learn more")
26                }
27            };
28
29        println!(
30            "\tDescription: {}\n\tAttributes: {}\n",
31            from_utf8(&description[..(description_length as usize)]).unwrap(),
32            from_utf8(&attributes[..(attributes_length as usize)]).unwrap()
33        );
34    }
35}
Source§

impl Environment<NoVersion>

Source

pub fn new() -> Return<Self>

Allocates a new Environment

Examples found in repository?
examples/bind_columns.rs (line 47)
45fn main() {
46
47    let env = Environment::new().unwrap();
48    let env = env.declare_version_3().unwrap();
49
50    match run(&env) {
51        Ok(()) => (),
52        Err(LastError(message)) => println!("An error occurred: {}", message),
53    }
54}
More examples
Hide additional examples
examples/print_table.rs (line 47)
45fn main() {
46
47    let env = Environment::new().unwrap();
48    let env = env.declare_version_3().unwrap();
49
50    match run(&env) {
51        Ok(()) => (),
52        Err(LastError(message)) => println!("An error occurred: {}", message),
53    }
54}
examples/prepared_query.rs (line 8)
6fn main() {
7
8    let env = Environment::new().unwrap();
9    let env = env.declare_version_3().unwrap();
10    let conn = connect(&env);
11    let mut stmt = prepare_query(&conn);
12    for &year in [1968, 1993].iter() {
13        let result_set = execute_query(stmt, year);
14        stmt = print_fields(result_set);
15        println!("");
16    }
17}
examples/affected_row_count.rs (line 7)
6fn main() {
7    let env = Environment::new().unwrap();
8    let env = env.declare_version_3().unwrap();
9    let conn = DataSource::with_parent(&env).unwrap();
10    let conn = conn.connect("TestDataSource", "", "").unwrap();
11    exec(&conn, "INSERT INTO movies (title, year) VALUES ('TEST movie', 9999), ('TEST movie', 9998)");
12    exec(&conn, "DELETE FROM movies WHERE title = 'TEST movie'");
13}
examples/transactions.rs (line 6)
5fn main() {
6    let env = Environment::new().unwrap();
7    let env = env.declare_version_3().unwrap();
8
9    let ds = DataSource::with_parent(&env).unwrap();
10    let conn = ds.connect("TestDataSource", "", "").unwrap();
11    let mut conn = conn.disable_autocommit().unwrap();
12
13    {
14        //Any statement now will start transaction which could be ended with conn.commit() or conn.rollback()
15        //If either commit or rollback was not called before connection drop automatic rollback will be issued
16        let stmt = Statement::with_parent(&conn).unwrap();
17        let res = stmt.exec_direct("SELECT 'HELLO' FROM MOVIES");
18        println!("Result {:?}", res);
19    }
20
21    let end_tx_result = conn.commit();
22
23    println!("End TX result {:?}", end_tx_result);
24}
examples/list_datasources.rs (line 10)
8fn main() {
9
10    let env = Environment::new().unwrap();
11    let mut env = env.declare_version_3().unwrap();
12
13    let mut server_name = [0; 512];
14    let mut description = [0; 512];
15
16    println!("ODBC Data Sources:");
17
18    loop {
19        let (name_length, description_length) =
20            match env.data_sources(SQL_FETCH_NEXT, &mut server_name, &mut description) {
21                ReturnOption::Success(v) => v,
22                ReturnOption::Info(_) => panic!("Buffers not large enough. Truncation occurred."),
23                ReturnOption::NoData(()) => break,
24                ReturnOption::Error(()) => {
25                    panic!("Error occurred. Could use diagnostics to learn more")
26                }
27            };
28
29        println!(
30            "\tName: {}\n\tDescription: {}\n",
31            from_utf8(&server_name[..(name_length as usize)]).unwrap(),
32            from_utf8(&description[..(description_length as usize)]).unwrap()
33        );
34    }
35}
Source

pub fn declare_version<V: Version>( self, ) -> Return<Environment<V>, Environment<NoVersion>>

Before an application allocates a connection which specification it follows. Currently these bindings only support ODBC 3.x.

It is valid to specify ODBC 3.x even then connecting to an ODBC 2.x driver. Applications must however avoid calling 3.x functionality on 2.x drivers. Since drivers are connected at runtime, these kind of errors can not be catched by the type system.

Source

pub fn declare_version_3_8( self, ) -> Return<Environment<Odbc3m8>, Environment<NoVersion>>

Before an application allocates a connection which specification it follows. Currently these bindings only support ODBC 3.x.

It is valid to specify ODBC 3.x even then connecting to an ODBC 2.x driver. Applications must however avoid calling 3.x functionality on 2.x drivers. Since drivers are connected at runtime, these kind of errors can not be catched by the type system.

This method is a shorthand for declare_version::<Odbc3m8>.

Source

pub fn declare_version_3( self, ) -> Return<Environment<Odbc3>, Environment<NoVersion>>

Before an application allocates a connection which specification it follows. Currently these bindings only support ODBC 3.x.

It is valid to specify ODBC 3.x even then connecting to an ODBC 2.x driver. Applications must however avoid calling 3.x functionality on 2.x drivers. Since drivers are connected at runtime, these kind of errors can not be catched by the type system.

This method is a shorthand for declare_version::<Odbc3>.

Examples found in repository?
examples/bind_columns.rs (line 48)
45fn main() {
46
47    let env = Environment::new().unwrap();
48    let env = env.declare_version_3().unwrap();
49
50    match run(&env) {
51        Ok(()) => (),
52        Err(LastError(message)) => println!("An error occurred: {}", message),
53    }
54}
More examples
Hide additional examples
examples/print_table.rs (line 48)
45fn main() {
46
47    let env = Environment::new().unwrap();
48    let env = env.declare_version_3().unwrap();
49
50    match run(&env) {
51        Ok(()) => (),
52        Err(LastError(message)) => println!("An error occurred: {}", message),
53    }
54}
examples/prepared_query.rs (line 9)
6fn main() {
7
8    let env = Environment::new().unwrap();
9    let env = env.declare_version_3().unwrap();
10    let conn = connect(&env);
11    let mut stmt = prepare_query(&conn);
12    for &year in [1968, 1993].iter() {
13        let result_set = execute_query(stmt, year);
14        stmt = print_fields(result_set);
15        println!("");
16    }
17}
examples/affected_row_count.rs (line 8)
6fn main() {
7    let env = Environment::new().unwrap();
8    let env = env.declare_version_3().unwrap();
9    let conn = DataSource::with_parent(&env).unwrap();
10    let conn = conn.connect("TestDataSource", "", "").unwrap();
11    exec(&conn, "INSERT INTO movies (title, year) VALUES ('TEST movie', 9999), ('TEST movie', 9998)");
12    exec(&conn, "DELETE FROM movies WHERE title = 'TEST movie'");
13}
examples/transactions.rs (line 7)
5fn main() {
6    let env = Environment::new().unwrap();
7    let env = env.declare_version_3().unwrap();
8
9    let ds = DataSource::with_parent(&env).unwrap();
10    let conn = ds.connect("TestDataSource", "", "").unwrap();
11    let mut conn = conn.disable_autocommit().unwrap();
12
13    {
14        //Any statement now will start transaction which could be ended with conn.commit() or conn.rollback()
15        //If either commit or rollback was not called before connection drop automatic rollback will be issued
16        let stmt = Statement::with_parent(&conn).unwrap();
17        let res = stmt.exec_direct("SELECT 'HELLO' FROM MOVIES");
18        println!("Result {:?}", res);
19    }
20
21    let end_tx_result = conn.commit();
22
23    println!("End TX result {:?}", end_tx_result);
24}
examples/list_datasources.rs (line 11)
8fn main() {
9
10    let env = Environment::new().unwrap();
11    let mut env = env.declare_version_3().unwrap();
12
13    let mut server_name = [0; 512];
14    let mut description = [0; 512];
15
16    println!("ODBC Data Sources:");
17
18    loop {
19        let (name_length, description_length) =
20            match env.data_sources(SQL_FETCH_NEXT, &mut server_name, &mut description) {
21                ReturnOption::Success(v) => v,
22                ReturnOption::Info(_) => panic!("Buffers not large enough. Truncation occurred."),
23                ReturnOption::NoData(()) => break,
24                ReturnOption::Error(()) => {
25                    panic!("Error occurred. Could use diagnostics to learn more")
26                }
27            };
28
29        println!(
30            "\tName: {}\n\tDescription: {}\n",
31            from_utf8(&server_name[..(name_length as usize)]).unwrap(),
32            from_utf8(&description[..(description_length as usize)]).unwrap()
33        );
34    }
35}

Trait Implementations§

Source§

impl<V: Debug + VersionOption> Debug for Environment<V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<V: VersionOption> Diagnostics for Environment<V>

Source§

fn diagnostics( &self, rec_number: SQLSMALLINT, message_text: &mut [SQLCHAR], ) -> ReturnOption<DiagResult>

Returns the current values of multiple fields of a diagnostic record that contains error, warning, and status information. Read more

Auto Trait Implementations§

§

impl<V> Freeze for Environment<V>

§

impl<V> RefUnwindSafe for Environment<V>
where V: RefUnwindSafe,

§

impl<V> !Send for Environment<V>

§

impl<V> !Sync for Environment<V>

§

impl<V> Unpin for Environment<V>
where V: Unpin,

§

impl<V> UnwindSafe for Environment<V>
where V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.