mirror of https://github.com/go-gorm/gorm.git
Add index priority supports
This commit is contained in:
parent
de482f57ff
commit
619cd332ec
|
@ -1,6 +1,7 @@
|
||||||
package schema
|
package schema
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -20,6 +21,7 @@ type IndexOption struct {
|
||||||
Sort string // DESC, ASC
|
Sort string // DESC, ASC
|
||||||
Collate string
|
Collate string
|
||||||
Length int
|
Length int
|
||||||
|
priority int
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseIndexes parse schema indexes
|
// ParseIndexes parse schema indexes
|
||||||
|
@ -43,7 +45,12 @@ func (schema *Schema) ParseIndexes() map[string]Index {
|
||||||
if idx.Comment == "" {
|
if idx.Comment == "" {
|
||||||
idx.Comment = index.Comment
|
idx.Comment = index.Comment
|
||||||
}
|
}
|
||||||
|
|
||||||
idx.Fields = append(idx.Fields, index.Fields...)
|
idx.Fields = append(idx.Fields, index.Fields...)
|
||||||
|
sort.Slice(idx.Fields, func(i, j int) bool {
|
||||||
|
return idx.Fields[i].priority < idx.Fields[j].priority
|
||||||
|
})
|
||||||
|
|
||||||
indexes[index.Name] = idx
|
indexes[index.Name] = idx
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -101,6 +108,11 @@ func parseFieldIndexes(field *Field) (indexes []Index) {
|
||||||
settings["CLASS"] = "UNIQUE"
|
settings["CLASS"] = "UNIQUE"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
priority, err := strconv.Atoi(settings["PRIORITY"])
|
||||||
|
if err != nil {
|
||||||
|
priority = 10
|
||||||
|
}
|
||||||
|
|
||||||
indexes = append(indexes, Index{
|
indexes = append(indexes, Index{
|
||||||
Name: name,
|
Name: name,
|
||||||
Class: settings["CLASS"],
|
Class: settings["CLASS"],
|
||||||
|
@ -113,6 +125,7 @@ func parseFieldIndexes(field *Field) (indexes []Index) {
|
||||||
Sort: settings["SORT"],
|
Sort: settings["SORT"],
|
||||||
Collate: settings["COLLATE"],
|
Collate: settings["COLLATE"],
|
||||||
Length: length,
|
Length: length,
|
||||||
|
priority: priority,
|
||||||
}},
|
}},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ type UserIndex struct {
|
||||||
Name6 int64 `gorm:"index:profile,comment:hello \\, world,where:age > 10"`
|
Name6 int64 `gorm:"index:profile,comment:hello \\, world,where:age > 10"`
|
||||||
Age int64 `gorm:"index:profile,expression:ABS(age)"`
|
Age int64 `gorm:"index:profile,expression:ABS(age)"`
|
||||||
OID int64 `gorm:"index:idx_id;index:idx_oid,unique"`
|
OID int64 `gorm:"index:idx_id;index:idx_oid,unique"`
|
||||||
MemberNumber string `gorm:"index:idx_id"`
|
MemberNumber string `gorm:"index:idx_id,priority:1"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseIndex(t *testing.T) {
|
func TestParseIndex(t *testing.T) {
|
||||||
|
@ -29,18 +29,19 @@ func TestParseIndex(t *testing.T) {
|
||||||
results := map[string]schema.Index{
|
results := map[string]schema.Index{
|
||||||
"idx_user_indices_name": {
|
"idx_user_indices_name": {
|
||||||
Name: "idx_user_indices_name",
|
Name: "idx_user_indices_name",
|
||||||
Fields: []schema.IndexOption{{}},
|
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name"}}},
|
||||||
},
|
},
|
||||||
"idx_name": {
|
"idx_name": {
|
||||||
Name: "idx_name",
|
Name: "idx_name",
|
||||||
Class: "UNIQUE",
|
Class: "UNIQUE",
|
||||||
Fields: []schema.IndexOption{{}},
|
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name2"}}},
|
||||||
},
|
},
|
||||||
"idx_user_indices_name3": {
|
"idx_user_indices_name3": {
|
||||||
Name: "idx_user_indices_name3",
|
Name: "idx_user_indices_name3",
|
||||||
Type: "btree",
|
Type: "btree",
|
||||||
Where: "name3 != 'jinzhu'",
|
Where: "name3 != 'jinzhu'",
|
||||||
Fields: []schema.IndexOption{{
|
Fields: []schema.IndexOption{{
|
||||||
|
Field: &schema.Field{Name: "Name3"},
|
||||||
Sort: "desc",
|
Sort: "desc",
|
||||||
Collate: "utf8",
|
Collate: "utf8",
|
||||||
Length: 10,
|
Length: 10,
|
||||||
|
@ -49,31 +50,32 @@ func TestParseIndex(t *testing.T) {
|
||||||
"idx_user_indices_name4": {
|
"idx_user_indices_name4": {
|
||||||
Name: "idx_user_indices_name4",
|
Name: "idx_user_indices_name4",
|
||||||
Class: "UNIQUE",
|
Class: "UNIQUE",
|
||||||
Fields: []schema.IndexOption{{}},
|
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name4"}}},
|
||||||
},
|
},
|
||||||
"idx_user_indices_name5": {
|
"idx_user_indices_name5": {
|
||||||
Name: "idx_user_indices_name5",
|
Name: "idx_user_indices_name5",
|
||||||
Class: "FULLTEXT",
|
Class: "FULLTEXT",
|
||||||
Comment: "hello , world",
|
Comment: "hello , world",
|
||||||
Where: "age > 10",
|
Where: "age > 10",
|
||||||
Fields: []schema.IndexOption{{}},
|
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name5"}}},
|
||||||
},
|
},
|
||||||
"profile": {
|
"profile": {
|
||||||
Name: "profile",
|
Name: "profile",
|
||||||
Comment: "hello , world",
|
Comment: "hello , world",
|
||||||
Where: "age > 10",
|
Where: "age > 10",
|
||||||
Fields: []schema.IndexOption{{}, {
|
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "Name6"}}, {
|
||||||
|
Field: &schema.Field{Name: "Age"},
|
||||||
Expression: "ABS(age)",
|
Expression: "ABS(age)",
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
"idx_id": {
|
"idx_id": {
|
||||||
Name: "idx_id",
|
Name: "idx_id",
|
||||||
Fields: []schema.IndexOption{{}, {}},
|
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "MemberNumber"}}, {Field: &schema.Field{Name: "OID"}}},
|
||||||
},
|
},
|
||||||
"idx_oid": {
|
"idx_oid": {
|
||||||
Name: "idx_oid",
|
Name: "idx_oid",
|
||||||
Class: "UNIQUE",
|
Class: "UNIQUE",
|
||||||
Fields: []schema.IndexOption{{}},
|
Fields: []schema.IndexOption{{Field: &schema.Field{Name: "OID"}}},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,6 +98,10 @@ func TestParseIndex(t *testing.T) {
|
||||||
|
|
||||||
for idx, ef := range result.Fields {
|
for idx, ef := range result.Fields {
|
||||||
rf := v.Fields[idx]
|
rf := v.Fields[idx]
|
||||||
|
if rf.Field.Name != ef.Field.Name {
|
||||||
|
t.Fatalf("index field should equal, expects %v, got %v", rf.Field.Name, ef.Field.Name)
|
||||||
|
}
|
||||||
|
|
||||||
for _, name := range []string{"Expression", "Sort", "Collate", "Length"} {
|
for _, name := range []string{"Expression", "Sort", "Collate", "Length"} {
|
||||||
if reflect.ValueOf(ef).FieldByName(name).Interface() != reflect.ValueOf(rf).FieldByName(name).Interface() {
|
if reflect.ValueOf(ef).FieldByName(name).Interface() != reflect.ValueOf(rf).FieldByName(name).Interface() {
|
||||||
t.Errorf(
|
t.Errorf(
|
||||||
|
|
|
@ -130,7 +130,7 @@ func (schema *Schema) buildPolymorphicRelation(relation *Relationship, field *Fi
|
||||||
PolymorphicID: relation.FieldSchema.FieldsByName[polymorphic+"ID"],
|
PolymorphicID: relation.FieldSchema.FieldsByName[polymorphic+"ID"],
|
||||||
}
|
}
|
||||||
|
|
||||||
if value, ok := field.TagSettings["POLYMORPHIC_VALUE"]; ok {
|
if value, ok := field.TagSettings["POLYMORPHICVALUE"]; ok {
|
||||||
relation.Polymorphic.Value = strings.TrimSpace(value)
|
relation.Polymorphic.Value = strings.TrimSpace(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,8 +9,8 @@ import (
|
||||||
type Hamster struct {
|
type Hamster struct {
|
||||||
Id int
|
Id int
|
||||||
Name string
|
Name string
|
||||||
PreferredToy Toy `gorm:"polymorphic:Owner;polymorphic_value:hamster_preferred"`
|
PreferredToy Toy `gorm:"polymorphic:Owner;polymorphicValue:hamster_preferred"`
|
||||||
OtherToy Toy `gorm:"polymorphic:Owner;polymorphic_value:hamster_other"`
|
OtherToy Toy `gorm:"polymorphic:Owner;polymorphicValue:hamster_other"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNamedPolymorphic(t *testing.T) {
|
func TestNamedPolymorphic(t *testing.T) {
|
||||||
|
|
|
@ -24,8 +24,8 @@ type User struct {
|
||||||
ManagerID *uint
|
ManagerID *uint
|
||||||
Manager *User
|
Manager *User
|
||||||
Team []User `gorm:"foreignkey:ManagerID"`
|
Team []User `gorm:"foreignkey:ManagerID"`
|
||||||
Languages []Language `gorm:"many2many:UserSpeak"`
|
Languages []Language `gorm:"many2many:UserSpeak;"`
|
||||||
Friends []*User `gorm:"many2many:user_friends"`
|
Friends []*User `gorm:"many2many:user_friends;"`
|
||||||
Active bool
|
Active bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue