#![warn(missing_docs)]
use std::fmt::{Debug, Display};
use crate::schema::{ColumnConstraint, ColumnValidators, DefaultValueEnum, GeneratedColumn};
#[derive(Clone, Debug)]
pub struct Column<T> {
pub(crate) name: &'static str,
default_value: Option<DefaultValueEnum<T>>,
table_name: &'static str,
comment: Option<&'static str>,
charset: Option<&'static str>,
collate: Option<&'static str>,
validators: Vec<ColumnValidators>,
constraints: Vec<ColumnConstraint>,
}
impl<T: Debug> Display for Column<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}.{}", self.table_name, self.name)
}
}
impl<T> Column<T> {
pub const fn new(name: &'static str, table_name: &'static str) -> Self {
Self {
name,
default_value: None,
table_name,
comment: None,
charset: None,
collate: None,
validators: Vec::new(),
constraints: Vec::new(),
}
}
pub fn default_value<K: Into<T>>(mut self, value: K) -> Self {
self.default_value = Some(DefaultValueEnum::Value(value.into()));
self
}
pub fn default_now(mut self) -> Self {
self.default_value = Some(DefaultValueEnum::CurrentTimestamp);
self
}
pub fn not_null(mut self) -> Self {
self.constraints.push(ColumnConstraint::NonNullable);
self
}
pub fn default_random(mut self) -> Self {
self.default_value = Some(DefaultValueEnum::Random);
self
}
pub fn email(mut self) -> Self {
self.validators.push(ColumnValidators::Email);
self
}
pub fn link(mut self) -> Self {
self.validators.push(ColumnValidators::Url);
self
}
pub fn min_len(mut self, min: i32) -> Self {
self.validators.push(ColumnValidators::MinLen(min as usize));
self
}
pub fn max_len(mut self, max: i32) -> Self {
self.validators.push(ColumnValidators::MaxLen(max as usize));
self
}
pub fn min(mut self, min: usize) -> Self {
self.validators.push(ColumnValidators::Min(min));
self
}
pub fn max(mut self, max: usize) -> Self {
self.validators.push(ColumnValidators::Max(max));
self
}
pub fn unique(mut self) -> Self {
self.constraints.push(ColumnConstraint::Unique);
self
}
pub fn primary_key(mut self) -> Self {
self.constraints.push(ColumnConstraint::PrimaryKey);
self
}
pub fn indexed(mut self) -> Self {
self.constraints.push(ColumnConstraint::Indexed);
self
}
pub fn auto_increment(mut self) -> Self {
self.constraints.push(ColumnConstraint::AutoIncrement);
self
}
pub fn comment(mut self, comment: &'static str) -> Self {
self.comment = Some(comment);
self
}
pub fn charset(mut self, charset: &'static str) -> Self {
self.charset = Some(charset);
self
}
pub fn collate(mut self, collate: &'static str) -> Self {
self.collate = Some(collate);
self
}
pub fn on_update_current_timestamp(mut self) -> Self {
self.constraints
.push(ColumnConstraint::OnUpdateCurrentTimestamp);
self
}
pub fn invisible(mut self) -> Self {
self.constraints.push(ColumnConstraint::Invisible);
self
}
pub fn check(mut self, expression: &'static str) -> Self {
self.constraints.push(ColumnConstraint::Check(expression));
self
}
pub fn generated_virtual(mut self, expression: &'static str) -> Self {
self.constraints
.push(ColumnConstraint::Generated(GeneratedColumn::Virtual(
expression,
)));
self
}
pub fn generated_stored(mut self, expression: &'static str) -> Self {
self.constraints
.push(ColumnConstraint::Generated(GeneratedColumn::Stored(
expression,
)));
self
}
#[doc(hidden)]
pub fn __internal_name(&self) -> &'static str {
self.name
}
#[doc(hidden)]
pub fn __internal_table_name(&self) -> &'static str {
self.table_name
}
#[doc(hidden)]
pub fn __internal_get_default(&self) -> Option<&DefaultValueEnum<T>> {
self.default_value.as_ref()
}
#[doc(hidden)]
pub fn __internal_get_validators(&self) -> &Vec<ColumnValidators> {
return self.validators.as_ref();
}
#[doc(hidden)]
pub fn __internal_get_constraints(&self) -> &Vec<ColumnConstraint> {
return self.constraints.as_ref();
}
#[doc(hidden)]
pub fn __internal_get_comment(&self) -> Option<&'static str> {
self.comment
}
#[doc(hidden)]
pub fn __internal_get_charset(&self) -> Option<&'static str> {
self.charset
}
#[doc(hidden)]
pub fn __internal_get_collate(&self) -> Option<&'static str> {
self.collate
}
}