onasty/internal/store/psqlutil/psqlutil.go(view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
package psqlutil
import (
"context"
"errors"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxpool"
)
type DB struct{ *pgxpool.Pool }
func Connect(ctx context.Context, dsn string) (*DB, error) {
db, err := pgxpool.New(ctx, dsn)
if err != nil {
return nil, err
}
if err := db.Ping(ctx); err != nil {
return nil, err
}
return &DB{
Pool: db,
}, nil
}
func (db *DB) Close() error {
db.Pool.Close()
return nil
}
// IsDuplicateErr function that checks if the error is a duplicate key violation.
func IsDuplicateErr(err error, constraintName ...string) bool {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
if len(constraintName) == 0 || len(constraintName) == 1 {
return pgErr.Code == "23505" && // unique_violation
pgErr.ConstraintName == constraintName[0]
}
return pgErr.Code == "23505" // unique_violation
}
return false
}
|