common.go 784 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package repositories
  2. import (
  3. "context"
  4. "database/sql"
  5. "errors"
  6. "github.com/lib/pq"
  7. )
  8. const (
  9. FKViolationErrorCode = "23503"
  10. DuplicateKeyErrorCode = "23505"
  11. )
  12. type DB interface {
  13. SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
  14. GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
  15. ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
  16. }
  17. func IsFKViolationError(err error) bool {
  18. var pgErr *pq.Error
  19. if errors.As(err, &pgErr) {
  20. return pgErr.Code == FKViolationErrorCode
  21. }
  22. return false
  23. }
  24. func IsDuplicateKeyError(err error) bool {
  25. var pgErr *pq.Error
  26. if errors.As(err, &pgErr) {
  27. return pgErr.Code == DuplicateKeyErrorCode
  28. }
  29. return false
  30. }