sea_query/foreign_key/
drop.rs

1use inherent::inherent;
2
3use crate::{backend::SchemaBuilder, types::*, SchemaStatementBuilder, TableForeignKey};
4
5/// Drop a foreign key constraint for an existing table
6///
7/// # Examples
8///
9/// ```
10/// use sea_query::{tests_cfg::*, *};
11///
12/// let foreign_key = ForeignKey::drop()
13///     .name("FK_character_font")
14///     .table(Char::Table)
15///     .to_owned();
16///
17/// assert_eq!(
18///     foreign_key.to_string(MysqlQueryBuilder),
19///     r#"ALTER TABLE `character` DROP FOREIGN KEY `FK_character_font`"#
20/// );
21/// assert_eq!(
22///     foreign_key.to_string(PostgresQueryBuilder),
23///     r#"ALTER TABLE "character" DROP CONSTRAINT "FK_character_font""#
24/// );
25/// // Sqlite does not support modification of foreign key constraints to existing tables
26/// ```
27#[derive(Default, Debug, Clone)]
28pub struct ForeignKeyDropStatement {
29    pub(crate) foreign_key: TableForeignKey,
30    pub(crate) table: Option<TableRef>,
31}
32
33impl ForeignKeyDropStatement {
34    /// Construct a new [`ForeignKeyDropStatement`]
35    pub fn new() -> Self {
36        Self::default()
37    }
38
39    /// Set foreign key name
40    pub fn name<T>(&mut self, name: T) -> &mut Self
41    where
42        T: Into<String>,
43    {
44        self.foreign_key.name(name);
45        self
46    }
47
48    /// Set key table and referencing table
49    pub fn table<T>(&mut self, table: T) -> &mut Self
50    where
51        T: IntoTableRef,
52    {
53        self.table = Some(table.into_table_ref());
54        self
55    }
56}
57
58#[inherent]
59impl SchemaStatementBuilder for ForeignKeyDropStatement {
60    pub fn build<T: SchemaBuilder>(&self, schema_builder: T) -> String {
61        let mut sql = String::with_capacity(256);
62        schema_builder.prepare_foreign_key_drop_statement(self, &mut sql);
63        sql
64    }
65
66    pub fn build_any(&self, schema_builder: &dyn SchemaBuilder) -> String {
67        let mut sql = String::with_capacity(256);
68        schema_builder.prepare_foreign_key_drop_statement(self, &mut sql);
69        sql
70    }
71
72    pub fn to_string<T: SchemaBuilder>(&self, schema_builder: T) -> String;
73}