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::{slice_to_cow_utf8, slice_to_utf8, OutputStringBuffer, SqlChar, SqlText, SzBuffer},
33    sql_result::SqlResult,
34    statement::{AsStatementRef, ParameterDescription, Statement, StatementImpl, StatementRef},
35};
36
37use log::debug;
38use odbc_sys::{Handle, HandleType, SQLFreeHandle, SqlReturn};
39use std::thread::panicking;
40
41/// Helper function freeing a handle and panicking on errors. Yet if the drop is triggered during
42/// another panic, the function will simply ignore errors from failed drops.
43///
44/// # Safety
45///
46/// `handle` Must be a valid ODBC handle and `handle_type` must match its type.
47pub unsafe fn drop_handle(handle: Handle, handle_type: HandleType) {
48    match SQLFreeHandle(handle_type, handle) {
49        SqlReturn::SUCCESS => {
50            debug!("SQLFreeHandle dropped {handle:?} of type {handle_type:?}.");
51        }
52        other => {
53            // Avoid panicking, if we already have a panic. We don't want to mask the
54            // original error.
55            if !panicking() {
56                panic!("SQLFreeHandle failed with error code: {:?}", other.0)
57            }
58        }
59    }
60}