odbc_api/handles/
data_type.rs

1use std::num::NonZeroUsize;
2
3use odbc_sys::SqlDataType;
4
5/// For Microsoft SQL Server, but also for Oracle there exists a maximum string length of 4000 for
6/// `NVARCHAR` SQL type.
7pub(crate) const ASSUMED_MAX_LENGTH_OF_W_VARCHAR: usize = 4000;
8
9/// The relational type of the column. Think of it as the type used in the `CREATE TABLE` statement
10/// then creating the database.
11///
12/// There might be a mismatch between the types supported by your database and the types defined in
13/// ODBC. E.g. ODBC does not have a timestamp with timezone type, theras Postgersql and Microsoft
14/// SQL Server both have one. In such cases it is up to the specific ODBC driver what happens.
15/// Microsoft SQL Server return a custom type, with its meaning specific to that driver. PostgreSQL
16/// identifies that column as an ordinary ODBC timestamp.
17#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
18/// Enumeration over valid SQL Data Types supported by ODBC
19pub enum DataType {
20    /// The type is not known.
21    #[default]
22    Unknown,
23    /// `Char(n)`. Character string of fixed length.
24    Char {
25        /// Column size in characters (excluding terminating zero).
26        length: Option<NonZeroUsize>,
27    },
28    /// `NChar(n)`. Character string of fixed length.
29    WChar {
30        /// Column size in characters (excluding terminating zero).
31        length: Option<NonZeroUsize>,
32    },
33    /// `Numeric(p,s). Signed, exact, numeric value with a precision p and scale s (1 <= p <= 15; s
34    /// <= p)
35    Numeric {
36        /// Total number of digits.
37        precision: usize,
38        /// Number of decimal digits.
39        scale: i16,
40    },
41    /// `Decimal(p,s)`. Signed, exact, numeric value with a precision of at least p and scale s.
42    /// The maximum precision is driver-defined. (1 <= p <= 15; s <= p)
43    Decimal {
44        /// Total number of digits.
45        precision: usize,
46        /// Number of decimal digits.
47        scale: i16,
48    },
49    /// `Integer`. 32 Bit Integer
50    Integer,
51    /// `Smallint`. 16 Bit Integer
52    SmallInt,
53    /// `Float(p)`. Signed, approximate, numeric value with a binary precision of at least p. The
54    /// maximum precision is driver-defined.
55    ///
56    /// Depending on the implementation binary precision is either 24 (`f32`) or 53 (`f64`).
57    Float { precision: usize },
58    /// `Real`. Signed, approximate, numeric value with a binary precision 24 (zero or absolute
59    /// value 10^-38] to 10^38).
60    Real,
61    /// `Double Precision`. Signed, approximate, numeric value with a binary precision 53 (zero or
62    /// absolute value 10^-308 to 10^308).
63    Double,
64    /// `Varchar(n)`. Variable length character string.
65    Varchar {
66        /// Maximum length of the character string (excluding terminating zero). Whether this
67        /// length is to be interpreted as bytes or Codepoints is ambigious and depends on
68        /// the datasource.
69        ///
70        /// E.g. For Microsoft SQL Server this is the binary length, theras for a MariaDB this
71        /// refers to codepoints in case of UTF-8 encoding. If you need the binary size query the
72        /// octet length for that column instead.
73        ///
74        /// To find out how to interpret this value for a particular datasource you can use the
75        /// `odbcsv` command line tool `list-columns` subcommand and query a Varchar column. If the
76        /// buffer/octet length matches the column size, you can interpret this as the byte length.
77        length: Option<NonZeroUsize>,
78    },
79    /// `NVARCHAR(n)`. Variable length character string. Indicates the use of wide character strings
80    /// and use of UCS2 encoding on the side of the database.
81    WVarchar {
82        /// Maximum length of the character string (excluding terminating zero).
83        length: Option<NonZeroUsize>,
84    },
85    /// `TEXT`. Variable length characeter string for long text objects.
86    LongVarchar {
87        /// Maximum length of the character string (excluding terminating zero). Maximum size
88        /// depends on the capabilities of the driver and datasource. E.g. its 2^31 - 1 for MSSQL.
89        length: Option<NonZeroUsize>,
90    },
91    /// `NVARCHAR(MAX)`. Variable length characeter string for long text objects. Indicates the use
92    /// of wide character strings and the use of UCS2 encoding on the side of the database.
93    WLongVarchar {
94        /// Maximum length of the character string (excluding terminating zero). Maximum size
95        /// depends on the capabilities of the driver and datasource. E.g. its 2^31 - 1 for MSSQL.
96        length: Option<NonZeroUsize>,
97    },
98    /// `BLOB`. Variable length data for long binary objects.
99    LongVarbinary {
100        /// Maximum length of the binary data. Maximum size depends on the capabilities of the
101        /// driver and datasource.
102        length: Option<NonZeroUsize>,
103    },
104    /// `Date`. Year, month, and day fields, conforming to the rules of the Gregorian calendar.
105    Date,
106    /// `Time`. Hour, minute, and second fields, with valid values for hours of 00 to 23, valid
107    /// values for minutes of 00 to 59, and valid values for seconds of 00 to 61. Precision p
108    /// indicates the seconds precision.
109    Time {
110        /// Number of radix ten digits used to represent the timestamp after the decimal points.
111        /// E.g. Milliseconds would be represented by precision 3, Microseconds by 6 and
112        /// Nanoseconds by 9.
113        precision: i16,
114    },
115    /// `Timestamp`. Year, month, day, hour, minute, and second fields, with valid values as
116    /// defined for the Date and Time variants.
117    Timestamp {
118        /// Number of radix ten digits used to represent the timestamp after the decimal points.
119        /// E.g. Milliseconds would be represented by precision 3, Microseconds by 6 and
120        /// Nanoseconds by 9.
121        precision: i16,
122    },
123    /// `BIGINT`. Exact numeric value with precision 19 (if signed) or 20 (if unsigned) and scale 0
124    /// (signed: -2^63 <= n <= 2^63 - 1, unsigned: 0 <= n <= 2^64 - 1). Has no corresponding
125    /// type in SQL-92.
126    BigInt,
127    /// `TINYINT`. Exact numeric value with precision 3 and scale 0 (signed: -128 <= n <= 127,
128    /// unsigned: 0 <= n <= 255)
129    TinyInt,
130    /// `BIT`. Single bit binary data.
131    Bit,
132    /// `VARBINARY(n)`. Type for variable sized binary data.
133    Varbinary { length: Option<NonZeroUsize> },
134    /// `BINARY(n)`. Type for fixed sized binary data.
135    Binary { length: Option<NonZeroUsize> },
136    /// The driver returned a type, but it is not among the other types of these enumeration. This
137    /// is a catchall, in case the library is incomplete, or the data source supports custom or
138    /// non-standard types.
139    Other {
140        /// Type of the column
141        data_type: SqlDataType,
142        /// Size of column element. This is the size used to bind the data type as a paramater.
143        column_size: Option<NonZeroUsize>,
144        /// Decimal digits returned for the column element. Exact meaning if any depends on the
145        /// `data_type` field. Like `column_size` this is used then using the [`DataType`] to bind
146        /// data as a parameter.
147        decimal_digits: i16,
148    },
149}
150
151impl DataType {
152    /// This constructor is useful to create an instance of the enumeration using values returned by
153    /// ODBC Api calls like `SQLDescribeCol`, rather than just initializing a variant directly.
154    pub fn new(data_type: SqlDataType, column_size: usize, decimal_digits: i16) -> Self {
155        match data_type {
156            SqlDataType::UNKNOWN_TYPE => DataType::Unknown,
157            SqlDataType::EXT_LONG_VARCHAR => DataType::LongVarchar {
158                length: NonZeroUsize::new(column_size),
159            },
160            SqlDataType::EXT_W_LONG_VARCHAR => DataType::WLongVarchar {
161                length: NonZeroUsize::new(column_size),
162            },
163            SqlDataType::EXT_BINARY => DataType::Binary {
164                length: NonZeroUsize::new(column_size),
165            },
166            SqlDataType::EXT_VAR_BINARY => DataType::Varbinary {
167                length: NonZeroUsize::new(column_size),
168            },
169            SqlDataType::EXT_LONG_VAR_BINARY => DataType::LongVarbinary {
170                length: NonZeroUsize::new(column_size),
171            },
172            SqlDataType::CHAR => DataType::Char {
173                length: NonZeroUsize::new(column_size),
174            },
175            SqlDataType::VARCHAR => DataType::Varchar {
176                length: NonZeroUsize::new(column_size),
177            },
178            SqlDataType::NUMERIC => DataType::Numeric {
179                precision: column_size,
180                scale: decimal_digits,
181            },
182            SqlDataType::DECIMAL => DataType::Decimal {
183                precision: column_size,
184                scale: decimal_digits,
185            },
186            SqlDataType::INTEGER => DataType::Integer,
187            SqlDataType::SMALLINT => DataType::SmallInt,
188            SqlDataType::FLOAT => DataType::Float {
189                precision: column_size,
190            },
191            SqlDataType::REAL => DataType::Real,
192            SqlDataType::DOUBLE => DataType::Double,
193            SqlDataType::DATE => DataType::Date,
194            SqlDataType::TIME => DataType::Time {
195                precision: decimal_digits,
196            },
197            SqlDataType::TIMESTAMP => DataType::Timestamp {
198                precision: decimal_digits,
199            },
200            SqlDataType::EXT_BIG_INT => DataType::BigInt,
201            SqlDataType::EXT_TINY_INT => DataType::TinyInt,
202            SqlDataType::EXT_BIT => DataType::Bit,
203            SqlDataType::EXT_W_VARCHAR => DataType::WVarchar {
204                length: NonZeroUsize::new(column_size),
205            },
206            SqlDataType::EXT_W_CHAR => DataType::WChar {
207                length: NonZeroUsize::new(column_size),
208            },
209            other => DataType::Other {
210                data_type: other,
211                column_size: NonZeroUsize::new(column_size),
212                decimal_digits,
213            },
214        }
215    }
216
217    /// The associated consicse SQL `data_type` discriminator for this variant.
218    pub fn data_type(&self) -> SqlDataType {
219        match self {
220            DataType::Unknown => SqlDataType::UNKNOWN_TYPE,
221            DataType::Binary { .. } => SqlDataType::EXT_BINARY,
222            DataType::Varbinary { .. } => SqlDataType::EXT_VAR_BINARY,
223            DataType::LongVarbinary { .. } => SqlDataType::EXT_LONG_VAR_BINARY,
224            DataType::Char { .. } => SqlDataType::CHAR,
225            DataType::Numeric { .. } => SqlDataType::NUMERIC,
226            DataType::Decimal { .. } => SqlDataType::DECIMAL,
227            DataType::Integer => SqlDataType::INTEGER,
228            DataType::SmallInt => SqlDataType::SMALLINT,
229            DataType::Float { .. } => SqlDataType::FLOAT,
230            DataType::Real => SqlDataType::REAL,
231            DataType::Double => SqlDataType::DOUBLE,
232            DataType::Varchar { .. } => SqlDataType::VARCHAR,
233            DataType::LongVarchar { .. } => SqlDataType::EXT_LONG_VARCHAR,
234            DataType::WLongVarchar { .. } => SqlDataType::EXT_W_LONG_VARCHAR,
235            DataType::Date => SqlDataType::DATE,
236            DataType::Time { .. } => SqlDataType::TIME,
237            DataType::Timestamp { .. } => SqlDataType::TIMESTAMP,
238            DataType::BigInt => SqlDataType::EXT_BIG_INT,
239            DataType::TinyInt => SqlDataType::EXT_TINY_INT,
240            DataType::Bit => SqlDataType::EXT_BIT,
241            DataType::WVarchar { .. } => SqlDataType::EXT_W_VARCHAR,
242            DataType::WChar { .. } => SqlDataType::EXT_W_CHAR,
243            DataType::Other { data_type, .. } => *data_type,
244        }
245    }
246
247    // Return the column size, as it is required to bind the data type as a parameter. Fixed sized
248    // types are mapped to `None` and should be bound using `0`. See also
249    // [crates::Cursor::describe_col]. Variadic types without upper bound are also mapped to `None`.
250    pub fn column_size(&self) -> Option<NonZeroUsize> {
251        match self {
252            DataType::Unknown
253            | DataType::Integer
254            | DataType::SmallInt
255            | DataType::Real
256            | DataType::Double
257            | DataType::Date
258            | DataType::Time { .. }
259            | DataType::Timestamp { .. }
260            | DataType::BigInt
261            | DataType::TinyInt
262            | DataType::Bit => None,
263            DataType::Char { length }
264            | DataType::Varchar { length }
265            | DataType::Varbinary { length }
266            | DataType::LongVarbinary { length }
267            | DataType::Binary { length }
268            | DataType::WChar { length }
269            | DataType::WVarchar { length }
270            | DataType::WLongVarchar { length }
271            | DataType::LongVarchar { length } => *length,
272            DataType::Float { precision, .. }
273            | DataType::Numeric { precision, .. }
274            | DataType::Decimal { precision, .. } => NonZeroUsize::new(*precision),
275            DataType::Other { column_size, .. } => *column_size,
276        }
277    }
278
279    /// Return the number of decimal digits as required to bind the data type as a parameter.
280    pub fn decimal_digits(&self) -> i16 {
281        match self {
282            DataType::Unknown
283            | DataType::Char { .. }
284            | DataType::Integer
285            | DataType::SmallInt
286            | DataType::Float { .. }
287            | DataType::Real
288            | DataType::Double
289            | DataType::Varchar { .. }
290            | DataType::WVarchar { .. }
291            | DataType::WChar { .. }
292            | DataType::Varbinary { .. }
293            | DataType::LongVarbinary { .. }
294            | DataType::Binary { .. }
295            | DataType::WLongVarchar { .. }
296            | DataType::LongVarchar { .. }
297            | DataType::Date
298            | DataType::BigInt
299            | DataType::TinyInt
300            | DataType::Bit => 0,
301            DataType::Numeric { scale, .. } | DataType::Decimal { scale, .. } => *scale,
302            DataType::Time { precision } | DataType::Timestamp { precision } => *precision,
303            DataType::Other { decimal_digits, .. } => *decimal_digits,
304        }
305    }
306
307    /// The maximum number of characters needed to display data in character form.
308    ///
309    /// See: <https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/display-size>
310    pub fn display_size(&self) -> Option<NonZeroUsize> {
311        match self {
312            DataType::Unknown
313            | DataType::Other {
314                data_type: _,
315                column_size: _,
316                decimal_digits: _,
317            } => None,
318            // Each binary byte is represented by a 2-digit hexadecimal number.
319            DataType::Varbinary { length }
320            | DataType::Binary { length }
321            | DataType::LongVarbinary { length } => {
322                length.map(|l| l.get() * 2).and_then(NonZeroUsize::new)
323            }
324            // The defined (for fixed types) or maximum (for variable types) number of characters
325            // needed to display the data in character form.
326            DataType::Varchar { length }
327            | DataType::WVarchar { length }
328            | DataType::WChar { length }
329            | DataType::Char { length }
330            | DataType::WLongVarchar { length }
331            | DataType::LongVarchar { length } => *length,
332            // The precision of the column plus 2 (a sign, precision digits, and a decimal point).
333            // For example, the display size of a column defined as NUMERIC(10,3) is 12.
334            DataType::Numeric {
335                precision,
336                scale: _,
337            }
338            | DataType::Decimal {
339                precision,
340                scale: _,
341            } => NonZeroUsize::new(precision + 2),
342            // 11 if signed (a sign and 10 digits) or 10 if unsigned (10 digits).
343            DataType::Integer => NonZeroUsize::new(11),
344            // 6 if signed (a sign and 5 digits) or 5 if unsigned (5 digits).
345            DataType::SmallInt => NonZeroUsize::new(6),
346            // 24 (a sign, 15 digits, a decimal point, the letter E, a sign, and 3 digits).
347            DataType::Float { .. } | DataType::Double => NonZeroUsize::new(24),
348            // 14 (a sign, 7 digits, a decimal point, the letter E, a sign, and 2 digits).
349            DataType::Real => NonZeroUsize::new(14),
350            // 10 (a date in the format yyyy-mm-dd).
351            DataType::Date => NonZeroUsize::new(10),
352            // 8 (a time in the format hh:mm:ss)
353            // or
354            // 9 + s (a time in the format hh:mm:ss[.fff...], where s is the fractional seconds
355            // precision).
356            DataType::Time { precision } => NonZeroUsize::new(if *precision == 0 {
357                8
358            } else {
359                9 + *precision as usize
360            }),
361            // 19 (for a timestamp in the yyyy-mm-dd hh:mm:ss format)
362            // or
363            // 20 + s (for a timestamp in the yyyy-mm-dd hh:mm:ss[.fff...] format, where s is the
364            // fractional seconds precision).
365            DataType::Timestamp { precision } => NonZeroUsize::new(if *precision == 0 {
366                19
367            } else {
368                20 + *precision as usize
369            }),
370            // 20 (a sign and 19 digits if signed or 20 digits if unsigned).
371            DataType::BigInt => NonZeroUsize::new(20),
372            // 4 if signed (a sign and 3 digits) or 3 if unsigned (3 digits).
373            DataType::TinyInt => NonZeroUsize::new(4),
374            // 1 digit.
375            DataType::Bit => NonZeroUsize::new(1),
376        }
377    }
378
379    /// The maximum length of the UTF-8 representation in bytes.
380    ///
381    /// ```
382    /// use odbc_api::DataType;
383    /// use std::num::NonZeroUsize;
384    ///
385    /// let nz = NonZeroUsize::new;
386    /// // Character set data types length is multiplied by four.
387    /// assert_eq!(DataType::Varchar { length: nz(10) }.utf8_len(), nz(40));
388    /// assert_eq!(DataType::Char { length: nz(10) }.utf8_len(), nz(40));
389    /// assert_eq!(DataType::WVarchar { length: nz(10) }.utf8_len(), nz(40));
390    /// assert_eq!(DataType::WChar { length: nz(10) }.utf8_len(), nz(40));
391    /// assert_eq!(DataType::LongVarchar { length: nz(10) }.utf8_len(), nz(40));
392    /// assert_eq!(DataType::WLongVarchar { length: nz(10) }.utf8_len(), nz(40));
393    /// // For other types return value is identical to display size as they are assumed to be
394    /// // entirely representable with ASCII characters.
395    /// assert_eq!(DataType::Numeric { precision: 10, scale: 3}.utf8_len(), nz(10 + 2));
396    /// ```
397    pub fn utf8_len(&self) -> Option<NonZeroUsize> {
398        match self {
399            // One character may need up to four bytes to be represented in utf-8.
400            DataType::Varchar { length }
401            | DataType::WVarchar { length }
402            | DataType::Char { length }
403            | DataType::WChar { length }
404            | DataType::LongVarchar { length }
405            | DataType::WLongVarchar { length } => {
406                length.map(|l| l.get() * 4).and_then(NonZeroUsize::new)
407            }
408            other => other.display_size(),
409        }
410    }
411
412    /// The maximum length of the UTF-16 representation in 2-Byte characters.
413    ///
414    /// ```
415    /// use odbc_api::DataType;
416    /// use std::num::NonZeroUsize;
417    ///
418    /// let nz = NonZeroUsize::new;
419    ///
420    /// // Character set data types length is multiplied by two.
421    /// assert_eq!(DataType::Varchar { length: nz(10) }.utf16_len(), nz(20));
422    /// assert_eq!(DataType::Char { length: nz(10) }.utf16_len(), nz(20));
423    /// assert_eq!(DataType::WVarchar { length: nz(10) }.utf16_len(), nz(20));
424    /// assert_eq!(DataType::WChar { length: nz(10) }.utf16_len(), nz(20));
425    /// assert_eq!(DataType::LongVarchar { length: nz(10) }.utf16_len(), nz(20));
426    /// assert_eq!(DataType::WLongVarchar { length: nz(10) }.utf16_len(), nz(20));
427    /// // For other types return value is identical to display size as they are assumed to be
428    /// // entirely representable with ASCII characters.
429    /// assert_eq!(DataType::Numeric { precision: 10, scale: 3}.utf16_len(), nz(10 + 2));
430    /// ```
431    pub fn utf16_len(&self) -> Option<NonZeroUsize> {
432        match self {
433            // One character may need up to two u16 to be represented in utf-16.
434            DataType::Varchar { length }
435            | DataType::WVarchar { length }
436            | DataType::WChar { length }
437            | DataType::Char { length }
438            | DataType::LongVarchar { length }
439            | DataType::WLongVarchar { length } => {
440                length.map(|l| l.get() * 2).and_then(NonZeroUsize::new)
441            }
442            other => other.display_size(),
443        }
444    }
445}