use super::impls::Constraint;
use super::impls::{BaseType, ReferentialAction, WrapVec};
use crate::types::Type;
pub fn primary() -> Type {
Type::new(BaseType::Primary)
.nullable(true) .increments(true) .primary(false) .unique(false) .indexed(false)
}
pub fn uuid() -> Type {
Type::new(BaseType::UUID)
.nullable(false)
.unique(true)
.indexed(true)
}
pub fn integer() -> Type {
Type::new(BaseType::Integer)
}
pub fn serial() -> Type {
Type::new(BaseType::Serial)
}
pub fn float() -> Type {
Type::new(BaseType::Float)
}
pub fn double() -> Type {
Type::new(BaseType::Double)
}
pub fn boolean() -> Type {
Type::new(BaseType::Boolean)
}
pub fn varchar(len: usize) -> Type {
Type::new(BaseType::Varchar(len))
}
pub fn r#char(len: usize) -> Type {
Type::new(BaseType::Char(len))
}
pub fn text() -> Type {
Type::new(BaseType::Text)
}
pub fn json() -> Type {
Type::new(BaseType::Json)
}
pub fn binary<'inner>() -> Type {
Type::new(BaseType::Binary)
}
pub fn foreign<S, I>(
table: S,
keys: I,
on_update: ReferentialAction,
on_delete: ReferentialAction,
) -> Type
where
S: Into<String>,
I: Into<WrapVec<String>>,
{
Type::new(BaseType::Foreign(
None,
table.into(),
keys.into(),
on_update,
on_delete,
))
}
pub fn foreign_schema<S, I>(
schema: S,
table: S,
keys: I,
on_update: ReferentialAction,
on_delete: ReferentialAction,
) -> Type
where
S: Into<String>,
I: Into<WrapVec<String>>,
{
Type::new(BaseType::Foreign(
Some(schema.into()),
table.into(),
keys.into(),
on_update,
on_delete,
))
}
pub fn custom(sql: &'static str) -> Type {
Type::new(BaseType::Custom(sql))
}
pub fn date() -> Type {
Type::new(BaseType::Date)
}
pub fn time() -> Type {
Type::new(BaseType::Time)
}
pub fn datetime() -> Type {
Type::new(BaseType::DateTime)
}
pub fn array(inner: &Type) -> Type {
Type::new(BaseType::Array(Box::new(inner.get_inner())))
}
pub fn index<I, S>(columns: I) -> Type
where
S: ToString,
I: IntoIterator<Item = S>,
{
let vec: Vec<String> = columns.into_iter().map(|s| s.to_string()).collect();
Type::new(BaseType::Index(vec))
}
pub fn unique_constraint<I, S>(columns: I) -> Type
where
S: ToString,
I: IntoIterator<Item = S>,
{
let vec: Vec<String> = columns.into_iter().map(|s| s.to_string()).collect();
Type::new(BaseType::Constraint(Constraint::Unique, vec))
}