use odbc_api::DataType;
use std::fmt::{Display, Formatter, Result as FmtResult};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OdbcTypeInfo {
data_type: DataType,
}
impl OdbcTypeInfo {
pub const fn new(data_type: DataType) -> Self {
Self { data_type }
}
pub const fn data_type(&self) -> DataType {
self.data_type
}
pub const BIGINT: Self = Self::new(DataType::BigInt);
pub const BIT: Self = Self::new(DataType::Bit);
pub const DATE: Self = Self::new(DataType::Date);
pub const DOUBLE: Self = Self::new(DataType::Double);
pub const INTEGER: Self = Self::new(DataType::Integer);
pub const REAL: Self = Self::new(DataType::Real);
pub const SMALLINT: Self = Self::new(DataType::SmallInt);
pub const TINYINT: Self = Self::new(DataType::TinyInt);
pub const UNKNOWN: Self = Self::new(DataType::Unknown);
pub const TIME: Self = Self::new(DataType::Time { precision: 0 });
pub const TIMESTAMP: Self = Self::new(DataType::Timestamp { precision: 0 });
pub const fn varchar(length: Option<std::num::NonZeroUsize>) -> Self {
Self::new(DataType::Varchar { length })
}
pub const fn varbinary(length: Option<std::num::NonZeroUsize>) -> Self {
Self::new(DataType::Varbinary { length })
}
pub const fn decimal(precision: usize, scale: i16) -> Self {
Self::new(DataType::Decimal { precision, scale })
}
pub const fn numeric(precision: usize, scale: i16) -> Self {
Self::new(DataType::Numeric { precision, scale })
}
}
impl sqlx_core::type_info::TypeInfo for OdbcTypeInfo {
fn is_null(&self) -> bool {
false
}
fn name(&self) -> &str {
match self.data_type {
DataType::BigInt => "BIGINT",
DataType::Binary { .. } => "BINARY",
DataType::Bit => "BIT",
DataType::Char { .. } => "CHAR",
DataType::Date => "DATE",
DataType::Decimal { .. } => "DECIMAL",
DataType::Double => "DOUBLE",
DataType::Float { .. } => "FLOAT",
DataType::Integer => "INTEGER",
DataType::LongVarbinary { .. } => "LONGVARBINARY",
DataType::LongVarchar { .. } => "LONGVARCHAR",
DataType::Numeric { .. } => "NUMERIC",
DataType::Real => "REAL",
DataType::SmallInt => "SMALLINT",
DataType::Time { .. } => "TIME",
DataType::Timestamp { .. } => "TIMESTAMP",
DataType::TinyInt => "TINYINT",
DataType::Varbinary { .. } => "VARBINARY",
DataType::Varchar { .. } => "VARCHAR",
DataType::WChar { .. } => "WCHAR",
DataType::WLongVarchar { .. } => "WLONGVARCHAR",
DataType::WVarchar { .. } => "WVARCHAR",
DataType::Unknown => "UNKNOWN",
DataType::Other { .. } => "OTHER",
}
}
}
pub trait DataTypeExt {
fn name(self) -> &'static str;
fn accepts_character_data(self) -> bool;
fn accepts_binary_data(self) -> bool;
fn accepts_numeric_data(self) -> bool;
}
impl DataTypeExt for DataType {
fn name(self) -> &'static str {
match self {
DataType::BigInt => "BIGINT",
DataType::Binary { .. } => "BINARY",
DataType::Bit => "BIT",
DataType::Char { .. } => "CHAR",
DataType::Date => "DATE",
DataType::Decimal { .. } => "DECIMAL",
DataType::Double => "DOUBLE",
DataType::Float { .. } => "FLOAT",
DataType::Integer => "INTEGER",
DataType::LongVarbinary { .. } => "LONGVARBINARY",
DataType::LongVarchar { .. } => "LONGVARCHAR",
DataType::Numeric { .. } => "NUMERIC",
DataType::Real => "REAL",
DataType::SmallInt => "SMALLINT",
DataType::Time { .. } => "TIME",
DataType::Timestamp { .. } => "TIMESTAMP",
DataType::TinyInt => "TINYINT",
DataType::Varbinary { .. } => "VARBINARY",
DataType::Varchar { .. } => "VARCHAR",
DataType::WChar { .. } => "WCHAR",
DataType::WLongVarchar { .. } => "WLONGVARCHAR",
DataType::WVarchar { .. } => "WVARCHAR",
DataType::Unknown => "UNKNOWN",
DataType::Other { .. } => "OTHER",
}
}
fn accepts_character_data(self) -> bool {
matches!(
self,
DataType::Char { .. }
| DataType::Varchar { .. }
| DataType::LongVarchar { .. }
| DataType::WChar { .. }
| DataType::WVarchar { .. }
| DataType::WLongVarchar { .. }
)
}
fn accepts_binary_data(self) -> bool {
matches!(
self,
DataType::Binary { .. } | DataType::Varbinary { .. } | DataType::LongVarbinary { .. }
)
}
fn accepts_numeric_data(self) -> bool {
matches!(
self,
DataType::TinyInt
| DataType::SmallInt
| DataType::Integer
| DataType::BigInt
| DataType::Real
| DataType::Float { .. }
| DataType::Double
| DataType::Decimal { .. }
| DataType::Numeric { .. }
)
}
}
impl Display for OdbcTypeInfo {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
f.write_str(sqlx_core::type_info::TypeInfo::name(self))
}
}