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 as_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;
21
22pub use {
23    as_handle::AsHandle,
24    bind::{CData, CDataMut, DelayedInput, HasDataType},
25    column_description::{ColumnDescription, Nullability},
26    connection::Connection,
27    data_type::DataType,
28    descriptor::Descriptor,
29    diagnostics::{Diagnostics, Record, State},
30    environment::Environment,
31    logging::log_diagnostics,
32    sql_char::{OutputStringBuffer, SqlChar, SqlText, SzBuffer, slice_to_cow_utf8, slice_to_utf8},
33    sql_result::SqlResult,
34    statement::{AsStatementRef, ParameterDescription, Statement, StatementImpl, StatementRef},
35};
36
37pub (crate) use data_type::ASSUMED_MAX_LENGTH_OF_W_VARCHAR;
38
39use log::debug;
40use odbc_sys::{Handle, HandleType, SQLFreeHandle, SqlReturn};
41use std::thread::panicking;
42
43/// Helper function freeing a handle and panicking on errors. Yet if the drop is triggered during
44/// another panic, the function will simply ignore errors from failed drops.
45///
46/// # Safety
47///
48/// `handle` Must be a valid ODBC handle and `handle_type` must match its type.
49pub unsafe fn drop_handle(handle: Handle, handle_type: HandleType) {
50    match unsafe { SQLFreeHandle(handle_type, handle) } {
51        SqlReturn::SUCCESS => {
52            debug!("SQLFreeHandle dropped {handle:?} of type {handle_type:?}.");
53        }
54        other => {
55            // Avoid panicking, if we already have a panic. We don't want to mask the
56            // original error.
57            if !panicking() {
58                panic!("SQLFreeHandle failed with error code: {:?}", other.0)
59            }
60        }
61    }
62}