sea_query/index/
drop.rs

1use inherent::inherent;
2
3use crate::{SchemaStatementBuilder, TableIndex, backend::SchemaBuilder, types::*};
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    pub(crate) concurrently: bool,
36}
37
38impl IndexDropStatement {
39    /// Construct a new [`IndexDropStatement`]
40    pub fn new() -> Self {
41        Self::default()
42    }
43
44    /// Set index name
45    pub fn name<T>(&mut self, name: T) -> &mut Self
46    where
47        T: Into<String>,
48    {
49        self.index.name(name);
50        self
51    }
52
53    /// Set target table
54    pub fn table<T>(&mut self, table: T) -> &mut Self
55    where
56        T: IntoTableRef,
57    {
58        self.table = Some(table.into_table_ref());
59        self
60    }
61
62    pub fn if_exists(&mut self) -> &mut Self {
63        self.if_exists = true;
64        self
65    }
66
67    /// Set index to be dropped concurrently. Only available on Postgres.
68    pub fn concurrently(&mut self) -> &mut Self {
69        self.concurrently = true;
70        self
71    }
72}
73
74#[inherent]
75impl SchemaStatementBuilder for IndexDropStatement {
76    pub fn build<T>(&self, schema_builder: T) -> String
77    where
78        T: SchemaBuilder,
79    {
80        let mut sql = String::with_capacity(256);
81        schema_builder.prepare_index_drop_statement(self, &mut sql);
82        sql
83    }
84
85    pub fn to_string<T>(&self, schema_builder: T) -> String
86    where
87        T: SchemaBuilder;
88}