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
use super::{
data_type::DataType,
sql_char::{slice_to_utf8, DecodingError, SqlChar},
};
#[derive(Clone, Copy, Hash, Debug, Eq, PartialEq)]
pub enum Nullability {
Unknown,
Nullable,
NoNulls,
}
impl Default for Nullability {
fn default() -> Self {
Nullability::Unknown
}
}
impl Nullability {
pub fn new(nullability: odbc_sys::Nullability) -> Self {
match nullability {
odbc_sys::Nullability::UNKNOWN => Nullability::Unknown,
odbc_sys::Nullability::NO_NULLS => Nullability::NoNulls,
odbc_sys::Nullability::NULLABLE => Nullability::Nullable,
other => panic!("ODBC returned invalid value for Nullable: {}", other.0),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct ColumnDescription {
pub name: Vec<SqlChar>,
pub data_type: DataType,
pub nullability: Nullability,
}
impl ColumnDescription {
pub fn name_to_string(&self) -> Result<String, DecodingError> {
slice_to_utf8(&self.name)
}
pub fn could_be_nullable(&self) -> bool {
match self.nullability {
Nullability::Nullable | Nullability::Unknown => true,
Nullability::NoNulls => false,
}
}
}
#[cfg(test)]
mod tests {
use crate::Nullability;
#[test]
#[should_panic(expected = "ODBC returned invalid value for Nullable: 5")]
fn invalid_nullable_representation() {
Nullability::new(odbc_sys::Nullability(5));
}
}