Documentation
¶
Overview ¶
Package pgutil provides helpers for working with pgx (jackc/pgx)
Error checks ¶
IsNoRows reports whether err is or wraps pgx.ErrNoRows (use after QueryRow when a missing row is expected) IsPgErrorCode reports whether err is a PgError with the given SQLSTATE code IsPgUniqueViolation, IsForeignKeyViolation, IsNotNullViolation, and IsCheckViolation are conveniences for common constraint codes (23505, 23503, 23502, 23514). IsSerializationFailure, IsDeadlockDetected, and IsRetryableTxError help classify transaction errors that are commonly retried at the transaction boundary
Timestamp conversion ¶
pgx uses pgtype.Timestamptz for timestamp with time zone. TimestamptzToTime returns *time.Time or nil when invalid TimestamptzToTimeZero returns time.Time or the zero value. TimeToTimestamptz converts *time.Time to pgtype.Timestamptz (invalid if nil) PtrTimeToTime dereferences a *time.Time or returns time.Time{} if nil
Index ¶
- Constants
- func IsCheckViolation(err error) bool
- func IsDeadlockDetected(err error) bool
- func IsForeignKeyViolation(err error) bool
- func IsNoRows(err error) bool
- func IsNotNullViolation(err error) bool
- func IsPgErrorCode(err error, code string) bool
- func IsPgUniqueViolation(err error) bool
- func IsRetryableTxError(err error) bool
- func IsSerializationFailure(err error) bool
- func PgErrorCode(err error) string
- func PtrTimeToTime(t *time.Time) time.Time
- func TimeToTimestamptz(t *time.Time) pgtype.Timestamptz
- func TimestamptzToTime(t pgtype.Timestamptz) *time.Time
- func TimestamptzToTimeZero(t pgtype.Timestamptz) time.Time
Constants ¶
const ( CodeUniqueViolation = "23505" CodeForeignKeyViolation = "23503" CodeNotNullViolation = "23502" CodeCheckViolation = "23514" CodeSerializationFailure = "40001" CodeDeadlockDetected = "40P01" )
Variables ¶
This section is empty.
Functions ¶
func IsCheckViolation ¶ added in v1.4.0
IsCheckViolation reports whether err is a PostgreSQL check constraint violation (SQLSTATE 23514)
func IsDeadlockDetected ¶ added in v1.4.0
IsDeadlockDetected reports whether err is a PostgreSQL deadlock detected error (SQLSTATE 40P01)
func IsForeignKeyViolation ¶
IsForeignKeyViolation reports whether err is a PostgreSQL foreign key violation (SQLSTATE 23503)
func IsNoRows ¶
IsNoRows reports whether err is or wraps pgx.ErrNoRows. Use after QueryRow when a missing row is acceptable
func IsNotNullViolation ¶
IsNotNullViolation reports whether err is a PostgreSQL not null violation (SQLSTATE 23502)
func IsPgErrorCode ¶
IsPgErrorCode reports whether err is or wraps a PgError with the given SQLSTATE code
func IsPgUniqueViolation ¶
IsPgUniqueViolation reports whether err is a PostgreSQL unique constraint violation (SQLSTATE 23505)
func IsRetryableTxError ¶ added in v1.4.0
IsRetryableTxError reports whether err is a transaction error commonly safe to retry at the transaction boundary
func IsSerializationFailure ¶ added in v1.4.0
IsSerializationFailure reports whether err is a PostgreSQL serialization failure (SQLSTATE 40001)
func PgErrorCode ¶
PgErrorCode extracts the SQLSTATE code from err, or "" if err is not a PgError
func PtrTimeToTime ¶
PtrTimeToTime returns *t, or time.Time{} if t is nil
func TimeToTimestamptz ¶
func TimeToTimestamptz(t *time.Time) pgtype.Timestamptz
TimeToTimestamptz converts *time.Time to pgtype.Timestamptz. Returns an invalid Timestamptz if t is nil
func TimestamptzToTime ¶
func TimestamptzToTime(t pgtype.Timestamptz) *time.Time
TimestamptzToTime returns a pointer to the time, or nil if t.Valid is false
func TimestamptzToTimeZero ¶
func TimestamptzToTimeZero(t pgtype.Timestamptz) time.Time
TimestamptzToTimeZero returns t.Time, or time.Time{} if t.Valid is false
Types ¶
This section is empty.