mirror of https://github.com/go-gorm/gorm.git
Compile regexp ahead of time
Signed-off-by: Xavier Sandal <sandalwing@sandalwing.com>
This commit is contained in:
parent
0f2ceb5a77
commit
5a4dca7645
17
scope.go
17
scope.go
|
@ -447,7 +447,12 @@ func (scope *Scope) callMethod(methodName string, reflectValue reflect.Value) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var columnRegexp = regexp.MustCompile("^[a-zA-Z]+(\\.[a-zA-Z]+)*$") // only match string like `name`, `users.name`
|
var (
|
||||||
|
columnRegexp = regexp.MustCompile("^[a-zA-Z]+(\\.[a-zA-Z]+)*$") // only match string like `name`, `users.name`
|
||||||
|
isNumberRegexp = regexp.MustCompile("^\\s*\\d+\\s*$") // match if string is number
|
||||||
|
comparisonRegexp = regexp.MustCompile("(?i) (=|<>|>|<|LIKE|IS|IN) ")
|
||||||
|
countingQueryRegexp = regexp.MustCompile("(?i)^count(.+)$")
|
||||||
|
)
|
||||||
|
|
||||||
func (scope *Scope) quoteIfPossible(str string) string {
|
func (scope *Scope) quoteIfPossible(str string) string {
|
||||||
if columnRegexp.MatchString(str) {
|
if columnRegexp.MatchString(str) {
|
||||||
|
@ -509,8 +514,7 @@ func (scope *Scope) primaryCondition(value interface{}) string {
|
||||||
func (scope *Scope) buildWhereCondition(clause map[string]interface{}) (str string) {
|
func (scope *Scope) buildWhereCondition(clause map[string]interface{}) (str string) {
|
||||||
switch value := clause["query"].(type) {
|
switch value := clause["query"].(type) {
|
||||||
case string:
|
case string:
|
||||||
// if string is number
|
if isNumberRegexp.MatchString(value) {
|
||||||
if regexp.MustCompile("^\\s*\\d+\\s*$").MatchString(value) {
|
|
||||||
return scope.primaryCondition(scope.AddToVars(value))
|
return scope.primaryCondition(scope.AddToVars(value))
|
||||||
} else if value != "" {
|
} else if value != "" {
|
||||||
str = fmt.Sprintf("(%v)", value)
|
str = fmt.Sprintf("(%v)", value)
|
||||||
|
@ -573,11 +577,10 @@ func (scope *Scope) buildNotCondition(clause map[string]interface{}) (str string
|
||||||
|
|
||||||
switch value := clause["query"].(type) {
|
switch value := clause["query"].(type) {
|
||||||
case string:
|
case string:
|
||||||
// is number
|
if isNumberRegexp.MatchString(value) {
|
||||||
if regexp.MustCompile("^\\s*\\d+\\s*$").MatchString(value) {
|
|
||||||
id, _ := strconv.Atoi(value)
|
id, _ := strconv.Atoi(value)
|
||||||
return fmt.Sprintf("(%v <> %v)", scope.Quote(primaryKey), id)
|
return fmt.Sprintf("(%v <> %v)", scope.Quote(primaryKey), id)
|
||||||
} else if regexp.MustCompile("(?i) (=|<>|>|<|LIKE|IS|IN) ").MatchString(value) {
|
} else if comparisonRegexp.MatchString(value) {
|
||||||
str = fmt.Sprintf(" NOT (%v) ", value)
|
str = fmt.Sprintf(" NOT (%v) ", value)
|
||||||
notEqualSQL = fmt.Sprintf("NOT (%v)", value)
|
notEqualSQL = fmt.Sprintf("NOT (%v)", value)
|
||||||
} else {
|
} else {
|
||||||
|
@ -924,7 +927,7 @@ func (scope *Scope) pluck(column string, value interface{}) *Scope {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) count(value interface{}) *Scope {
|
func (scope *Scope) count(value interface{}) *Scope {
|
||||||
if query, ok := scope.Search.selects["query"]; !ok || !regexp.MustCompile("(?i)^count(.+)$").MatchString(fmt.Sprint(query)) {
|
if query, ok := scope.Search.selects["query"]; !ok || !countingQueryRegexp.MatchString(fmt.Sprint(query)) {
|
||||||
scope.Search.Select("count(*)")
|
scope.Search.Select("count(*)")
|
||||||
}
|
}
|
||||||
scope.Search.ignoreOrderQuery = true
|
scope.Search.ignoreOrderQuery = true
|
||||||
|
|
5
utils.go
5
utils.go
|
@ -26,6 +26,9 @@ var NowFunc = func() time.Time {
|
||||||
var commonInitialisms = []string{"API", "ASCII", "CPU", "CSS", "DNS", "EOF", "GUID", "HTML", "HTTP", "HTTPS", "ID", "IP", "JSON", "LHS", "QPS", "RAM", "RHS", "RPC", "SLA", "SMTP", "SSH", "TLS", "TTL", "UI", "UID", "UUID", "URI", "URL", "UTF8", "VM", "XML", "XSRF", "XSS"}
|
var commonInitialisms = []string{"API", "ASCII", "CPU", "CSS", "DNS", "EOF", "GUID", "HTML", "HTTP", "HTTPS", "ID", "IP", "JSON", "LHS", "QPS", "RAM", "RHS", "RPC", "SLA", "SMTP", "SSH", "TLS", "TTL", "UI", "UID", "UUID", "URI", "URL", "UTF8", "VM", "XML", "XSRF", "XSS"}
|
||||||
var commonInitialismsReplacer *strings.Replacer
|
var commonInitialismsReplacer *strings.Replacer
|
||||||
|
|
||||||
|
var goSrcRegexp = regexp.MustCompile(`jinzhu/gorm/.*.go`)
|
||||||
|
var goTestRegexp = regexp.MustCompile(`jinzhu/gorm/.*test.go`)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
var commonInitialismsForReplacer []string
|
var commonInitialismsForReplacer []string
|
||||||
for _, initialism := range commonInitialisms {
|
for _, initialism := range commonInitialisms {
|
||||||
|
@ -171,7 +174,7 @@ func toQueryValues(values [][]interface{}) (results []interface{}) {
|
||||||
func fileWithLineNum() string {
|
func fileWithLineNum() string {
|
||||||
for i := 2; i < 15; i++ {
|
for i := 2; i < 15; i++ {
|
||||||
_, file, line, ok := runtime.Caller(i)
|
_, file, line, ok := runtime.Caller(i)
|
||||||
if ok && (!regexp.MustCompile(`jinzhu/gorm/.*.go`).MatchString(file) || regexp.MustCompile(`jinzhu/gorm/.*test.go`).MatchString(file)) {
|
if ok && (!goSrcRegexp.MatchString(file) || goTestRegexp.MatchString(file)) {
|
||||||
return fmt.Sprintf("%v:%v", file, line)
|
return fmt.Sprintf("%v:%v", file, line)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue