2020-02-21 18:51:38 +03:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Index struct {
|
2020-02-21 19:02:05 +03:00
|
|
|
Name string
|
|
|
|
Class string // UNIQUE | FULLTEXT | SPATIAL
|
|
|
|
Type string // btree, hash, gist, spgist, gin, and brin
|
|
|
|
Where string
|
|
|
|
Comment string
|
|
|
|
Fields []IndexOption
|
2020-02-21 18:51:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type IndexOption struct {
|
|
|
|
*Field
|
|
|
|
Expression string
|
|
|
|
Sort string // DESC, ASC
|
|
|
|
Collate string
|
|
|
|
Length int
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseIndexes parse schema indexes
|
|
|
|
func (schema *Schema) ParseIndexes() map[string]Index {
|
|
|
|
var indexes = map[string]Index{}
|
|
|
|
|
2020-04-03 02:57:52 +03:00
|
|
|
for _, field := range schema.Fields {
|
2020-02-21 18:51:38 +03:00
|
|
|
if field.TagSettings["INDEX"] != "" || field.TagSettings["UNIQUE_INDEX"] != "" {
|
|
|
|
for _, index := range parseFieldIndexes(field) {
|
|
|
|
idx := indexes[index.Name]
|
|
|
|
idx.Name = index.Name
|
|
|
|
if idx.Class == "" {
|
|
|
|
idx.Class = index.Class
|
|
|
|
}
|
2020-02-21 19:02:05 +03:00
|
|
|
if idx.Type == "" {
|
|
|
|
idx.Type = index.Type
|
|
|
|
}
|
|
|
|
if idx.Where == "" {
|
|
|
|
idx.Where = index.Where
|
|
|
|
}
|
|
|
|
if idx.Comment == "" {
|
|
|
|
idx.Comment = index.Comment
|
|
|
|
}
|
2020-02-21 18:51:38 +03:00
|
|
|
idx.Fields = append(idx.Fields, index.Fields...)
|
|
|
|
indexes[index.Name] = idx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return indexes
|
|
|
|
}
|
|
|
|
|
2020-05-30 17:27:20 +03:00
|
|
|
func (schema *Schema) LookIndex(name string) *Index {
|
|
|
|
indexes := schema.ParseIndexes()
|
|
|
|
for _, index := range indexes {
|
|
|
|
if index.Name == name {
|
|
|
|
return &index
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, field := range index.Fields {
|
|
|
|
if field.Name == name {
|
|
|
|
return &index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-21 18:51:38 +03:00
|
|
|
func parseFieldIndexes(field *Field) (indexes []Index) {
|
|
|
|
for _, value := range strings.Split(field.Tag.Get("gorm"), ";") {
|
|
|
|
if value != "" {
|
|
|
|
v := strings.Split(value, ":")
|
|
|
|
k := strings.TrimSpace(strings.ToUpper(v[0]))
|
|
|
|
if k == "INDEX" || k == "UNIQUE_INDEX" {
|
|
|
|
var (
|
2020-02-21 19:02:05 +03:00
|
|
|
name string
|
|
|
|
tag = strings.Join(v[1:], ":")
|
|
|
|
idx = strings.Index(tag, ",")
|
|
|
|
settings = ParseTagSetting(tag, ",")
|
|
|
|
length, _ = strconv.Atoi(settings["LENGTH"])
|
2020-02-21 18:51:38 +03:00
|
|
|
)
|
|
|
|
|
2020-04-03 02:57:52 +03:00
|
|
|
if idx == -1 {
|
|
|
|
idx = len(tag)
|
|
|
|
}
|
|
|
|
|
2020-02-21 19:02:05 +03:00
|
|
|
if idx != -1 {
|
|
|
|
name = tag[0:idx]
|
2020-02-21 18:51:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if name == "" {
|
|
|
|
name = field.Schema.namer.IndexName(field.Schema.Table, field.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (k == "UNIQUE_INDEX") || settings["UNIQUE"] != "" {
|
|
|
|
settings["CLASS"] = "UNIQUE"
|
|
|
|
}
|
|
|
|
|
|
|
|
indexes = append(indexes, Index{
|
2020-02-21 19:02:05 +03:00
|
|
|
Name: name,
|
|
|
|
Class: settings["CLASS"],
|
|
|
|
Type: settings["TYPE"],
|
|
|
|
Where: settings["WHERE"],
|
|
|
|
Comment: settings["COMMENT"],
|
2020-02-21 18:51:38 +03:00
|
|
|
Fields: []IndexOption{{
|
|
|
|
Field: field,
|
|
|
|
Expression: settings["EXPRESSION"],
|
|
|
|
Sort: settings["SORT"],
|
|
|
|
Collate: settings["COLLATE"],
|
|
|
|
Length: length,
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|