index: add composite id
This commit is contained in:
		
							parent
							
								
									b49ae84780
								
							
						
					
					
						commit
						31673d490c
					
				@ -1,6 +1,7 @@
 | 
				
			|||||||
package schema
 | 
					package schema
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"sort"
 | 
						"sort"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
@ -31,7 +32,12 @@ func (schema *Schema) ParseIndexes() map[string]Index {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	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) {
 | 
								fieldIndexes, err := parseFieldIndexes(field)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									schema.err = err
 | 
				
			||||||
 | 
									break
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								for _, index := range fieldIndexes {
 | 
				
			||||||
				idx := indexes[index.Name]
 | 
									idx := indexes[index.Name]
 | 
				
			||||||
				idx.Name = index.Name
 | 
									idx.Name = index.Name
 | 
				
			||||||
				if idx.Class == "" {
 | 
									if idx.Class == "" {
 | 
				
			||||||
@ -82,7 +88,7 @@ func (schema *Schema) LookIndex(name string) *Index {
 | 
				
			|||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func parseFieldIndexes(field *Field) (indexes []Index) {
 | 
					func parseFieldIndexes(field *Field) (indexes []Index, err error) {
 | 
				
			||||||
	for _, value := range strings.Split(field.Tag.Get("gorm"), ";") {
 | 
						for _, value := range strings.Split(field.Tag.Get("gorm"), ";") {
 | 
				
			||||||
		if value != "" {
 | 
							if value != "" {
 | 
				
			||||||
			v := strings.Split(value, ":")
 | 
								v := strings.Split(value, ":")
 | 
				
			||||||
@ -106,7 +112,20 @@ func parseFieldIndexes(field *Field) (indexes []Index) {
 | 
				
			|||||||
				}
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				if name == "" {
 | 
									if name == "" {
 | 
				
			||||||
					name = field.Schema.namer.IndexName(field.Schema.Table, field.Name)
 | 
										subName := field.Name
 | 
				
			||||||
 | 
										const key = "COMPOSITE"
 | 
				
			||||||
 | 
										if composite, found := settings[key]; found {
 | 
				
			||||||
 | 
											if len(composite) == 0 || composite == key {
 | 
				
			||||||
 | 
												err = fmt.Errorf(
 | 
				
			||||||
 | 
													"The composite tag of %s.%s cannot be empty",
 | 
				
			||||||
 | 
													field.Schema.Name,
 | 
				
			||||||
 | 
													field.Name)
 | 
				
			||||||
 | 
												return
 | 
				
			||||||
 | 
											}
 | 
				
			||||||
 | 
											subName = composite
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
										name = field.Schema.namer.IndexName(
 | 
				
			||||||
 | 
											field.Schema.Table, subName)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				if (k == "UNIQUEINDEX") || settings["UNIQUE"] != "" {
 | 
									if (k == "UNIQUEINDEX") || settings["UNIQUE"] != "" {
 | 
				
			||||||
@ -138,5 +157,6 @@ func parseFieldIndexes(field *Field) (indexes []Index) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err = nil
 | 
				
			||||||
	return
 | 
						return
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user