use crate::{types::*, value::*};
#[derive(Debug, Clone)]
pub struct ColumnDef {
pub(crate) table: Option<TableRef>,
pub(crate) name: DynIden,
pub(crate) types: Option<ColumnType>,
pub(crate) spec: Vec<ColumnSpec>,
}
#[non_exhaustive]
#[derive(Debug, Clone)]
pub enum ColumnType {
Char(Option<u32>),
String(Option<u32>),
Text,
TinyInteger(Option<u32>),
SmallInteger(Option<u32>),
Integer(Option<u32>),
BigInteger(Option<u32>),
TinyUnsigned(Option<u32>),
SmallUnsigned(Option<u32>),
Unsigned(Option<u32>),
BigUnsigned(Option<u32>),
Float(Option<u32>),
Double(Option<u32>),
Decimal(Option<(u32, u32)>),
DateTime(Option<u32>),
Timestamp(Option<u32>),
TimestampWithTimeZone(Option<u32>),
Time(Option<u32>),
Date,
Interval(Option<PgInterval>, Option<u32>),
Binary(Option<u32>),
Boolean,
Money(Option<(u32, u32)>),
Json,
JsonBinary,
Uuid,
Custom(DynIden),
Enum(String, Vec<String>),
Array(Option<String>),
}
#[derive(Debug, Clone)]
pub enum ColumnSpec {
Null,
NotNull,
Default(Value),
AutoIncrement,
UniqueKey,
PrimaryKey,
Extra(String),
}
#[derive(Debug, Clone)]
pub enum PgInterval {
Year,
Month,
Day,
Hour,
Minute,
Second,
YearToMonth,
DayToHour,
DayToMinute,
DayToSecond,
HourToMinute,
HourToSecond,
MinuteToSecond,
}
#[cfg(feature = "postgres-interval")]
impl quote::ToTokens for PgInterval {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
use quote::{quote, TokenStreamExt};
tokens.append_all(match self {
PgInterval::Year => quote! { PgInterval::Year },
PgInterval::Month => quote! { PgInterval::Month },
PgInterval::Day => quote! { PgInterval::Day },
PgInterval::Hour => quote! { PgInterval::Hour },
PgInterval::Minute => quote! { PgInterval::Minute },
PgInterval::Second => quote! { PgInterval::Second },
PgInterval::YearToMonth => quote! { PgInterval::YearToMonth },
PgInterval::DayToHour => quote! { PgInterval::DayToHour },
PgInterval::DayToMinute => quote! { PgInterval::DayToMinute },
PgInterval::DayToSecond => quote! { PgInterval::DayToSecond },
PgInterval::HourToMinute => quote! { PgInterval::HourToMinute },
PgInterval::HourToSecond => quote! { PgInterval::HourToSecond },
PgInterval::MinuteToSecond => quote! { PgInterval::MinuteToSecond },
});
}
}
impl ColumnDef {
pub fn new<T: 'static>(name: T) -> Self
where
T: Iden,
{
Self {
table: None,
name: SeaRc::new(name),
types: None,
spec: Vec::new(),
}
}
pub fn new_with_type<T: 'static>(name: T, types: ColumnType) -> Self
where
T: Iden,
{
Self {
table: None,
name: SeaRc::new(name),
types: Some(types),
spec: Vec::new(),
}
}
pub fn not_null(&mut self) -> &mut Self {
self.spec.push(ColumnSpec::NotNull);
self
}
pub fn default<T>(&mut self, value: T) -> &mut Self
where
T: Into<Value>,
{
self.spec.push(ColumnSpec::Default(value.into()));
self
}
pub fn auto_increment(&mut self) -> &mut Self {
self.spec.push(ColumnSpec::AutoIncrement);
self
}
pub fn unique_key(&mut self) -> &mut Self {
self.spec.push(ColumnSpec::UniqueKey);
self
}
pub fn primary_key(&mut self) -> &mut Self {
self.spec.push(ColumnSpec::PrimaryKey);
self
}
pub fn char_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::Char(Some(length)));
self
}
pub fn char(&mut self) -> &mut Self {
self.types = Some(ColumnType::Char(None));
self
}
pub fn string_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::String(Some(length)));
self
}
pub fn string(&mut self) -> &mut Self {
self.types = Some(ColumnType::String(None));
self
}
pub fn text(&mut self) -> &mut Self {
self.types = Some(ColumnType::Text);
self
}
pub fn tiny_integer_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::TinyInteger(Some(length)));
self
}
pub fn tiny_integer(&mut self) -> &mut Self {
self.types = Some(ColumnType::TinyInteger(None));
self
}
pub fn small_integer_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::SmallInteger(Some(length)));
self
}
pub fn small_integer(&mut self) -> &mut Self {
self.types = Some(ColumnType::SmallInteger(None));
self
}
pub fn integer_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::Integer(Some(length)));
self
}
pub fn integer(&mut self) -> &mut Self {
self.types = Some(ColumnType::Integer(None));
self
}
pub fn big_integer_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::BigInteger(Some(length)));
self
}
pub fn big_integer(&mut self) -> &mut Self {
self.types = Some(ColumnType::BigInteger(None));
self
}
pub fn tiny_unsigned_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::TinyUnsigned(Some(length)));
self
}
pub fn tiny_unsigned(&mut self) -> &mut Self {
self.types = Some(ColumnType::TinyUnsigned(None));
self
}
pub fn small_unsigned_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::SmallUnsigned(Some(length)));
self
}
pub fn small_unsigned(&mut self) -> &mut Self {
self.types = Some(ColumnType::SmallUnsigned(None));
self
}
pub fn unsigned_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::Unsigned(Some(length)));
self
}
pub fn unsigned(&mut self) -> &mut Self {
self.types = Some(ColumnType::Unsigned(None));
self
}
pub fn big_unsigned_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::BigUnsigned(Some(length)));
self
}
pub fn big_unsigned(&mut self) -> &mut Self {
self.types = Some(ColumnType::BigUnsigned(None));
self
}
pub fn float_len(&mut self, precision: u32) -> &mut Self {
self.types = Some(ColumnType::Float(Some(precision)));
self
}
pub fn float(&mut self) -> &mut Self {
self.types = Some(ColumnType::Float(None));
self
}
pub fn double_len(&mut self, precision: u32) -> &mut Self {
self.types = Some(ColumnType::Double(Some(precision)));
self
}
pub fn double(&mut self) -> &mut Self {
self.types = Some(ColumnType::Double(None));
self
}
pub fn decimal_len(&mut self, precision: u32, scale: u32) -> &mut Self {
self.types = Some(ColumnType::Decimal(Some((precision, scale))));
self
}
pub fn decimal(&mut self) -> &mut Self {
self.types = Some(ColumnType::Decimal(None));
self
}
pub fn date_time_len(&mut self, precision: u32) -> &mut Self {
self.types = Some(ColumnType::DateTime(Some(precision)));
self
}
pub fn date_time(&mut self) -> &mut Self {
self.types = Some(ColumnType::DateTime(None));
self
}
#[cfg(feature = "backend-postgres")]
pub fn interval(&mut self, fields: Option<PgInterval>, precision: Option<u32>) -> &mut Self {
self.types = Some(ColumnType::Interval(fields, precision));
self
}
pub fn timestamp_len(&mut self, precision: u32) -> &mut Self {
self.types = Some(ColumnType::Timestamp(Some(precision)));
self
}
pub fn timestamp(&mut self) -> &mut Self {
self.types = Some(ColumnType::Timestamp(None));
self
}
pub fn timestamp_with_time_zone(&mut self) -> &mut Self {
self.types = Some(ColumnType::TimestampWithTimeZone(None));
self
}
pub fn timestamp_with_time_zone_len(&mut self, precision: u32) -> &mut Self {
self.types = Some(ColumnType::TimestampWithTimeZone(Some(precision)));
self
}
pub fn time_len(&mut self, precision: u32) -> &mut Self {
self.types = Some(ColumnType::Time(Some(precision)));
self
}
pub fn time(&mut self) -> &mut Self {
self.types = Some(ColumnType::Time(None));
self
}
pub fn date(&mut self) -> &mut Self {
self.types = Some(ColumnType::Date);
self
}
pub fn binary_len(&mut self, length: u32) -> &mut Self {
self.types = Some(ColumnType::Binary(Some(length)));
self
}
pub fn binary(&mut self) -> &mut Self {
self.types = Some(ColumnType::Binary(None));
self
}
pub fn boolean(&mut self) -> &mut Self {
self.types = Some(ColumnType::Boolean);
self
}
pub fn money_len(&mut self, precision: u32, scale: u32) -> &mut Self {
self.types = Some(ColumnType::Money(Some((precision, scale))));
self
}
pub fn money(&mut self) -> &mut Self {
self.types = Some(ColumnType::Money(None));
self
}
pub fn json(&mut self) -> &mut Self {
self.types = Some(ColumnType::Json);
self
}
pub fn json_binary(&mut self) -> &mut Self {
self.types = Some(ColumnType::JsonBinary);
self
}
pub fn uuid(&mut self) -> &mut Self {
self.types = Some(ColumnType::Uuid);
self
}
pub fn custom<T: 'static>(&mut self, n: T) -> &mut Self
where
T: Iden,
{
self.types = Some(ColumnType::Custom(SeaRc::new(n)));
self
}
pub fn enumeration<N, S, V>(&mut self, name: N, variants: V) -> &mut Self
where
N: ToString,
S: ToString,
V: IntoIterator<Item = S>,
{
self.types = Some(ColumnType::Enum(
name.to_string(),
variants.into_iter().map(|v| v.to_string()).collect(),
));
self
}
pub fn array(&mut self, elem_type: String) -> &mut Self {
self.types = Some(ColumnType::Array(Some(elem_type)));
self
}
pub fn extra(&mut self, string: String) -> &mut Self {
self.spec.push(ColumnSpec::Extra(string));
self
}
pub fn get_column_name(&self) -> String {
self.name.to_string()
}
pub fn get_column_type(&self) -> Option<&ColumnType> {
self.types.as_ref()
}
pub fn get_column_spec(&self) -> &Vec<ColumnSpec> {
self.spec.as_ref()
}
pub fn take(&mut self) -> Self {
Self {
table: self.table.take(),
name: std::mem::replace(&mut self.name, SeaRc::new(NullAlias::new())),
types: self.types.take(),
spec: std::mem::take(&mut self.spec),
}
}
}