55 lines
2.1 KiB
Go
55 lines
2.1 KiB
Go
package dbx
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
)
|
|
|
|
// Querier is the common interface satisfied by pgxpool.Pool, pgx.Tx, and pgx.Conn.
|
|
type Querier interface {
|
|
Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)
|
|
Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
|
|
QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
|
|
SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults
|
|
CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)
|
|
}
|
|
|
|
// DB extends Querier with transaction support and lifecycle management.
|
|
type DB interface {
|
|
Querier
|
|
Begin(ctx context.Context) (pgx.Tx, error)
|
|
BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)
|
|
Ping(ctx context.Context) error
|
|
Close()
|
|
}
|
|
|
|
// Logger is the interface for pluggable structured logging.
|
|
type Logger interface {
|
|
Debug(ctx context.Context, msg string, fields ...any)
|
|
Info(ctx context.Context, msg string, fields ...any)
|
|
Warn(ctx context.Context, msg string, fields ...any)
|
|
Error(ctx context.Context, msg string, fields ...any)
|
|
}
|
|
|
|
// MetricsHook provides optional callbacks for observability.
|
|
// All fields are nil-safe — only set the hooks you need.
|
|
type MetricsHook struct {
|
|
OnQueryStart func(ctx context.Context, node string, sql string)
|
|
OnQueryEnd func(ctx context.Context, node string, sql string, err error, duration time.Duration)
|
|
OnRetry func(ctx context.Context, node string, attempt int, err error)
|
|
OnNodeDown func(ctx context.Context, node string, err error)
|
|
OnNodeUp func(ctx context.Context, node string)
|
|
OnReplicaFallback func(ctx context.Context, fromNode string, toNode string)
|
|
}
|
|
|
|
// nopLogger is a Logger that discards all output.
|
|
type nopLogger struct{}
|
|
|
|
func (nopLogger) Debug(context.Context, string, ...any) {}
|
|
func (nopLogger) Info(context.Context, string, ...any) {}
|
|
func (nopLogger) Warn(context.Context, string, ...any) {}
|
|
func (nopLogger) Error(context.Context, string, ...any) {}
|