pgutil

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 20, 2026 License: MIT Imports: 5 Imported by: 0

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 are conveniences for common codes (23505, 23503, 23502).

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

This section is empty.

Variables

This section is empty.

Functions

func IsForeignKeyViolation

func IsForeignKeyViolation(err error) bool

IsForeignKeyViolation reports whether err is a PostgreSQL foreign key violation (SQLSTATE 23503).

func IsNoRows

func IsNoRows(err error) bool

IsNoRows reports whether err is or wraps pgx.ErrNoRows. Use after QueryRow when a missing row is acceptable.

func IsNotNullViolation

func IsNotNullViolation(err error) bool

IsNotNullViolation reports whether err is a PostgreSQL not null violation (SQLSTATE 23502).

func IsPgErrorCode

func IsPgErrorCode(err error, code string) bool

IsPgErrorCode reports whether err is or wraps a PgError with the given SQLSTATE code.

func IsPgUniqueViolation

func IsPgUniqueViolation(err error) bool

IsPgUniqueViolation reports whether err is a PostgreSQL unique constraint violation (SQLSTATE 23505).

func PgErrorCode

func PgErrorCode(err error) string

PgErrorCode extracts the SQLSTATE code from err, or "" if err is not a PgError.

func PtrTimeToTime

func PtrTimeToTime(t *time.Time) time.Time

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL