forked from schemalex/schemalex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
178 lines (146 loc) · 3.27 KB
/
index.go
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package model
import (
"crypto/sha256"
"fmt"
)
// NewIndex creates a new index with the given index kind.
func NewIndex(kind IndexKind, table string) Index {
return &index{
kind: kind,
table: table,
}
}
func (stmt *index) ID() string {
// This is tricky. and index may or may not have a name. It would
// have been so much easier if we did, but we don't, so we'll fake
// something.
//
// In case we don't have a name, we need to know the table, the kind,
// the type, // the column(s), and the reference(s).
name := "index"
if stmt.HasName() {
name = name + "#" + stmt.Name()
}
h := sha256.New()
sym := "none"
if stmt.HasSymbol() {
sym = stmt.Symbol()
}
fmt.Fprintf(h,
"%s.%s.%s.%s",
stmt.table,
sym,
stmt.kind,
stmt.typ,
)
for col := range stmt.Columns() {
fmt.Fprintf(h, ".")
fmt.Fprintf(h, "%s", col.ID())
}
if stmt.reference != nil {
fmt.Fprintf(h, ".")
fmt.Fprintf(h, stmt.reference.ID())
}
return fmt.Sprintf("%s#%x", name, h.Sum(nil))
}
func (stmt *index) AddColumns(l ...IndexColumn) {
stmt.columns = append(stmt.columns, l...)
}
func (stmt *index) Columns() chan IndexColumn {
c := make(chan IndexColumn, len(stmt.columns))
for _, col := range stmt.columns {
c <- col
}
close(c)
return c
}
func (stmt *index) Reference() Reference {
return stmt.reference
}
func (stmt *index) Name() string {
return stmt.name.Value
}
func (stmt *index) Symbol() string {
return stmt.symbol.Value
}
func (stmt *index) HasName() bool {
return stmt.name.Valid
}
func (stmt *index) HasSymbol() bool {
return stmt.symbol.Valid
}
func (stmt *index) SetReference(r Reference) Index {
stmt.reference = r
return stmt
}
func (stmt *index) SetName(s string) Index {
stmt.name.Valid = true
stmt.name.Value = s
return stmt
}
func (stmt *index) SetSymbol(s string) Index {
stmt.symbol.Valid = true
stmt.symbol.Value = s
return stmt
}
func (stmt *index) SetType(typ IndexType) Index {
stmt.typ = typ
return stmt
}
func (stmt *index) IsBtree() bool {
return stmt.typ == IndexTypeBtree
}
func (stmt *index) IsHash() bool {
return stmt.typ == IndexTypeHash
}
func (stmt *index) IsPrimaryKey() bool {
return stmt.kind == IndexKindPrimaryKey
}
func (stmt *index) IsNormal() bool {
return stmt.kind == IndexKindNormal
}
func (stmt *index) IsUnique() bool {
return stmt.kind == IndexKindUnique
}
func (stmt *index) IsFullText() bool {
return stmt.kind == IndexKindFullText
}
func (stmt *index) IsSpatial() bool {
return stmt.kind == IndexKindSpatial
}
func (stmt *index) IsForeginKey() bool {
return stmt.kind == IndexKindForeignKey
}
func (stmt *index) Normalize() (Index, bool) {
return stmt, false
}
func (stmt *index) Clone() Index {
newindex := &index{}
*newindex = *stmt
return newindex
}
func NewIndexColumn(name string) IndexColumn {
return &indexColumn{
name: name,
}
}
func (col *indexColumn) ID() string {
if col.HasLength() {
return "index_column#" + col.Name() + "-" + col.Length()
}
return "index_column#" + col.Name()
}
func (col *indexColumn) Name() string {
return col.name
}
func (col *indexColumn) HasLength() bool {
return col.length.Valid
}
func (col *indexColumn) Length() string {
return col.length.Value
}
func (col *indexColumn) SetLength(s string) IndexColumn {
col.length.Valid = true
col.length.Value = s
return col
}