From e80853e7f5eb5313be1c41ae122b34335cbafcf7 Mon Sep 17 00:00:00 2001 From: heige Date: Sun, 7 Feb 2021 10:12:13 +0800 Subject: [PATCH] optimization check for ParseCheckConstraints (#4063) --- schema/check.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/schema/check.go b/schema/check.go index 7d31ec70..ec66bad2 100644 --- a/schema/check.go +++ b/schema/check.go @@ -5,6 +5,11 @@ import ( "strings" ) +var ( + // match English letters and midline + regEnLetterAndmidline = regexp.MustCompile("^[A-Za-z-_]+$") +) + type Check struct { Name string Constraint string // length(phone) >= 10 @@ -17,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 && regexp.MustCompile("^[A-Za-z-_]+$").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] == "" {