Skip to main content

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::{DiagnosticResult, 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, ColumnType, Statement, StatementImpl, StatementRef},
36    statement_connection::{StatementConnection, StatementParent},
37};
38
39#[allow(deprecated)]
40pub use self::statement::ParameterDescription;
41
42pub(crate) use self::data_type::{ASSUMED_MAX_LENGTH_OF_VARCHAR, ASSUMED_MAX_LENGTH_OF_W_VARCHAR};
43
44use log::debug;
45use odbc_sys::{Handle, HandleType, SQLFreeHandle, SqlReturn};
46use std::thread::panicking;
47
48/// Helper function freeing a handle and panicking on errors. Yet if the drop is triggered during
49/// another panic, the function will simply ignore errors from failed drops.
50///
51/// # Safety
52///
53/// `handle` Must be a valid ODBC handle and `handle_type` must match its type.
54pub unsafe fn drop_handle(handle: Handle, handle_type: HandleType) {
55    match unsafe { SQLFreeHandle(handle_type, handle) } {
56        SqlReturn::SUCCESS => {
57            debug!("SQLFreeHandle dropped {handle:?} of type {handle_type:?}.");
58        }
59        other => {
60            // Avoid panicking, if we already have a panic. We don't want to mask the
61            // original error.
62            if !panicking() {
63                panic!("SQLFreeHandle failed with error code: {:?}", other.0)
64            }
65        }
66    }
67}