odbc_api::handles

Enum DataType

Source
pub enum DataType {
Show 23 variants Unknown, Char { length: Option<NonZeroUsize>, }, WChar { length: Option<NonZeroUsize>, }, Numeric { precision: usize, scale: i16, }, Decimal { precision: usize, scale: i16, }, Integer, SmallInt, Float { precision: usize, }, Real, Double, Varchar { length: Option<NonZeroUsize>, }, WVarchar { length: Option<NonZeroUsize>, }, LongVarchar { length: Option<NonZeroUsize>, }, LongVarbinary { length: Option<NonZeroUsize>, }, Date, Time { precision: i16, }, Timestamp { precision: i16, }, BigInt, TinyInt, Bit, Varbinary { length: Option<NonZeroUsize>, }, Binary { length: Option<NonZeroUsize>, }, Other { data_type: SqlDataType, column_size: Option<NonZeroUsize>, decimal_digits: i16, },
}
Expand description

The relational type of the column. Think of it as the type used in the CREATE TABLE statement then creating the database.

There might be a mismatch between the types supported by your database and the types defined in ODBC. E.g. ODBC does not have a timestamp with timezone type, theras Postgersql and Microsoft SQL Server both have one. In such cases it is up to the specific ODBC driver what happens. Microsoft SQL Server return a custom type, with its meaning specific to that driver. PostgreSQL identifies that column as an ordinary ODBC timestamp. Enumeration over valid SQL Data Types supported by ODBC

Variants§

§

Unknown

The type is not known.

§

Char

Char(n). Character string of fixed length.

Fields

§length: Option<NonZeroUsize>

Column size in characters (excluding terminating zero).

§

WChar

NChar(n). Character string of fixed length.

Fields

§length: Option<NonZeroUsize>

Column size in characters (excluding terminating zero).

§

Numeric

`Numeric(p,s). Signed, exact, numeric value with a precision p and scale s (1 <= p <= 15; s <= p)

Fields

§precision: usize

Total number of digits.

§scale: i16

Number of decimal digits.

§

Decimal

Decimal(p,s). Signed, exact, numeric value with a precision of at least p and scale s. The maximum precision is driver-defined. (1 <= p <= 15; s <= p)

Fields

§precision: usize

Total number of digits.

§scale: i16

Number of decimal digits.

§

Integer

Integer. 32 Bit Integer

§

SmallInt

Smallint. 16 Bit Integer

§

Float

Float(p). Signed, approximate, numeric value with a binary precision of at least p. The maximum precision is driver-defined.

Depending on the implementation binary precision is either 24 (f32) or 53 (f64).

Fields

§precision: usize
§

Real

Real. Signed, approximate, numeric value with a binary precision 24 (zero or absolute value 10^-38] to 10^38).

§

Double

Double Precision. Signed, approximate, numeric value with a binary precision 53 (zero or absolute value 10^-308 to 10^308).

§

Varchar

Varchar(n). Variable length character string.

Fields

§length: Option<NonZeroUsize>

Maximum length of the character string (excluding terminating zero). Whether this length is to be interpreted as bytes or Codepoints is ambigious and depends on the datasource.

E.g. For Microsoft SQL Server this is the binary length, theras for a MariaDB this refers to codepoints in case of UTF-8 encoding. If you need the binary size query the octet length for that column instead.

To find out how to interpret this value for a particular datasource you can use the odbcsv command line tool list-columns subcommand and query a Varchar column. If the buffer/octet length matches the column size, you can interpret this as the byte length.

§

WVarchar

NVARCHAR(n). Variable length character string. Indicates the use of wide character strings and use of UCS2 encoding on the side of the database.

Fields

§length: Option<NonZeroUsize>

Maximum length of the character string (excluding terminating zero).

§

LongVarchar

TEXT. Variable length characeter string for long text objects.

Fields

§length: Option<NonZeroUsize>

Maximum length of the character string (excluding terminating zero). Maximum size depends on the capabilities of the driver and datasource. E.g. its 2^31 - 1 for MSSQL.

§

LongVarbinary

BLOB. Variable length data for long binary objects.

Fields

§length: Option<NonZeroUsize>

Maximum length of the binary data. Maximum size depends on the capabilities of the driver and datasource.

§

Date

Date. Year, month, and day fields, conforming to the rules of the Gregorian calendar.

§

Time

Time. Hour, minute, and second fields, with valid values for hours of 00 to 23, valid values for minutes of 00 to 59, and valid values for seconds of 00 to 61. Precision p indicates the seconds precision.

Fields

§precision: i16

Number of radix ten digits used to represent the timestamp after the decimal points. E.g. Milliseconds would be represented by precision 3, Microseconds by 6 and Nanoseconds by 9.

§

Timestamp

Timestamp. Year, month, day, hour, minute, and second fields, with valid values as defined for the Date and Time variants.

Fields

§precision: i16

Number of radix ten digits used to represent the timestamp after the decimal points. E.g. Milliseconds would be represented by precision 3, Microseconds by 6 and Nanoseconds by 9.

§

BigInt

BIGINT. Exact numeric value with precision 19 (if signed) or 20 (if unsigned) and scale 0 (signed: -2^63 <= n <= 2^63 - 1, unsigned: 0 <= n <= 2^64 - 1). Has no corresponding type in SQL-92.

§

TinyInt

TINYINT. Exact numeric value with precision 3 and scale 0 (signed: -128 <= n <= 127, unsigned: 0 <= n <= 255)

§

Bit

BIT. Single bit binary data.

§

Varbinary

VARBINARY(n). Type for variable sized binary data.

Fields

§

Binary

BINARY(n). Type for fixed sized binary data.

Fields

§

Other

The driver returned a type, but it is not among the other types of these enumeration. This is a catchall, in case the library is incomplete, or the data source supports custom or non-standard types.

Fields

§data_type: SqlDataType

Type of the column

§column_size: Option<NonZeroUsize>

Size of column element

§decimal_digits: i16

Decimal digits returned for the column element. Exact meaning if any depends on the data_type field.

Implementations§

Source§

impl DataType

Source

pub fn new( data_type: SqlDataType, column_size: usize, decimal_digits: i16, ) -> Self

This constructor is useful to create an instance of the enumeration using values returned by ODBC Api calls like SQLDescribeCol, rather than just initializing a variant directly.

Source

pub fn data_type(&self) -> SqlDataType

The associated data_type discriminator for this variant.

Source

pub fn column_size(&self) -> Option<NonZeroUsize>

Source

pub fn decimal_digits(&self) -> i16

Return the number of decimal digits as required to bind the data type as a parameter.

Source

pub fn display_size(&self) -> Option<NonZeroUsize>

The maximum number of characters needed to display data in character form.

See: https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/display-size

Source

pub fn utf8_len(&self) -> Option<NonZeroUsize>

The maximum length of the UTF-8 representation in bytes.

use odbc_api::DataType;
use std::num::NonZeroUsize;

let nz = NonZeroUsize::new;
// Character set data types length is multiplied by four.
assert_eq!(DataType::Varchar { length: nz(10) }.utf8_len(), nz(40));
assert_eq!(DataType::Char { length: nz(10) }.utf8_len(), nz(40));
assert_eq!(DataType::WVarchar { length: nz(10) }.utf8_len(), nz(40));
assert_eq!(DataType::WChar { length: nz(10) }.utf8_len(), nz(40));
// For other types return value is identical to display size as they are assumed to be
// entirely representable with ASCII characters.
assert_eq!(DataType::Numeric { precision: 10, scale: 3}.utf8_len(), nz(10 + 2));
Source

pub fn utf16_len(&self) -> Option<NonZeroUsize>

The maximum length of the UTF-16 representation in 2-Byte characters.

use odbc_api::DataType;
use std::num::NonZeroUsize;

let nz = NonZeroUsize::new;

// Character set data types length is multiplied by two.
assert_eq!(DataType::Varchar { length: nz(10) }.utf16_len(), nz(20));
assert_eq!(DataType::Char { length: nz(10) }.utf16_len(), nz(20));
assert_eq!(DataType::WVarchar { length: nz(10) }.utf16_len(), nz(20));
assert_eq!(DataType::WChar { length: nz(10) }.utf16_len(), nz(20));
// For other types return value is identical to display size as they are assumed to be
// entirely representable with ASCII characters.
assert_eq!(DataType::Numeric { precision: 10, scale: 3}.utf16_len(), nz(10 + 2));

Trait Implementations§

Source§

impl Clone for DataType

Source§

fn clone(&self) -> DataType

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DataType

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for DataType

Source§

fn default() -> DataType

Returns the “default value” for a type. Read more
Source§

impl PartialEq for DataType

Source§

fn eq(&self, other: &DataType) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for DataType

Source§

impl Eq for DataType

Source§

impl StructuralPartialEq for DataType

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.