35 lines
1.2 KiB
Go
35 lines
1.2 KiB
Go
// Package dbutil holds small helpers shared by the handler packages that
|
|
// talk to Postgres via pgx — kept minimal since this is a 3-handler app,
|
|
// not a reason to build a data-access layer.
|
|
package dbutil
|
|
|
|
import "strings"
|
|
|
|
// NullIfEmpty converts an empty string to nil so COALESCE/optional columns
|
|
// stay unset rather than being overwritten with "".
|
|
func NullIfEmpty(s string) interface{} {
|
|
if s == "" {
|
|
return nil
|
|
}
|
|
return s
|
|
}
|
|
|
|
// IsFKViolation reports whether err is a Postgres foreign-key violation —
|
|
// checked by message substring since the handler packages don't otherwise
|
|
// depend on pgconn's error types.
|
|
func IsFKViolation(err error) bool {
|
|
return err != nil && strings.Contains(err.Error(), "violates foreign key constraint")
|
|
}
|
|
|
|
// IsUniqueViolation reports whether err is a Postgres unique-constraint
|
|
// violation, same message-substring approach as IsFKViolation.
|
|
func IsUniqueViolation(err error) bool {
|
|
return err != nil && strings.Contains(err.Error(), "violates unique constraint")
|
|
}
|
|
|
|
// IsCheckViolation reports whether err is a Postgres CHECK-constraint
|
|
// violation, same message-substring approach as IsFKViolation.
|
|
func IsCheckViolation(err error) bool {
|
|
return err != nil && strings.Contains(err.Error(), "violates check constraint")
|
|
}
|