#![allow(unsafe_code)]
use crate::dd::ffi;
use crate::sys::DdColumn;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(missing_docs)] pub enum ColumnType {
Decimal,
Tiny,
Short,
Long,
Float,
Double,
Null,
Timestamp,
LongLong,
Int24,
Date,
Time,
DateTime,
Year,
NewDate,
VarChar,
Bit,
Timestamp2,
DateTime2,
Time2,
NewDecimal,
Enum,
Set,
TinyBlob,
MediumBlob,
LongBlob,
Blob,
VarString,
String,
Geometry,
Json,
Unknown,
}
impl ColumnType {
#[must_use]
pub const fn from_raw(raw: i32) -> Self {
match raw {
1 => Self::Decimal,
2 => Self::Tiny,
3 => Self::Short,
4 => Self::Long,
5 => Self::Float,
6 => Self::Double,
7 => Self::Null,
8 => Self::Timestamp,
9 => Self::LongLong,
10 => Self::Int24,
11 => Self::Date,
12 => Self::Time,
13 => Self::DateTime,
14 => Self::Year,
15 => Self::NewDate,
16 => Self::VarChar,
17 => Self::Bit,
18 => Self::Timestamp2,
19 => Self::DateTime2,
20 => Self::Time2,
21 => Self::NewDecimal,
22 => Self::Enum,
23 => Self::Set,
24 => Self::TinyBlob,
25 => Self::MediumBlob,
26 => Self::LongBlob,
27 => Self::Blob,
28 => Self::VarString,
29 => Self::String,
30 => Self::Geometry,
31 => Self::Json,
_ => Self::Unknown,
}
}
}
impl DdColumn {
#[must_use]
pub fn name(&self) -> String {
let p: *const DdColumn = self;
ffi::read_name(|buf, cap| unsafe { ffi::mysql__DdColumn__name(p, buf, cap) })
}
#[must_use]
pub fn column_type(&self) -> ColumnType {
let p: *const DdColumn = self;
let raw = unsafe { ffi::mysql__DdColumn__type(p) };
ColumnType::from_raw(raw)
}
#[must_use]
pub fn is_nullable(&self) -> bool {
let p: *const DdColumn = self;
unsafe { ffi::mysql__DdColumn__is_nullable(p) }
}
#[must_use]
pub fn is_unsigned(&self) -> bool {
let p: *const DdColumn = self;
unsafe { ffi::mysql__DdColumn__is_unsigned(p) }
}
#[must_use]
pub fn char_length(&self) -> u32 {
let p: *const DdColumn = self;
unsafe { ffi::mysql__DdColumn__char_length(p) }
}
#[must_use]
pub fn is_hidden(&self) -> bool {
let p: *const DdColumn = self;
unsafe { ffi::mysql__DdColumn__is_hidden(p) }
}
#[must_use]
pub fn ordinal_position(&self) -> u32 {
let p: *const DdColumn = self;
unsafe { ffi::mysql__DdColumn__ordinal_position(p) }
}
}
#[cfg(test)]
mod tests {
use super::ColumnType;
#[test]
fn from_raw_maps_known_variants() {
assert_eq!(ColumnType::from_raw(2), ColumnType::Tiny);
assert_eq!(ColumnType::from_raw(4), ColumnType::Long);
assert_eq!(ColumnType::from_raw(9), ColumnType::LongLong);
assert_eq!(ColumnType::from_raw(16), ColumnType::VarChar);
assert_eq!(ColumnType::from_raw(31), ColumnType::Json);
}
#[test]
fn from_raw_returns_unknown_for_out_of_range() {
assert_eq!(ColumnType::from_raw(0), ColumnType::Unknown);
assert_eq!(ColumnType::from_raw(-1), ColumnType::Unknown);
assert_eq!(ColumnType::from_raw(99), ColumnType::Unknown);
}
}