sea_query/index/
drop.rs

1use inherent::inherent;
2
3use crate::{backend::SchemaBuilder, types::*, SchemaStatementBuilder, TableIndex};
4
5/// Drop an index for an existing table
6///
7/// # Examples
8///
9/// ```
10/// use sea_query::{tests_cfg::*, *};
11///
12/// let index = Index::drop()
13///     .name("idx-glyph-aspect")
14///     .table(Glyph::Table)
15///     .to_owned();
16///
17/// assert_eq!(
18///     index.to_string(MysqlQueryBuilder),
19///     r#"DROP INDEX `idx-glyph-aspect` ON `glyph`"#
20/// );
21/// assert_eq!(
22///     index.to_string(PostgresQueryBuilder),
23///     r#"DROP INDEX "idx-glyph-aspect""#
24/// );
25/// assert_eq!(
26///     index.to_string(SqliteQueryBuilder),
27///     r#"DROP INDEX "idx-glyph-aspect""#
28/// );
29/// ```
30#[derive(Default, Debug, Clone)]
31pub struct IndexDropStatement {
32    pub(crate) table: Option<TableRef>,
33    pub(crate) index: TableIndex,
34    pub(crate) if_exists: bool,
35}
36
37impl IndexDropStatement {
38    /// Construct a new [`IndexDropStatement`]
39    pub fn new() -> Self {
40        Self::default()
41    }
42
43    /// Set index name
44    pub fn name<T>(&mut self, name: T) -> &mut Self
45    where
46        T: Into<String>,
47    {
48        self.index.name(name);
49        self
50    }
51
52    /// Set target table
53    pub fn table<T>(&mut self, table: T) -> &mut Self
54    where
55        T: IntoTableRef,
56    {
57        self.table = Some(table.into_table_ref());
58        self
59    }
60
61    pub fn if_exists(&mut self) -> &mut Self {
62        self.if_exists = true;
63        self
64    }
65}
66
67#[inherent]
68impl SchemaStatementBuilder for IndexDropStatement {
69    pub fn build<T: SchemaBuilder>(&self, schema_builder: T) -> String {
70        let mut sql = String::with_capacity(256);
71        schema_builder.prepare_index_drop_statement(self, &mut sql);
72        sql
73    }
74
75    pub fn build_any(&self, schema_builder: &dyn SchemaBuilder) -> String {
76        let mut sql = String::with_capacity(256);
77        schema_builder.prepare_index_drop_statement(self, &mut sql);
78        sql
79    }
80
81    pub fn to_string<T: SchemaBuilder>(&self, schema_builder: T) -> String;
82}