2020-02-22 06:15:51 +03:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-01-06 10:02:53 +03:00
|
|
|
// reg match english letters and midline
|
|
|
|
var regEnLetterAndMidline = regexp.MustCompile("^[A-Za-z-_]+$")
|
2021-02-07 05:12:13 +03:00
|
|
|
|
2020-02-22 06:15:51 +03:00
|
|
|
type Check struct {
|
|
|
|
Name string
|
|
|
|
Constraint string // length(phone) >= 10
|
|
|
|
*Field
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseCheckConstraints parse schema check constraints
|
|
|
|
func (schema *Schema) ParseCheckConstraints() map[string]Check {
|
2022-01-06 10:02:53 +03:00
|
|
|
checks := map[string]Check{}
|
2020-02-22 06:15:51 +03:00
|
|
|
for _, field := range schema.FieldsByDBName {
|
|
|
|
if chk := field.TagSettings["CHECK"]; chk != "" {
|
|
|
|
names := strings.Split(chk, ",")
|
2021-03-08 05:21:33 +03:00
|
|
|
if len(names) > 1 && regEnLetterAndMidline.MatchString(names[0]) {
|
2020-02-22 06:15:51 +03:00
|
|
|
checks[names[0]] = Check{Name: names[0], Constraint: strings.Join(names[1:], ","), Field: field}
|
|
|
|
} else {
|
2020-02-22 14:41:01 +03:00
|
|
|
if names[0] == "" {
|
|
|
|
chk = strings.Join(names[1:], ",")
|
|
|
|
}
|
2020-02-22 06:15:51 +03:00
|
|
|
name := schema.namer.CheckerName(schema.Table, field.DBName)
|
|
|
|
checks[name] = Check{Name: name, Constraint: chk, Field: field}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return checks
|
|
|
|
}
|