ParseIndexes parse schema indexes
()
| 28 | |
| 29 | // ParseIndexes parse schema indexes |
| 30 | func (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 | |
| 80 | func (schema *Schema) LookIndex(name string) *Index { |
| 81 | if schema != nil { |