sqlmo/query/
create_index.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use crate::{Dialect, ToSql};
use crate::util::SqlExtension;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IndexType {
    BTree,
    Hash,
    Gist,
    SpGist,
    Brin,
}

impl Default for IndexType {
    fn default() -> Self {
        IndexType::BTree
    }
}

/// Create index action for a table
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreateIndex {
    pub name: String,
    pub unique: bool,
    pub schema: Option<String>,
    pub table: String,
    pub columns: Vec<String>,
    pub type_: IndexType,
}

impl ToSql for CreateIndex {
    fn write_sql(&self, buf: &mut String, _: Dialect) {
        buf.push_str("CREATE ");
        if self.unique {
            buf.push_str("UNIQUE ");
        }
        buf.push_quoted(&self.name);
        buf.push_str(" ON ");
        buf.push_table_name(&self.schema, &self.table);
        buf.push_str(" USING ");
        match self.type_ {
            IndexType::BTree => buf.push_str("BTREE"),
            IndexType::Hash => buf.push_str("HASH"),
            IndexType::Gist => buf.push_str("GIST"),
            IndexType::SpGist => buf.push_str("SPGIST"),
            IndexType::Brin => buf.push_str("BRIN"),
        }
        buf.push_str(" (");
        buf.push_quoted_sequence(&self.columns, ", ");
        buf.push(')');
    }
}