Skip to content

Commit 02cb405

Browse files
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 <ratanphayade@users.noreply.github.com>
1 parent 221d0a0 commit 02cb405

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

schema/check.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
)
77

88
var (
9-
// match English letters and midline
10-
regEnLetterAndmidline = regexp.MustCompile("^[A-Za-z-_]+$")
9+
// reg match english letters and midline
10+
regEnLetterAndMidline = regexp.MustCompile("^[A-Za-z-_]+$")
1111
)
1212

1313
type Check struct {
@@ -22,7 +22,7 @@ func (schema *Schema) ParseCheckConstraints() map[string]Check {
2222
for _, field := range schema.FieldsByDBName {
2323
if chk := field.TagSettings["CHECK"]; chk != "" {
2424
names := strings.Split(chk, ",")
25-
if len(names) > 1 && regEnLetterAndmidline.MatchString(names[0]) {
25+
if len(names) > 1 && regEnLetterAndMidline.MatchString(names[0]) {
2626
checks[names[0]] = Check{Name: names[0], Constraint: strings.Join(names[1:], ","), Field: field}
2727
} else {
2828
if names[0] == "" {

schema/relationship.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package schema
33
import (
44
"fmt"
55
"reflect"
6-
"regexp"
76
"strings"
87

98
"github.com/jinzhu/inflection"
@@ -536,7 +535,11 @@ func (rel *Relationship) ParseConstraint() *Constraint {
536535
settings = ParseTagSetting(str, ",")
537536
)
538537

539-
if idx != -1 && regexp.MustCompile("^[A-Za-z-_]+$").MatchString(str[0:idx]) {
538+
// optimize match english letters and midline
539+
// The following code is basically called in for.
540+
// In order to avoid the performance problems caused by repeated compilation of regular expressions,
541+
// it only needs to be done once outside, so optimization is done here.
542+
if idx != -1 && regEnLetterAndMidline.MatchString(str[0:idx]) {
540543
name = str[0:idx]
541544
} else {
542545
name = rel.Schema.namer.RelationshipFKName(*rel)

0 commit comments

Comments
 (0)