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