sql/schema/
index.rs

1use crate::{Dialect, ToSql};
2use crate::util::SqlExtension;
3
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum IndexKind {
6    BTree,
7    Hash,
8    Gist,
9    SpGist,
10    Brin,
11    Other(String),
12}
13
14impl Default for IndexKind {
15    fn default() -> Self {
16        IndexKind::BTree
17    }
18}
19
20/// Create index action for a table
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub struct Index {
23    pub name: String,
24    pub unique: bool,
25    pub schema: Option<String>,
26    pub table: String,
27    pub columns: Vec<String>,
28    pub kind: IndexKind,
29}
30
31impl ToSql for Index {
32    fn write_sql(&self, buf: &mut String, _: Dialect) {
33        buf.push_str("CREATE ");
34        if self.unique {
35            buf.push_str("UNIQUE ");
36        }
37        buf.push_quoted(&self.name);
38        buf.push_str(" ON ");
39        buf.push_table_name(&self.schema, &self.table);
40        buf.push_str(" USING ");
41        match &self.kind {
42            // btree is default
43            IndexKind::BTree => {}
44            IndexKind::Hash => buf.push_str("HASH"),
45            IndexKind::Gist => buf.push_str("GIST"),
46            IndexKind::SpGist => buf.push_str("SPGIST"),
47            IndexKind::Brin => buf.push_str("BRIN"),
48            IndexKind::Other(kind) => buf.push_str(kind),
49        }
50        buf.push_str(" (");
51        buf.push_quoted_sequence(&self.columns, ", ");
52        buf.push(')');
53    }
54}