From 02cb40531ea2234acc8b201486588a0a6bc72da6 Mon Sep 17 00:00:00 2001 From: heige Date: Mon, 8 Mar 2021 10:21:33 +0800 Subject: [PATCH] Optimize parse constraint (#4153) * for Config.cacheStore store PreparedStmtDB key * invalid db error and value and invalid value length error (#4151) * support named params in Select API (#4142) * adds support for named arguments in select * changes clause identifies and adds test * optimize match english letters and midline Co-authored-by: Ratan Phayade --- schema/check.go | 6 +++--- schema/relationship.go | 7 +++++-- 2 files changed, 8 insertions(+), 5 deletions(-) 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)