odbc_safe/
data_type.rs

1use sys::*;
2
3/// Describes a column or parameter type.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum DataType {
6    /// Fixed sized single byte character data
7    Char(SQLULEN),
8    /// Exact numerical, with (Precision, Scale)
9    Numeric(SQLULEN, SQLSMALLINT),
10    /// Exact numerical, with (Precision, Scale)
11    Decimal(SQLULEN, SQLSMALLINT),
12    /// Integer numerical with precision 10
13    Integer,
14    /// Small integer numerical with precision 5
15    SmallInt,
16    /// Approximate numerical with precision 15
17    Float,
18    /// Approximate numerical with precison 7
19    Real,
20    /// Approximate numerical with precision 15
21    Double,
22    /// Variadic sized single byte character data
23    Varchar(SQLULEN),
24}
25
26/// Determines the type stored at the data source
27///
28/// See [Data Types][1]
29/// [1]: https://docs.microsoft.com/sql/odbc/reference/appendixes/appendix-d-data-types
30impl DataType {
31    /// Creates a `DataType` from a triplet. If the data_type does not require the information
32    /// `column_size` or `decimal_digits`.
33    pub fn new(
34        data_type: SqlDataType,
35        column_size: SQLULEN,
36        decimal_digits: SQLSMALLINT,
37    ) -> Option<DataType> {
38        use DataType::*;
39        match data_type {
40            SQL_CHAR => Some(Char(column_size)),
41            SQL_NUMERIC => Some(Numeric(column_size, decimal_digits)),
42            SQL_DECIMAL => Some(Decimal(column_size, decimal_digits)),
43            SQL_INTEGER => Some(Integer),
44            SQL_SMALLINT => Some(SmallInt),
45            SQL_FLOAT => Some(Float),
46            SQL_REAL => Some(Real),
47            SQL_DOUBLE => Some(Double),
48            SQL_VARCHAR => Some(Varchar(column_size)),
49            SQL_UNKNOWN_TYPE => None,
50            other => panic!("Returned unsupported type: {:?}", other),
51        }
52    }
53
54    /// See [SQL Data Types][1]
55    /// [1]: https://docs.microsoft.com/sql/odbc/reference/appendixes/sql-data-types
56    pub fn sql_data_type(&self) -> SqlDataType {
57        use DataType::*;
58        match *self {
59            Char(_) => SQL_CHAR,
60            Numeric(_, _) => SQL_NUMERIC,
61            Decimal(_, _) => SQL_DECIMAL,
62            Integer => SQL_INTEGER,
63            SmallInt => SQL_SMALLINT,
64            Float => SQL_FLOAT,
65            Real => SQL_REAL,
66            Double => SQL_DOUBLE,
67            Varchar(_) => SQL_VARCHAR,
68        }
69    }
70
71    /// See [Column Size][1]
72    /// [1]: https://docs.microsoft.com/sql/odbc/reference/appendixes/column-size
73    pub fn column_size(&self) -> SQLULEN {
74        use DataType::*;
75        match *self {
76            Numeric(precision, _) |
77            Decimal(precision, _) => precision,
78            Integer => 10,
79            SmallInt => 5,
80            Float | Double => 15,
81            Real => 7,
82            Char(len) | Varchar(len) => len,
83        }
84    }
85
86    /// See [Decimal Digits][1]
87    /// [1]: https://docs.microsoft.com/sql/odbc/reference/appendixes/decimal-digits
88    pub fn decimal_digits(&self) -> SQLSMALLINT {
89        use DataType::*;
90        match *self {
91            Char(_) | Integer | Float | Real | Double | Varchar(_) => 0,
92            Numeric(_, scale) |
93            Decimal(_, scale) => scale,
94            SmallInt => 5,
95        }
96    }
97}