index.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package schema
  2. import (
  3. "fmt"
  4. "sort"
  5. "strconv"
  6. "strings"
  7. )
  8. type Index struct {
  9. Name string
  10. Class string // UNIQUE | FULLTEXT | SPATIAL
  11. Type string // btree, hash, gist, spgist, gin, and brin
  12. Where string
  13. Comment string
  14. Option string // WITH PARSER parser_name
  15. Fields []IndexOption // Note: IndexOption's Field maybe the same
  16. }
  17. type IndexOption struct {
  18. *Field
  19. Expression string
  20. Sort string // DESC, ASC
  21. Collate string
  22. Length int
  23. Priority int
  24. }
  25. // ParseIndexes parse schema indexes
  26. func (schema *Schema) ParseIndexes() []*Index {
  27. indexesByName := map[string]*Index{}
  28. indexes := []*Index{}
  29. for _, field := range schema.Fields {
  30. if field.TagSettings["INDEX"] != "" || field.TagSettings["UNIQUEINDEX"] != "" {
  31. fieldIndexes, err := parseFieldIndexes(field)
  32. if err != nil {
  33. schema.err = err
  34. break
  35. }
  36. for _, index := range fieldIndexes {
  37. idx := indexesByName[index.Name]
  38. if idx == nil {
  39. idx = &Index{Name: index.Name}
  40. indexesByName[index.Name] = idx
  41. indexes = append(indexes, idx)
  42. }
  43. idx.Name = index.Name
  44. if idx.Class == "" {
  45. idx.Class = index.Class
  46. }
  47. if idx.Type == "" {
  48. idx.Type = index.Type
  49. }
  50. if idx.Where == "" {
  51. idx.Where = index.Where
  52. }
  53. if idx.Comment == "" {
  54. idx.Comment = index.Comment
  55. }
  56. if idx.Option == "" {
  57. idx.Option = index.Option
  58. }
  59. idx.Fields = append(idx.Fields, index.Fields...)
  60. sort.Slice(idx.Fields, func(i, j int) bool {
  61. return idx.Fields[i].Priority < idx.Fields[j].Priority
  62. })
  63. }
  64. }
  65. }
  66. for _, index := range indexes {
  67. if index.Class == "UNIQUE" && len(index.Fields) == 1 {
  68. index.Fields[0].Field.UniqueIndex = index.Name
  69. }
  70. }
  71. return indexes
  72. }
  73. func (schema *Schema) LookIndex(name string) *Index {
  74. if schema != nil {
  75. indexes := schema.ParseIndexes()
  76. for _, index := range indexes {
  77. if index.Name == name {
  78. return index
  79. }
  80. for _, field := range index.Fields {
  81. if field.Name == name {
  82. return index
  83. }
  84. }
  85. }
  86. }
  87. return nil
  88. }
  89. func parseFieldIndexes(field *Field) (indexes []Index, err error) {
  90. for _, value := range strings.Split(field.Tag.Get("gorm"), ";") {
  91. if value != "" {
  92. v := strings.Split(value, ":")
  93. k := strings.TrimSpace(strings.ToUpper(v[0]))
  94. if k == "INDEX" || k == "UNIQUEINDEX" {
  95. var (
  96. name string
  97. tag = strings.Join(v[1:], ":")
  98. idx = strings.Index(tag, ",")
  99. tagSetting = strings.Join(strings.Split(tag, ",")[1:], ",")
  100. settings = ParseTagSetting(tagSetting, ",")
  101. length, _ = strconv.Atoi(settings["LENGTH"])
  102. )
  103. if idx == -1 {
  104. idx = len(tag)
  105. }
  106. name = tag[0:idx]
  107. if name == "" {
  108. subName := field.Name
  109. const key = "COMPOSITE"
  110. if composite, found := settings[key]; found {
  111. if len(composite) == 0 || composite == key {
  112. err = fmt.Errorf(
  113. "the composite tag of %s.%s cannot be empty",
  114. field.Schema.Name,
  115. field.Name)
  116. return
  117. }
  118. subName = composite
  119. }
  120. name = field.Schema.namer.IndexName(
  121. field.Schema.Table, subName)
  122. }
  123. if (k == "UNIQUEINDEX") || settings["UNIQUE"] != "" {
  124. settings["CLASS"] = "UNIQUE"
  125. }
  126. priority, err := strconv.Atoi(settings["PRIORITY"])
  127. if err != nil {
  128. priority = 10
  129. }
  130. indexes = append(indexes, Index{
  131. Name: name,
  132. Class: settings["CLASS"],
  133. Type: settings["TYPE"],
  134. Where: settings["WHERE"],
  135. Comment: settings["COMMENT"],
  136. Option: settings["OPTION"],
  137. Fields: []IndexOption{{
  138. Field: field,
  139. Expression: settings["EXPRESSION"],
  140. Sort: settings["SORT"],
  141. Collate: settings["COLLATE"],
  142. Length: length,
  143. Priority: priority,
  144. }},
  145. })
  146. }
  147. }
  148. }
  149. err = nil
  150. return
  151. }