Struct odbc_api::Environment [−][src]
pub struct Environment { /* fields omitted */ }
An ODBC 3.8 environment.
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
Creating the environment is the first applications do, then interacting with an ODBC driver manager. There must only be one environment in the entire process.
Implementations
impl Environment
[src]
pub unsafe fn new() -> Result<Self, Error>
[src]
Entry point into this API. Allocates a new ODBC Environment and declares to the driver manager that the Application wants to use ODBC version 3.8.
Safety
There may only be one ODBC environment in any process at any time. Take care using this function in unit tests, as these run in parallel by default in Rust. Also no library should probably wrap the creation of an odbc environment into a safe function call. This is because using two of these “safe” libraries at the same time in different parts of your program may lead to race condition thus violating Rust’s safety guarantees.
Creating one environment in your binary is safe however.
pub fn connect(
&self,
data_source_name: &str,
user: &str,
pwd: &str
) -> Result<Connection<'_>, Error>
[src]
&self,
data_source_name: &str,
user: &str,
pwd: &str
) -> Result<Connection<'_>, Error>
Allocates a connection handle and establishes connections to a driver and a data source.
Arguments
data_source_name
- Data source name. The data might be located on the same computer as the program, or on another computer somewhere on a network.user
- User identifier.pwd
- Authentication string (typically the password).
Example
use odbc_api::Environment; // I herby solemnly swear that this is the only ODBC environment in the entire process, thus // making this call safe. let env = unsafe { Environment::new()? }; let mut conn = env.connect("YourDatabase", "SA", "<YourStrong@Passw0rd>")?;
pub fn connect_utf16(
&self,
data_source_name: &U16Str,
user: &U16Str,
pwd: &U16Str
) -> Result<Connection<'_>, Error>
[src]
&self,
data_source_name: &U16Str,
user: &U16Str,
pwd: &U16Str
) -> Result<Connection<'_>, Error>
Allocates a connection handle and establishes connections to a driver and a data source.
Arguments
data_source_name
- Data source name. The data might be located on the same computer as the program, or on another computer somewhere on a network.user
- User identifier.pwd
- Authentication string (typically the password).
pub fn connect_with_connection_string(
&self,
connection_string: &str
) -> Result<Connection<'_>, Error>
[src]
&self,
connection_string: &str
) -> Result<Connection<'_>, Error>
Allocates a connection handle and establishes connections to a driver and a data source.
An alternative to connect
. It supports data sources that require more connection
information than the three arguments in connect
and data sources that are not defined in
the system information.
To find out your connection string try: https://www.connectionstrings.com/
Example
use odbc_api::Environment; // I herby solemnly swear that this is the only ODBC environment in the entire process, thus // making this call safe. let env = unsafe { Environment::new()? }; let connection_string = " Driver={ODBC Driver 17 for SQL Server};\ Server=localhost;\ UID=SA;\ PWD=<YourStrong@Passw0rd>;\ "; let mut conn = env.connect_with_connection_string(connection_string)?;
pub fn connect_with_connection_string_utf16(
&self,
connection_string: &U16Str
) -> Result<Connection<'_>, Error>
[src]
&self,
connection_string: &U16Str
) -> Result<Connection<'_>, Error>
Allocates a connection handle and establishes connections to a driver and a data source.
An alternative to connect
. It supports data sources that require more connection
information than the three arguments in connect
and data sources that are not defined in
the system information.
To find out your connection string try: https://www.connectionstrings.com/
pub fn driver_connect(
&self,
connection_string: &str,
completed_connection_string: Option<&mut OutputStringBuffer>,
driver_completion: DriverCompleteOption<'_>
) -> Result<Connection<'_>, Error>
[src]
&self,
connection_string: &str,
completed_connection_string: Option<&mut OutputStringBuffer>,
driver_completion: DriverCompleteOption<'_>
) -> Result<Connection<'_>, Error>
Allocates a connection handle and establishes connections to a driver and a data source.
An alternative to connect
and connect_with_connection_string
. This method can be
provided with an incomplete or even empty connection string. If any additional information
is required, the driver manager/driver will attempt to create a prompt to allow the user to
provide the additional information.
If the connection is successful, the complete connection string (including any information provided by the user through a prompt) is returned.
Parameters
connection_string
: Connection string.completed_connection_string
: Output buffer with the complete connection string. It is recommended to choose a buffer with at least1024
bytes length.driver_completion
: Specifies how and if the driver manager uses a prompt to complete the provided connection string.
Examples
In the first example, we intentionally provide a blank connection string so the user will be prompted to select a data source to use. Note that this functionality is only available on windows.
use odbc_api::{Environment, handles::OutputStringBuffer, DriverCompleteOption}; // I hereby solemnly swear that this is the only ODBC environment in the entire process, // thus making this call safe. let env = unsafe { Environment::new()? }; let mut output_buffer = OutputStringBuffer::with_buffer_size(1024); let connection = env.driver_connect( "", Some(&mut output_buffer), DriverCompleteOption::Prompt(&parent_window), )?; // Check that the output buffer has been large enough to hold the entire connection string. assert!(!output_buffer.is_truncated()); // Now `connection_string` will contain the data source selected by the user. let connection_string = output_buffer.to_utf8();
In the following examples we specify a DSN that requires login credentials, but the DSN does
not provide those credentials. Instead, the user will be prompted for a UID and PWD. The
returned connection_string
will contain the UID
and PWD
provided by the user. Note
that this functionality is currently only available on windows targets.
let without_uid_or_pwd = "DSN=SomeSharedDatabase;"; let connection = env.driver_connect( &without_uid_or_pwd, Some(&mut output_buffer), DriverCompleteOption::Complete(&parent_window), )?; let connection_string = output_buffer.to_utf8(); // Now `connection_string` might be something like // `DSN=SomeSharedDatabase;UID=SA;PWD=<YourStrong@Passw0rd>;`
In this case, we use a DSN that is already sufficient and does not require a prompt. Because
a prompt is not needed, window
is also not required. The returned connection_string
will
be mostly the same as already_sufficient
but the driver may append some extra attributes.
let already_sufficient = "DSN=MicrosoftAccessFile;"; let connection = env.driver_connect( &already_sufficient, Some(&mut output_buffer), DriverCompleteOption::NoPrompt, )?; let connection_string = output_buffer.to_utf8(); // Now `connection_string` might be something like // `DSN=MicrosoftAccessFile;DBQ=C:\Db\Example.accdb;DriverId=25;FIL=MS Access;MaxBufferSize=2048;`
pub fn drivers(&self) -> Result<Vec<DriverInfo>, Error>
[src]
Get information about available drivers. Only 32 or 64 Bit drivers will be listed, depending on wether you are building a 32 Bit or 64 Bit application.
Example
use odbc_api::Environment; let env = unsafe { Environment::new () }?; for driver_info in env.drivers()? { println!("{:#?}", driver_info); }
pub fn data_sources(&self) -> Result<Vec<DataSourceInfo>, Error>
[src]
User and system data sources
Example
use odbc_api::Environment; let env = unsafe { Environment::new () }?; for data_source in env.data_sources()? { println!("{:#?}", data_source); }
pub fn system_data_sources(&self) -> Result<Vec<DataSourceInfo>, Error>
[src]
Only system data sources
Example
use odbc_api::Environment; let env = unsafe { Environment::new () }?; for data_source in env.system_data_sources()? { println!("{:#?}", data_source); }
pub fn user_data_sources(&self) -> Result<Vec<DataSourceInfo>, Error>
[src]
Only user data sources
Example
use odbc_api::Environment; let mut env = unsafe { Environment::new () }?; for data_source in env.user_data_sources()? { println!("{:#?}", data_source); }
Auto Trait Implementations
impl RefUnwindSafe for Environment
impl Send for Environment
impl Sync for Environment
impl Unpin for Environment
impl UnwindSafe for Environment
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,