use inherent::inherent;
use crate::{
backend::SchemaBuilder, types::*, ForeignKeyAction, SchemaStatementBuilder, TableForeignKey,
};
#[derive(Default, Debug, Clone)]
pub struct ForeignKeyCreateStatement {
pub(crate) foreign_key: TableForeignKey,
}
impl ForeignKeyCreateStatement {
pub fn new() -> Self {
Self::default()
}
pub fn name<T>(&mut self, name: T) -> &mut Self
where
T: Into<String>,
{
self.foreign_key.name(name);
self
}
pub fn from<T, C>(&mut self, table: T, columns: C) -> &mut Self
where
T: IntoTableRef,
C: IdenList,
{
self.foreign_key.from_tbl(table);
for col in columns.into_iter() {
self.foreign_key.from_col(col);
}
self
}
pub fn to<T, C>(&mut self, table: T, columns: C) -> &mut Self
where
T: IntoTableRef,
C: IdenList,
{
self.foreign_key.to_tbl(table);
for col in columns.into_iter() {
self.foreign_key.to_col(col);
}
self
}
pub fn from_tbl<T>(&mut self, table: T) -> &mut Self
where
T: IntoTableRef,
{
self.foreign_key.from_tbl(table);
self
}
pub fn to_tbl<R>(&mut self, ref_table: R) -> &mut Self
where
R: IntoTableRef,
{
self.foreign_key.to_tbl(ref_table);
self
}
pub fn from_col<T>(&mut self, column: T) -> &mut Self
where
T: IntoIden,
{
self.foreign_key.from_col(column);
self
}
pub fn to_col<R>(&mut self, ref_column: R) -> &mut Self
where
R: IntoIden,
{
self.foreign_key.to_col(ref_column);
self
}
pub fn on_delete(&mut self, action: ForeignKeyAction) -> &mut Self {
self.foreign_key.on_delete(action);
self
}
pub fn on_update(&mut self, action: ForeignKeyAction) -> &mut Self {
self.foreign_key.on_update(action);
self
}
pub fn get_foreign_key(&self) -> &TableForeignKey {
&self.foreign_key
}
pub fn take(&mut self) -> Self {
Self {
foreign_key: self.foreign_key.take(),
}
}
}
#[inherent]
impl SchemaStatementBuilder for ForeignKeyCreateStatement {
pub fn build<T: SchemaBuilder>(&self, schema_builder: T) -> String {
let mut sql = String::with_capacity(256);
schema_builder.prepare_foreign_key_create_statement(self, &mut sql);
sql
}
pub fn build_any(&self, schema_builder: &dyn SchemaBuilder) -> String {
let mut sql = String::with_capacity(256);
schema_builder.prepare_foreign_key_create_statement(self, &mut sql);
sql
}
pub fn to_string<T: SchemaBuilder>(&self, schema_builder: T) -> String;
}