Skip to main content

sea_query/index/
drop.rs

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