Struct odbc_api::handles::Connection
source · pub struct Connection<'c> { /* private fields */ }Expand description
The connection handle references storage of all information about the connection to the data source, including status, transaction state, and error information.
Implementations§
source§impl<'c> Connection<'c>
impl<'c> Connection<'c>
sourcepub unsafe fn new(handle: HDbc) -> Self
pub unsafe fn new(handle: HDbc) -> Self
Safety
Call this method only with a valid (successfully allocated) ODBC connection handle.
Examples found in repository?
More examples
sourcepub fn as_sys(&self) -> HDbc
pub fn as_sys(&self) -> HDbc
Directly acces the underlying ODBC handle.
Examples found in repository?
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
pub fn into_sys(self) -> HDbc {
// We do not want to run the drop handler, but transfer ownership instead.
ManuallyDrop::new(self).connection.as_sys()
}
/// Transfer ownership of this open connection to a wrapper around the raw ODBC pointer. The
/// wrapper allows you to call ODBC functions on the handle, but doesn't care if the connection
/// is in the right state.
///
/// You should not have a need to call this method if your usecase is covered by this library,
/// but, in case it is not, this may help you to break out of the type structure which might be
/// to rigid for you, while simultaniously abondoning its safeguards.
pub fn into_handle(self) -> handles::Connection<'c> {
unsafe { handles::Connection::new(ManuallyDrop::new(self).connection.as_sys()) }
}sourcepub fn connect(
&mut self,
data_source_name: &SqlText<'_>,
user: &SqlText<'_>,
pwd: &SqlText<'_>
) -> SqlResult<()>
pub fn connect(
&mut self,
data_source_name: &SqlText<'_>,
user: &SqlText<'_>,
pwd: &SqlText<'_>
) -> SqlResult<()>
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).
Examples found in repository?
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
pub fn connect(
&self,
data_source_name: &str,
user: &str,
pwd: &str,
) -> Result<Connection<'_>, Error> {
let data_source_name = SqlText::new(data_source_name);
let user = SqlText::new(user);
let pwd = SqlText::new(pwd);
let mut connection = self.allocate_connection()?;
connection
.connect(&data_source_name, &user, &pwd)
.into_result(&connection)?;
Ok(Connection::new(connection))
}sourcepub fn connect_with_connection_string(
&mut self,
connection_string: &SqlText<'_>
) -> SqlResult<()>
pub fn connect_with_connection_string(
&mut self,
connection_string: &SqlText<'_>
) -> SqlResult<()>
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.
sourcepub unsafe fn driver_connect(
&mut self,
connection_string: &SqlText<'_>,
parent_window: HWnd,
completed_connection_string: &mut OutputStringBuffer,
driver_completion: DriverConnectOption
) -> SqlResult<()>
pub unsafe fn driver_connect(
&mut self,
connection_string: &SqlText<'_>,
parent_window: HWnd,
completed_connection_string: &mut OutputStringBuffer,
driver_completion: DriverConnectOption
) -> SqlResult<()>
An alternative to connect for connecting with a connection string. Allows for completing
a connection string with a GUI prompt on windows.
Return
SqlResult::NoData in case the prompt completing the connection string has been aborted.
Safety
parent_window must either be a valid window handle or NULL.
Examples found in repository?
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
pub fn connect_with_connection_string(&mut self, connection_string: &SqlText) -> SqlResult<()> {
unsafe {
let parent_window = null_mut();
let mut completed_connection_string = OutputStringBuffer::empty();
self.driver_connect(
connection_string,
parent_window,
&mut completed_connection_string,
DriverConnectOption::NoPrompt,
)
// Since we did pass NoPrompt we know the user can not abort the prompt.
.map(|_connection_string_is_complete| ())
}
}More examples
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
pub unsafe fn driver_connect_with_hwnd(
&self,
connection_string: &str,
completed_connection_string: &mut OutputStringBuffer,
driver_completion: DriverCompleteOption,
parent_window: HWnd,
) -> Result<Connection<'_>, Error> {
let mut connection = self.allocate_connection()?;
let connection_string = SqlText::new(connection_string);
let connection_string_is_complete = connection
.driver_connect(
&connection_string,
parent_window,
completed_connection_string,
driver_completion.as_sys(),
)
.into_result_bool(&connection)?;
if !connection_string_is_complete {
return Err(Error::AbortedConnectionStringCompletion);
}
Ok(Connection::new(connection))
}sourcepub fn disconnect(&mut self) -> SqlResult<()>
pub fn disconnect(&mut self) -> SqlResult<()>
Disconnect from an ODBC data source.
Examples found in repository?
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn drop(&mut self) {
match self.connection.disconnect().into_result(&self.connection) {
Ok(()) => (),
Err(Error::Diagnostics {
record,
function: _,
}) if record.state == State::INVALID_STATE_TRANSACTION => {
// Invalid transaction state. Let's rollback the current transaction and try again.
if let Err(e) = self.rollback() {
// Avoid panicking, if we already have a panic. We don't want to mask the original
// error.
if !panicking() {
panic!(
"Unexpected error rolling back transaction (In order to recover \
from invalid transaction state during disconnect): {:?}",
e
)
}
}
// Transaction is rolled back. Now let's try again to disconnect.
if let Err(e) = self.connection.disconnect().into_result(&self.connection) {
// Avoid panicking, if we already have a panic. We don't want to mask the original
// error.
if !panicking() {
panic!("Unexpected error disconnecting): {:?}", e)
}
}
}
Err(e) => {
// Avoid panicking, if we already have a panic. We don't want to mask the original
// error.
if !panicking() {
panic!("Unexpected error disconnecting: {:?}", e)
}
}
}
}sourcepub fn allocate_statement(&self) -> SqlResult<StatementImpl<'_>>
pub fn allocate_statement(&self) -> SqlResult<StatementImpl<'_>>
Allocate a new statement handle. The Statement must not outlive the Connection.
sourcepub fn set_autocommit(&self, enabled: bool) -> SqlResult<()>
pub fn set_autocommit(&self, enabled: bool) -> SqlResult<()>
Specify the transaction mode. By default, ODBC transactions are in auto-commit mode (unless SQLSetConnectAttr and SQLSetConnectOption are not supported, which is unlikely). Switching from manual-commit mode to auto-commit mode automatically commits any open transaction on the connection.
sourcepub fn fetch_database_management_system_name(
&self,
buf: &mut Vec<SqlChar>
) -> SqlResult<()>
pub fn fetch_database_management_system_name(
&self,
buf: &mut Vec<SqlChar>
) -> SqlResult<()>
Fetch the name of the database management system used by the connection and store it into
the provided buf.
sourcepub fn max_catalog_name_len(&self) -> SqlResult<u16>
pub fn max_catalog_name_len(&self) -> SqlResult<u16>
Maximum length of catalog names.
sourcepub fn max_schema_name_len(&self) -> SqlResult<u16>
pub fn max_schema_name_len(&self) -> SqlResult<u16>
Maximum length of schema names.
sourcepub fn max_table_name_len(&self) -> SqlResult<u16>
pub fn max_table_name_len(&self) -> SqlResult<u16>
Maximum length of table names.
sourcepub fn max_column_name_len(&self) -> SqlResult<u16>
pub fn max_column_name_len(&self) -> SqlResult<u16>
Maximum length of column names.
Trait Implementations§
source§impl<'c> AsHandle for Connection<'c>
impl<'c> AsHandle for Connection<'c>
source§fn as_handle(&self) -> Handle
fn as_handle(&self) -> Handle
source§fn handle_type(&self) -> HandleType
fn handle_type(&self) -> HandleType
as_handle. This is a method rather than a constant
in order to make the type object safe.