2018-05-22 18:27:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-10-02 09:10:21 +00:00
|
|
|
"git.xdrm.io/go/aicra/driver"
|
2018-05-22 18:27:51 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2018-10-02 09:10:21 +00:00
|
|
|
func main() {}
|
|
|
|
func Export() driver.Checker { return new(VarcharChecker) }
|
|
|
|
|
|
|
|
type VarcharChecker struct {
|
|
|
|
min *uint64
|
|
|
|
max *uint64
|
|
|
|
}
|
2018-05-22 18:27:51 +00:00
|
|
|
|
2018-09-13 08:21:35 +00:00
|
|
|
// Match filters the parameter type format "varchar(min, max)"
|
2018-10-02 09:10:21 +00:00
|
|
|
func (vck *VarcharChecker) Match(name string) bool {
|
|
|
|
|
|
|
|
vck.min = nil
|
|
|
|
vck.max = nil
|
2018-05-22 18:27:51 +00:00
|
|
|
|
|
|
|
/* (1) Create regexp */
|
|
|
|
re, err := regexp.Compile(`^varchar\((\d+), ?(\d+)\)$`)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Check if matches */
|
|
|
|
matches := re.FindStringSubmatch(name)
|
|
|
|
if matches == nil || len(matches) < 3 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Extract min */
|
|
|
|
minVal, err := strconv.ParseUint(matches[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2018-10-02 09:10:21 +00:00
|
|
|
vck.min = &minVal
|
2018-05-22 18:27:51 +00:00
|
|
|
|
|
|
|
/* (4) Extract max */
|
|
|
|
maxVal, err := strconv.ParseUint(matches[2], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (5) Check that min <= max */
|
|
|
|
if maxVal < minVal {
|
|
|
|
panic("varchar(x, y) ; constraint violation : x <= y")
|
|
|
|
}
|
2018-10-02 09:10:21 +00:00
|
|
|
vck.max = &maxVal
|
2018-05-22 18:27:51 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-13 08:21:35 +00:00
|
|
|
// Check whether the given value fulfills the condition (min, max)
|
2018-10-02 09:10:21 +00:00
|
|
|
func (vck *VarcharChecker) Check(value interface{}) bool {
|
2018-05-22 18:27:51 +00:00
|
|
|
|
|
|
|
/* (1) Check if string */
|
|
|
|
strval, ok := value.(string)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Check if sizes set */
|
2018-10-02 09:10:21 +00:00
|
|
|
if vck.min == nil || vck.max == nil {
|
2018-05-22 18:27:51 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-10-02 09:10:21 +00:00
|
|
|
length := uint64(len(strval))
|
|
|
|
|
2018-05-22 18:27:51 +00:00
|
|
|
/* (3) Check min */
|
2018-10-02 09:10:21 +00:00
|
|
|
if length < *vck.min {
|
2018-05-22 18:27:51 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) Check max */
|
2018-10-02 09:10:21 +00:00
|
|
|
if length > *vck.max {
|
2018-05-22 18:27:51 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|