optimize ParseIndexes func

This commit is contained in:
daheige 2021-06-14 15:10:39 +08:00
parent bdf3eea560
commit aefcf017dc

View File

@ -27,23 +27,34 @@ type IndexOption struct {
// ParseIndexes parse schema indexes // ParseIndexes parse schema indexes
func (schema *Schema) ParseIndexes() map[string]Index { func (schema *Schema) ParseIndexes() map[string]Index {
indexes := make(map[string]Index, len(schema.Fields)+1) indexes := make(map[string]Index, len(schema.Fields))
for _, field := range schema.Fields { for _, field := range schema.Fields {
if field.TagSettings["INDEX"] != "" || field.TagSettings["UNIQUEINDEX"] != "" { if field.TagSettings["INDEX"] != "" || field.TagSettings["UNIQUEINDEX"] != "" {
for _, index := range parseFieldIndexes(field) { for _, index := range parseFieldIndexes(field) {
idx, ok := indexes[index.Name] idx, ok := indexes[index.Name]
if !ok { if !ok {
indexes[index.Name] = Index{ indexes[index.Name] = Index{
Name: index.Name, Fields: make([]IndexOption, 0, 10), // Fields cap default is 10
Class: index.Class,
Type: index.Type,
Where: index.Where,
Comment: index.Comment,
Option: index.Option,
Fields: make([]IndexOption, 0, 10), // Fields cap default is 10
} }
} }
idx.Name = index.Name
if idx.Class == "" {
idx.Class = index.Class
}
if idx.Type == "" {
idx.Type = index.Type
}
if idx.Where == "" {
idx.Where = index.Where
}
if idx.Comment == "" {
idx.Comment = index.Comment
}
if idx.Option == "" {
idx.Option = index.Option
}
idx.Fields = append(idx.Fields, index.Fields...) idx.Fields = append(idx.Fields, index.Fields...)
if len(idx.Fields) >= 2 { if len(idx.Fields) >= 2 {
sort.Slice(idx.Fields, func(i, j int) bool { sort.Slice(idx.Fields, func(i, j int) bool {