Documentation
¶
Index ¶
- func AsConflict(err error, msg string) error
- func AsDuplicate(err error, dup error) error
- func AsNotFound(err error, msg string) error
- func AsValidation(err error, msg string) error
- func IsForeignKeyViolation(err error) bool
- func IsInvalidTextRepresentation(err error) bool
- func IsNumericOutOfRange(err error) bool
- func IsStringTooLong(err error) bool
- func IsUniqueViolation(err error, constraint string) bool
- func TenantExec(ctx context.Context, db Beginner, id Identity, sql string, args ...any) (pgconn.CommandTag, error)
- func TenantScan(ctx context.Context, db Beginner, id Identity, sql string, args []any, ...) error
- func WithService(ctx context.Context, db Beginner, ...) error
- func WithTenant(ctx context.Context, db Beginner, id Identity, ...) error
- type Beginner
- type DB
- func (d *DB) Acquire(ctx context.Context) (*pgxpool.Conn, error)
- func (d *DB) Begin(ctx context.Context) (pgx.Tx, error)
- func (d *DB) Close()
- func (d *DB) Ping(ctx context.Context) error
- func (d *DB) Pool() *pgxpool.Pool
- func (d *DB) Read(ctx context.Context, fn func(ctx context.Context, q DBTX) error) error
- func (d *DB) Reset()
- func (d *DB) Tx(ctx context.Context) DBTX
- func (d *DB) Within(ctx context.Context, fn func(ctx context.Context) error) error
- type DBTX
- type Identity
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AsConflict ¶ added in v0.11.3
AsConflict maps a foreign-key violation to 409 with msg (the "still referenced, cannot delete" reading of 23503). Any other error passes through; use AsNotFound when the "referenced row missing" reading is the intended one.
func AsDuplicate ¶
func AsNotFound ¶
func AsValidation ¶ added in v0.11.3
AsValidation maps "the caller's input was rejected by the database" to a 400 E_VALIDATION carrying msg: string overflow, unparseable values and numeric range errors. Every other error (transport, constraint shape the caller must handle itself, programming errors) is returned untouched so the caller keeps its own sentinel semantics.
fid, err := r.createField(ctx, ...)
if err != nil { return pg.AsValidation(err, "field name exceeds the column width") }
Foreign-key violations are deliberately NOT folded in: 23503 means different things on insert (related row absent → 400/404) and delete (still referenced → 400/409), so callers pick with AsNotFound/IsForeignKeyViolation. It returns a *webx.Error (not the internal apierr carrier) so it drops straight into any handler that materializes errors through webx.WriteErr — including products with their own error-code bridge, which only recognizes *webx.Error and would otherwise re-bucket this as E_INTERNAL.
func IsForeignKeyViolation ¶ added in v0.11.3
IsForeignKeyViolation reports whether err is a foreign-key rejection (SQLSTATE 23503) — the referenced row is missing on insert, or the row is still referenced on delete.
func IsInvalidTextRepresentation ¶ added in v0.11.3
IsInvalidTextRepresentation reports whether err is a value the database could not parse as the target type (SQLSTATE 22P02) — typically a malformed UUID reaching a ::uuid cast or a path parameter used verbatim in a query.
func IsNumericOutOfRange ¶ added in v0.11.3
IsNumericOutOfRange reports whether err is a numeric overflow (SQLSTATE 22003).
func IsStringTooLong ¶ added in v0.11.3
IsStringTooLong reports whether err is PostgreSQL's varchar/char overflow (SQLSTATE 22001). Service layers validate maxLength themselves; this is the backstop for the columns nobody remembered to bound.
func IsUniqueViolation ¶
func TenantExec ¶ added in v0.9.0
func TenantExec(ctx context.Context, db Beginner, id Identity, sql string, args ...any) (pgconn.CommandTag, error)
TenantExec runs ONE statement inside a transaction that carries the tenant GUCs, committing immediately. Multi-statement units of work belong in WithTenant. Each call costs one BEGIN/COMMIT round trip — that is the price of correct tenant scoping; do not be tempted to set session-level GUCs on a pooled connection instead (cross-tenant leak).
func TenantScan ¶ added in v0.9.0
func TenantScan(ctx context.Context, db Beginner, id Identity, sql string, args []any, dest ...any) error
TenantScan runs ONE query inside the same scoped transaction and scans a SINGLE row into dest (pgx QueryRow semantics). Deliberately NOT a rows-returning helper: streaming rows would force the transaction (and the tenant scope) to outlive the call.