MCPcopy
hub / github.com/go-gorm/gorm / ParseIndexes

Method ParseIndexes

schema/index.go:30–78  ·  view source on GitHub ↗

ParseIndexes parse schema indexes

()

Source from the content-addressed store, hash-verified

28
29// ParseIndexes parse schema indexes
30func (schema *Schema) ParseIndexes() []*Index {
31 indexesByName := map[string]*Index{}
32 indexes := []*Index{}
33
34 for _, field := range schema.Fields {
35 if field.TagSettings["INDEX"] != "" || field.TagSettings["UNIQUEINDEX"] != "" {
36 fieldIndexes, err := parseFieldIndexes(field)
37 if err != nil {
38 schema.err = err
39 break
40 }
41 for _, index := range fieldIndexes {
42 idx := indexesByName[index.Name]
43 if idx == nil {
44 idx = &Index{Name: index.Name}
45 indexesByName[index.Name] = idx
46 indexes = append(indexes, idx)
47 }
48 idx.Name = index.Name
49 if idx.Class == "" {
50 idx.Class = index.Class
51 }
52 if idx.Type == "" {
53 idx.Type = index.Type
54 }
55 if idx.Where == "" {
56 idx.Where = index.Where
57 }
58 if idx.Comment == "" {
59 idx.Comment = index.Comment
60 }
61 if idx.Option == "" {
62 idx.Option = index.Option
63 }
64
65 idx.Fields = append(idx.Fields, index.Fields...)
66 sort.Slice(idx.Fields, func(i, j int) bool {
67 return idx.Fields[i].Priority < idx.Fields[j].Priority
68 })
69 }
70 }
71 }
72 for _, index := range indexes {
73 if index.Class == "UNIQUE" && len(index.Fields) == 1 {
74 index.Fields[0].Field.UniqueIndex = index.Name
75 }
76 }
77 return indexes
78}
79
80func (schema *Schema) LookIndex(name string) *Index {
81 if schema != nil {

Callers 6

LookIndexMethod · 0.95
AutoMigrateMethod · 0.80
CreateTableMethod · 0.80
TestParseIndexFunction · 0.80

Calls 1

parseFieldIndexesFunction · 0.85

Tested by 3

TestParseIndexFunction · 0.64