forked from yunionio/sqlchemy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.go
More file actions
62 lines (53 loc) · 1.2 KB
/
index.go
File metadata and controls
62 lines (53 loc) · 1.2 KB
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
59
60
61
62
package sqlchemy
import (
"fmt"
"strings"
)
type STableIndex struct {
name string
columns []string
isUnique bool
}
type TColumnNames []string
func (cols TColumnNames) Len() int {
return len(cols)
}
func (cols TColumnNames) Swap(i, j int) {
cols[i], cols[j] = cols[j], cols[i]
}
func (cols TColumnNames) Less(i, j int) bool {
if strings.Compare(cols[i], cols[j]) < 0 {
return true
} else {
return false
}
}
func (index *STableIndex) IsIdentical(cols ...string) bool {
if len(index.columns) != len(cols) {
return false
}
for i := 0; i < len(index.columns); i += 1 {
if index.columns[i] != cols[i] {
return false
}
}
return true
}
func (index *STableIndex) QuotedColumns() []string {
ret := make([]string, len(index.columns))
for i := 0; i < len(ret); i += 1 {
ret[i] = fmt.Sprintf("`%s`", index.columns[i])
}
return ret
}
func (ts *STableSpec) AddIndex(unique bool, cols ...string) bool {
for i := 0; i < len(ts.indexes); i += 1 {
if ts.indexes[i].IsIdentical(cols...) {
return false
}
}
name := fmt.Sprintf("ix_%s_%s", ts.name, strings.Join(cols, "_"))
idx := STableIndex{name: name, columns: cols, isUnique: unique}
ts.indexes = append(ts.indexes, idx)
return true
}