optimize match english letters and midline

This commit is contained in:
daheige 2021-03-07 16:48:23 +08:00
parent 539a44ac80
commit 0864f9bcec
2 changed files with 8 additions and 5 deletions

View File

@ -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] == "" {

View File

@ -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)