56 lines
1.9 KiB
Go
56 lines
1.9 KiB
Go
package tdb
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
type elementsNotNullConstraint struct {
|
|
table *table
|
|
field dbField
|
|
}
|
|
|
|
func newElementsNotNullConstraint(table *table, field dbField) (constraintish, error) {
|
|
if table == nil {
|
|
return nil, errors.New("[constraint.elementsnotnull] unable to create not-null without table")
|
|
}
|
|
|
|
if !field.IsSliceish() {
|
|
return nil, errors.New("[constraint.elementsnotnull] field is not an array or slice")
|
|
}
|
|
|
|
return &elementsNotNullConstraint{
|
|
table: table,
|
|
field: field,
|
|
}, nil
|
|
}
|
|
|
|
// func (c *notNullConstraint) validateRaw(tx *Tx, foreignKeys [][]byte) error {
|
|
// for _, foreignKey := range foreignKeys {
|
|
// for _, nullishValue := range nullishValues {
|
|
// if bytes.Equal(nullishValue, foreignKey) {
|
|
// c.table.debugLogf("[constraint.elementsnotnull.validateRaw] violation: '%s'.'%s' is null-ish value '%s'", c.table.name, c.field.Name, nullishValue)
|
|
// return fmt.Errorf("[constraint.elementsnotnull.validateRaw] violation: '%s'.'%s' is null-ish value '%s'", c.table.name, c.field.Name, nullishValue)
|
|
// }
|
|
// }
|
|
// }
|
|
// return nil
|
|
// }
|
|
|
|
func (c *elementsNotNullConstraint) validate(tx *Tx, pv dbPtrValue) error {
|
|
c.table.debugLogf("[constraint.elementsnotnull.validate] validating not-null for '%s'.'%s'", c.table.name, c.field.Name)
|
|
val := pv.dangerous_Field(c.field)
|
|
if val.IsZero() {
|
|
c.table.debugLogf("[constraint.elementsnotnull.validate] '%s'.'%s' is zero value, not validating elements", c.table.name, c.field.Name)
|
|
return nil
|
|
}
|
|
lenVal := val.Len()
|
|
for i := 0; i < lenVal; i++ {
|
|
if val.Index(i).IsZero() {
|
|
c.table.debugLogf("[constraint.elementsnotnull.validate] violation: '%s'.'%s' contains zero value at index %d", c.table.name, c.field.Name, i)
|
|
return fmt.Errorf("[constraint.elementsnotnull.validate] violation: '%s'.'%s' contains zero value at index %d", c.table.name, c.field.Name, i)
|
|
}
|
|
}
|
|
return nil
|
|
}
|