odbc_api/parameter/
c_string.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! Module contains trait implementations for `CStr` and `CString`.

use std::{
    ffi::{c_void, CStr, CString},
    num::NonZeroUsize,
};

use odbc_sys::{CDataType, NTS};

use crate::{
    handles::{CData, HasDataType},
    parameter::InputParameter,
    DataType,
};

use super::CElement;

unsafe impl CData for CStr {
    fn cdata_type(&self) -> CDataType {
        CDataType::Char
    }

    fn indicator_ptr(&self) -> *const isize {
        &NTS as *const isize
    }

    fn value_ptr(&self) -> *const c_void {
        self.as_ptr() as *const c_void
    }

    fn buffer_length(&self) -> isize {
        0
    }
}

impl HasDataType for CStr {
    fn data_type(&self) -> DataType {
        DataType::Varchar {
            length: NonZeroUsize::new(self.to_bytes().len()),
        }
    }
}
unsafe impl CElement for CStr {
    /// `CStr` is terminated by zero. Indicator can therefore never indicate a truncated value and
    /// is always complete.
    fn assert_completness(&self) {}
}
impl InputParameter for CStr {}

unsafe impl CData for CString {
    fn cdata_type(&self) -> CDataType {
        CDataType::Char
    }

    fn indicator_ptr(&self) -> *const isize {
        &NTS as *const isize
    }

    fn value_ptr(&self) -> *const c_void {
        self.as_ptr() as *const c_void
    }

    fn buffer_length(&self) -> isize {
        0
    }
}

impl HasDataType for CString {
    fn data_type(&self) -> DataType {
        DataType::Varchar {
            length: NonZeroUsize::new(self.as_bytes().len()),
        }
    }
}

unsafe impl CElement for CString {
    /// `CString`` is terminated by zero. Indicator can therefore never indicate a truncated value
    /// and is always complete.
    fn assert_completness(&self) {}
}