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
142 column_size: Option<NonZeroUsize>,
143 /// Decimal digits returned for the column element. Exact meaning if any depends on the
144 /// `data_type` field.
145 decimal_digits: i16,
146 },
147}
148
149impl DataType {
150 /// This constructor is useful to create an instance of the enumeration using values returned by
151 /// ODBC Api calls like `SQLDescribeCol`, rather than just initializing a variant directly.
152 pub fn new(data_type: SqlDataType, column_size: usize, decimal_digits: i16) -> Self {
153 match data_type {
154 SqlDataType::UNKNOWN_TYPE => DataType::Unknown,
155 SqlDataType::EXT_LONG_VARCHAR => DataType::LongVarchar {
156 length: NonZeroUsize::new(column_size),
157 },
158 SqlDataType::EXT_BINARY => DataType::Binary {
159 length: NonZeroUsize::new(column_size),
160 },
161 SqlDataType::EXT_VAR_BINARY => DataType::Varbinary {
162 length: NonZeroUsize::new(column_size),
163 },
164 SqlDataType::EXT_LONG_VAR_BINARY => DataType::LongVarbinary {
165 length: NonZeroUsize::new(column_size),
166 },
167 SqlDataType::CHAR => DataType::Char {
168 length: NonZeroUsize::new(column_size),
169 },
170 SqlDataType::VARCHAR => DataType::Varchar {
171 length: NonZeroUsize::new(column_size),
172 },
173 SqlDataType::NUMERIC => DataType::Numeric {
174 precision: column_size,
175 scale: decimal_digits,
176 },
177 SqlDataType::DECIMAL => DataType::Decimal {
178 precision: column_size,
179 scale: decimal_digits,
180 },
181 SqlDataType::INTEGER => DataType::Integer,
182 SqlDataType::SMALLINT => DataType::SmallInt,
183 SqlDataType::FLOAT => DataType::Float {
184 precision: column_size,
185 },
186 SqlDataType::REAL => DataType::Real,
187 SqlDataType::DOUBLE => DataType::Double,
188 SqlDataType::DATE => DataType::Date,
189 SqlDataType::TIME => DataType::Time {
190 precision: decimal_digits,
191 },
192 SqlDataType::TIMESTAMP => DataType::Timestamp {
193 precision: decimal_digits,
194 },
195 SqlDataType::EXT_BIG_INT => DataType::BigInt,
196 SqlDataType::EXT_TINY_INT => DataType::TinyInt,
197 SqlDataType::EXT_BIT => DataType::Bit,
198 SqlDataType::EXT_W_VARCHAR => DataType::WVarchar {
199 length: NonZeroUsize::new(column_size),
200 },
201 SqlDataType::EXT_W_CHAR => DataType::WChar {
202 length: NonZeroUsize::new(column_size),
203 },
204 other => DataType::Other {
205 data_type: other,
206 column_size: NonZeroUsize::new(column_size),
207 decimal_digits,
208 },
209 }
210 }
211
212 /// The associated `data_type` discriminator for this variant.
213 pub fn data_type(&self) -> SqlDataType {
214 match self {
215 DataType::Unknown => SqlDataType::UNKNOWN_TYPE,
216 DataType::Binary { .. } => SqlDataType::EXT_BINARY,
217 DataType::Varbinary { .. } => SqlDataType::EXT_VAR_BINARY,
218 DataType::LongVarbinary { .. } => SqlDataType::EXT_LONG_VAR_BINARY,
219 DataType::Char { .. } => SqlDataType::CHAR,
220 DataType::Numeric { .. } => SqlDataType::NUMERIC,
221 DataType::Decimal { .. } => SqlDataType::DECIMAL,
222 DataType::Integer => SqlDataType::INTEGER,
223 DataType::SmallInt => SqlDataType::SMALLINT,
224 DataType::Float { .. } => SqlDataType::FLOAT,
225 DataType::Real => SqlDataType::REAL,
226 DataType::Double => SqlDataType::DOUBLE,
227 DataType::Varchar { .. } => SqlDataType::VARCHAR,
228 DataType::LongVarchar { .. } => SqlDataType::EXT_LONG_VARCHAR,
229 DataType::WLongVarchar { .. } => SqlDataType::EXT_W_LONG_VARCHAR,
230 DataType::Date => SqlDataType::DATE,
231 DataType::Time { .. } => SqlDataType::TIME,
232 DataType::Timestamp { .. } => SqlDataType::TIMESTAMP,
233 DataType::BigInt => SqlDataType::EXT_BIG_INT,
234 DataType::TinyInt => SqlDataType::EXT_TINY_INT,
235 DataType::Bit => SqlDataType::EXT_BIT,
236 DataType::WVarchar { .. } => SqlDataType::EXT_W_VARCHAR,
237 DataType::WChar { .. } => SqlDataType::EXT_W_CHAR,
238 DataType::Other { data_type, .. } => *data_type,
239 }
240 }
241
242 // Return the column size, as it is required to bind the data type as a parameter. Fixed sized
243 // types are mapped to `None` and should be bound using `0`. See also
244 // [crates::Cursor::describe_col]. Variadic types without upper bound are also mapped to `None`.
245 pub fn column_size(&self) -> Option<NonZeroUsize> {
246 match self {
247 DataType::Unknown
248 | DataType::Integer
249 | DataType::SmallInt
250 | DataType::Real
251 | DataType::Double
252 | DataType::Date
253 | DataType::Time { .. }
254 | DataType::Timestamp { .. }
255 | DataType::BigInt
256 | DataType::TinyInt
257 | DataType::Bit => None,
258 DataType::Char { length }
259 | DataType::Varchar { length }
260 | DataType::Varbinary { length }
261 | DataType::LongVarbinary { length }
262 | DataType::Binary { length }
263 | DataType::WChar { length }
264 | DataType::WVarchar { length }
265 | DataType::WLongVarchar { length }
266 | DataType::LongVarchar { length } => *length,
267 DataType::Float { precision, .. }
268 | DataType::Numeric { precision, .. }
269 | DataType::Decimal { precision, .. } => NonZeroUsize::new(*precision),
270 DataType::Other { column_size, .. } => *column_size,
271 }
272 }
273
274 /// Return the number of decimal digits as required to bind the data type as a parameter.
275 pub fn decimal_digits(&self) -> i16 {
276 match self {
277 DataType::Unknown
278 | DataType::Char { .. }
279 | DataType::Integer
280 | DataType::SmallInt
281 | DataType::Float { .. }
282 | DataType::Real
283 | DataType::Double
284 | DataType::Varchar { .. }
285 | DataType::WVarchar { .. }
286 | DataType::WChar { .. }
287 | DataType::Varbinary { .. }
288 | DataType::LongVarbinary { .. }
289 | DataType::Binary { .. }
290 | DataType::WLongVarchar { .. }
291 | DataType::LongVarchar { .. }
292 | DataType::Date
293 | DataType::BigInt
294 | DataType::TinyInt
295 | DataType::Bit => 0,
296 DataType::Numeric { scale, .. } | DataType::Decimal { scale, .. } => *scale,
297 DataType::Time { precision } | DataType::Timestamp { precision } => *precision,
298 DataType::Other { decimal_digits, .. } => *decimal_digits,
299 }
300 }
301
302 /// The maximum number of characters needed to display data in character form.
303 ///
304 /// See: <https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/display-size>
305 pub fn display_size(&self) -> Option<NonZeroUsize> {
306 match self {
307 DataType::Unknown
308 | DataType::Other {
309 data_type: _,
310 column_size: _,
311 decimal_digits: _,
312 } => None,
313 // Each binary byte is represented by a 2-digit hexadecimal number.
314 DataType::Varbinary { length }
315 | DataType::Binary { length }
316 | DataType::LongVarbinary { length } => {
317 length.map(|l| l.get() * 2).and_then(NonZeroUsize::new)
318 }
319 // The defined (for fixed types) or maximum (for variable types) number of characters
320 // needed to display the data in character form.
321 DataType::Varchar { length }
322 | DataType::WVarchar { length }
323 | DataType::WChar { length }
324 | DataType::Char { length }
325 | DataType::WLongVarchar { length }
326 | DataType::LongVarchar { length } => *length,
327 // The precision of the column plus 2 (a sign, precision digits, and a decimal point).
328 // For example, the display size of a column defined as NUMERIC(10,3) is 12.
329 DataType::Numeric {
330 precision,
331 scale: _,
332 }
333 | DataType::Decimal {
334 precision,
335 scale: _,
336 } => NonZeroUsize::new(precision + 2),
337 // 11 if signed (a sign and 10 digits) or 10 if unsigned (10 digits).
338 DataType::Integer => NonZeroUsize::new(11),
339 // 6 if signed (a sign and 5 digits) or 5 if unsigned (5 digits).
340 DataType::SmallInt => NonZeroUsize::new(6),
341 // 24 (a sign, 15 digits, a decimal point, the letter E, a sign, and 3 digits).
342 DataType::Float { .. } | DataType::Double => NonZeroUsize::new(24),
343 // 14 (a sign, 7 digits, a decimal point, the letter E, a sign, and 2 digits).
344 DataType::Real => NonZeroUsize::new(14),
345 // 10 (a date in the format yyyy-mm-dd).
346 DataType::Date => NonZeroUsize::new(10),
347 // 8 (a time in the format hh:mm:ss)
348 // or
349 // 9 + s (a time in the format hh:mm:ss[.fff...], where s is the fractional seconds
350 // precision).
351 DataType::Time { precision } => NonZeroUsize::new(if *precision == 0 {
352 8
353 } else {
354 9 + *precision as usize
355 }),
356 // 19 (for a timestamp in the yyyy-mm-dd hh:mm:ss format)
357 // or
358 // 20 + s (for a timestamp in the yyyy-mm-dd hh:mm:ss[.fff...] format, where s is the
359 // fractional seconds precision).
360 DataType::Timestamp { precision } => NonZeroUsize::new(if *precision == 0 {
361 19
362 } else {
363 20 + *precision as usize
364 }),
365 // 20 (a sign and 19 digits if signed or 20 digits if unsigned).
366 DataType::BigInt => NonZeroUsize::new(20),
367 // 4 if signed (a sign and 3 digits) or 3 if unsigned (3 digits).
368 DataType::TinyInt => NonZeroUsize::new(4),
369 // 1 digit.
370 DataType::Bit => NonZeroUsize::new(1),
371 }
372 }
373
374 /// The maximum length of the UTF-8 representation in bytes.
375 ///
376 /// ```
377 /// use odbc_api::DataType;
378 /// use std::num::NonZeroUsize;
379 ///
380 /// let nz = NonZeroUsize::new;
381 /// // Character set data types length is multiplied by four.
382 /// assert_eq!(DataType::Varchar { length: nz(10) }.utf8_len(), nz(40));
383 /// assert_eq!(DataType::Char { length: nz(10) }.utf8_len(), nz(40));
384 /// assert_eq!(DataType::WVarchar { length: nz(10) }.utf8_len(), nz(40));
385 /// assert_eq!(DataType::WChar { length: nz(10) }.utf8_len(), nz(40));
386 /// // For other types return value is identical to display size as they are assumed to be
387 /// // entirely representable with ASCII characters.
388 /// assert_eq!(DataType::Numeric { precision: 10, scale: 3}.utf8_len(), nz(10 + 2));
389 /// ```
390 pub fn utf8_len(&self) -> Option<NonZeroUsize> {
391 match self {
392 // One character may need up to four bytes to be represented in utf-8.
393 DataType::Varchar { length }
394 | DataType::WVarchar { length }
395 | DataType::WChar { length }
396 | DataType::Char { length } => length.map(|l| l.get() * 4).and_then(NonZeroUsize::new),
397 other => other.display_size(),
398 }
399 }
400
401 /// The maximum length of the UTF-16 representation in 2-Byte characters.
402 ///
403 /// ```
404 /// use odbc_api::DataType;
405 /// use std::num::NonZeroUsize;
406 ///
407 /// let nz = NonZeroUsize::new;
408 ///
409 /// // Character set data types length is multiplied by two.
410 /// assert_eq!(DataType::Varchar { length: nz(10) }.utf16_len(), nz(20));
411 /// assert_eq!(DataType::Char { length: nz(10) }.utf16_len(), nz(20));
412 /// assert_eq!(DataType::WVarchar { length: nz(10) }.utf16_len(), nz(20));
413 /// assert_eq!(DataType::WChar { length: nz(10) }.utf16_len(), nz(20));
414 /// // For other types return value is identical to display size as they are assumed to be
415 /// // entirely representable with ASCII characters.
416 /// assert_eq!(DataType::Numeric { precision: 10, scale: 3}.utf16_len(), nz(10 + 2));
417 /// ```
418 pub fn utf16_len(&self) -> Option<NonZeroUsize> {
419 match self {
420 // One character may need up to two u16 to be represented in utf-16.
421 DataType::Varchar { length }
422 | DataType::WVarchar { length }
423 | DataType::WChar { length }
424 | DataType::Char { length } => length.map(|l| l.get() * 2).and_then(NonZeroUsize::new),
425 other => other.display_size(),
426 }
427 }
428}