tdb/constraints.go

50 lines
895 B
Go

package tdb
import (
"errors"
"fmt"
"strings"
)
var (
ConstraintValidated ConstraintValidationStatus = true
ConstraintViolation ConstraintValidationStatus = false
)
type ConstraintValidationStatus bool
type ConstraintOptions struct {
Table string
Field string
Foreign string
NotNull bool
}
type constraintish interface {
validate(tx *Tx, val dbPtrValue) error
}
type constraints []constraintish
func (c constraints) validate(tx *Tx, val dbPtrValue) error {
errs := make([]error, 0)
for _, constraint := range c {
if err := constraint.validate(tx, val); err != nil {
errs = append(errs, err)
}
}
errLen := len(errs)
if errLen == 0 {
return nil
}
sb := &strings.Builder{}
sb.WriteString(fmt.Sprintf("%d constraint violation errors:", errLen))
for _, e := range errs {
sb.WriteString("\n")
sb.WriteString(e.Error())
}
return errors.New(sb.String())
}