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
52
53
54
55
56
57
58
use crate::{Dialect, ToSql};
use crate::util::SqlExtension;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum IndexType {
BTree,
Hash,
Gist,
SpGist,
Brin,
}
impl Default for IndexType {
fn default() -> Self {
IndexType::BTree
}
}
#[derive(Debug)]
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, None);
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(" (");
let mut first = true;
for column in &self.columns {
if !first {
buf.push(',');
}
buf.push_quoted(column);
first = false;
}
buf.push(')');
}
}