diff --git a/schema/check.go b/schema/check.go index ec66bad2..161a6ac6 100644 --- a/schema/check.go +++ b/schema/check.go @@ -6,8 +6,8 @@ import ( ) var ( - // match English letters and midline - regEnLetterAndmidline = regexp.MustCompile("^[A-Za-z-_]+$") + // reg match english letters and midline + regEnLetterAndMidline = regexp.MustCompile("^[A-Za-z-_]+$") ) type Check struct { @@ -22,7 +22,7 @@ func (schema *Schema) ParseCheckConstraints() map[string]Check { for _, field := range schema.FieldsByDBName { if chk := field.TagSettings["CHECK"]; chk != "" { names := strings.Split(chk, ",") - if len(names) > 1 && regEnLetterAndmidline.MatchString(names[0]) { + if len(names) > 1 && regEnLetterAndMidline.MatchString(names[0]) { checks[names[0]] = Check{Name: names[0], Constraint: strings.Join(names[1:], ","), Field: field} } else { if names[0] == "" { diff --git a/schema/relationship.go b/schema/relationship.go index 606e722a..1b93ef88 100644 --- a/schema/relationship.go +++ b/schema/relationship.go @@ -3,7 +3,6 @@ package schema import ( "fmt" "reflect" - "regexp" "strings" "github.com/jinzhu/inflection" @@ -536,7 +535,11 @@ func (rel *Relationship) ParseConstraint() *Constraint { settings = ParseTagSetting(str, ",") ) - if idx != -1 && regexp.MustCompile("^[A-Za-z-_]+$").MatchString(str[0:idx]) { + // optimize match english letters and midline + // The following code is basically called in for. + // In order to avoid the performance problems caused by repeated compilation of regular expressions, + // it only needs to be done once outside, so optimization is done here. + if idx != -1 && regEnLetterAndMidline.MatchString(str[0:idx]) { name = str[0:idx] } else { name = rel.Schema.namer.RelationshipFKName(*rel)