mod column_type;
pub use column_type::{ColumnType, FromDataError};
mod data;
pub use data::ColumnData;
#[derive(Debug, Clone, PartialEq)]
pub struct Column {
pub name: &'static str,
pub kind: ColumnKind,
pub index: IndexKind,
}
impl Column {
pub fn new<T>(
name: &'static str,
len: Option<usize>,
index: IndexKind,
) -> Self
where
T: ColumnType,
{
let mut kind = T::column_kind();
if let Some(len) = len {
kind = match kind {
ColumnKind::Text => ColumnKind::Varchar(len),
_ => panic!(
"column kind {:?} doenst support len attribute",
kind
),
};
}
Self { name, kind, index }
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ColumnKind {
Boolean,
Varchar(usize),
FixedText(usize),
Text,
Date,
Timestamp,
F64,
F32,
I64,
I32,
I16,
Option(Box<ColumnKind>),
TextArray,
Bytea,
Json,
}
impl ColumnKind {
pub fn short(&self) -> &'static str {
match self {
Self::Boolean => "boolean",
Self::Varchar(_) => "varchar",
Self::FixedText(_) => "text",
Self::Text => "text",
Self::Date => "date",
Self::Timestamp => "timestamp",
Self::F64 => "float8",
Self::F32 => "float4",
Self::I64 => "int8",
Self::I32 => "int4",
Self::I16 => "int2",
Self::Option(t) => t.short(),
Self::TextArray => "text []",
Self::Bytea => "bytea",
Self::Json => "json",
}
}
pub fn value(&self, name: &str) -> String {
match self {
Self::Varchar(v) => format!("({})", v),
Self::FixedText(v) => format!(" CHECK (length({})={})", name, v),
Self::Option(t) => t.value(name),
_ => String::new(),
}
}
pub fn to_string(&self, name: &str) -> String {
format!("{}{}", self.short(), self.value(name))
}
pub fn not_null_str(&self) -> &'static str {
match self {
Self::Option(_) => "null",
_ => "not null",
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum IndexKind {
Primary,
Unique,
NamedUnique(&'static str),
Index,
None,
}
impl IndexKind {
pub fn is_none(&self) -> bool {
matches!(self, Self::None)
}
}