Debian Codegraph @

schema/index.go

raw
1 package schema
2
3 import (
4 "fmt"
5 "sort"
6 "strconv"
7 "strings"
8 )
9
10 type Index struct {
11 Name string
12 Class string // UNIQUE | FULLTEXT | SPATIAL
13 Type string // btree, hash, gist, spgist, gin, and brin
14 Where string
15 Comment string
16 Option string // WITH PARSER parser_name
17 Fields []IndexOption // Note: IndexOption's Field maybe the same
18 }
19
20 type IndexOption struct {
21 *Field
22 Expression string
23 Sort string // DESC, ASC
24 Collate string
25 Length int
26 Priority int
27 }
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 {
82 indexes := schema.ParseIndexes()
83 for _, index := range indexes {
84 if index.Name == name {
85 return index
86 }
87
88 for _, field := range index.Fields {
89 if field.Name == name {
90 return index
91 }
92 }
93 }
94 }
95
96 return nil
97 }
98
99 func parseFieldIndexes(field *Field) (indexes []Index, err error) {
100 for _, value := range strings.Split(field.Tag.Get("gorm"), ";") {
101 if value != "" {
102 v := strings.Split(value, ":")
103 k := strings.TrimSpace(strings.ToUpper(v[0]))
104 if k == "INDEX" || k == "UNIQUEINDEX" {
105 var (
106 name string
107 tag = strings.Join(v[1:], ":")
108 idx = strings.IndexByte(tag, ',')
109 tagSetting = strings.Join(strings.Split(tag, ",")[1:], ",")
110 settings = ParseTagSetting(tagSetting, ",")
111 length, _ = strconv.Atoi(settings["LENGTH"])
112 )
113
114 if idx == -1 {
115 idx = len(tag)
116 }
117
118 name = tag[0:idx]
119 if name == "" {
120 subName := field.Name
121 const key = "COMPOSITE"
122 if composite, found := settings[key]; found {
123 if len(composite) == 0 || composite == key {
124 err = fmt.Errorf(
125 "the composite tag of %s.%s cannot be empty",
126 field.Schema.Name,
127 field.Name)
128 return
129 }
130 subName = composite
131 }
132 name = field.Schema.namer.IndexName(
133 field.Schema.Table, subName)
134 }
135
136 if (k == "UNIQUEINDEX") || settings["UNIQUE"] != "" {
137 settings["CLASS"] = "UNIQUE"
138 }
139
140 priority, err := strconv.Atoi(settings["PRIORITY"])
141 if err != nil {
142 priority = 10
143 }
144
145 indexes = append(indexes, Index{
146 Name: name,
147 Class: settings["CLASS"],
148 Type: settings["TYPE"],
149 Where: settings["WHERE"],
150 Comment: settings["COMMENT"],
151 Option: settings["OPTION"],
152 Fields: []IndexOption{{
153 Field: field,
154 Expression: settings["EXPRESSION"],
155 Sort: settings["SORT"],
156 Collate: settings["COLLATE"],
157 Length: length,
158 Priority: priority,
159 }},
160 })
161 }
162 }
163 }
164
165 err = nil
166 return
167 }