constraints.go 883 B

123456789101112131415161718192021222324252627282930313233
  1. package storage
  2. import "fmt"
  3. // PrimaryKeyViolationError preserves duplicate-key identity through wrapped
  4. // executor errors so wire adapters can report integrity SQLSTATE 23505.
  5. type PrimaryKeyViolationError struct {
  6. Table string
  7. Key string
  8. }
  9. func (e *PrimaryKeyViolationError) Error() string {
  10. return fmt.Sprintf("duplicate primary key: %s", e.Key)
  11. }
  12. // NotNullViolationError identifies the final row that violates a required column.
  13. type NotNullViolationError struct {
  14. Table string
  15. Column string
  16. }
  17. func (e *NotNullViolationError) Error() string {
  18. return fmt.Sprintf("NOT NULL constraint failed: %s.%s", e.Table, e.Column)
  19. }
  20. func validateNotNull(schema *Schema, row Row) error {
  21. for _, column := range schema.Columns {
  22. if !column.Nullable && row[column.Name] == nil {
  23. return &NotNullViolationError{Table: schema.Name, Column: column.Name}
  24. }
  25. }
  26. return nil
  27. }