odbc_api/
handles.rs

1//! Provides basic abstraction over valid (i.e. allocated ODBC handles).
2//!
3//! Two decisions are already baked into this module:
4//!
5//! * Treat warnings by logging them with `log`.
6//! * Use the Unicode (wide) variants of the ODBC API.
7
8mod any_handle;
9mod bind;
10mod buffer;
11mod column_description;
12mod connection;
13mod data_type;
14mod descriptor;
15mod diagnostics;
16mod environment;
17mod logging;
18mod sql_char;
19mod sql_result;
20mod statement;
21mod statement_connection;
22
23pub use self::{
24    any_handle::AnyHandle,
25    bind::{CData, CDataMut, DelayedInput, HasDataType},
26    column_description::{ColumnDescription, Nullability},
27    connection::Connection,
28    data_type::DataType,
29    descriptor::Descriptor,
30    diagnostics::{DiagnosticStream, Diagnostics, Record, State},
31    environment::Environment,
32    logging::log_diagnostics,
33    sql_char::{OutputStringBuffer, SqlChar, SqlText, SzBuffer, slice_to_cow_utf8, slice_to_utf8},
34    sql_result::SqlResult,
35    statement::{AsStatementRef, ParameterDescription, Statement, StatementImpl, StatementRef},
36    statement_connection::{StatementConnection, StatementParent},
37};
38
39pub(crate) use self::data_type::ASSUMED_MAX_LENGTH_OF_W_VARCHAR;
40
41use log::debug;
42use odbc_sys::{Handle, HandleType, SQLFreeHandle, SqlReturn};
43use std::thread::panicking;
44
45/// Helper function freeing a handle and panicking on errors. Yet if the drop is triggered during
46/// another panic, the function will simply ignore errors from failed drops.
47///
48/// # Safety
49///
50/// `handle` Must be a valid ODBC handle and `handle_type` must match its type.
51pub unsafe fn drop_handle(handle: Handle, handle_type: HandleType) {
52    match unsafe { SQLFreeHandle(handle_type, handle) } {
53        SqlReturn::SUCCESS => {
54            debug!("SQLFreeHandle dropped {handle:?} of type {handle_type:?}.");
55        }
56        other => {
57            // Avoid panicking, if we already have a panic. We don't want to mask the
58            // original error.
59            if !panicking() {
60                panic!("SQLFreeHandle failed with error code: {:?}", other.0)
61            }
62        }
63    }
64}