Documentation
¶
Overview ¶
Package dbx is the common, context-aware database entry point for Go Common.
OpenPostgres provides instrumented database/sql access through pgx. OpenPGXPool provides a native pgxpool with a trace-safe query tracer. OpenSQL supports other registered database/sql drivers. OpenSQL caps pools at 10 open / 5 idle connections by default, with bounded connection lifetime and initial ping. Spans record operation type and outcome, and omit SQL statements, bind values, connection strings, and raw driver error messages.
BuildSelect, BuildInsert, BuildUpdate, and BuildDelete generate PostgreSQL CRUD statements with bound values and validated/quoted identifiers. CopyFrom provides typed pgx binary COPY with the same identifier validation. Resolve external names through a fixed application allowlist before calling Ident; syntactic validation alone is not an authorization policy. For complex queries, prefer generated sqlc methods and context-aware database/sql APIs. Never concatenate caller-controlled values into SQL. Identifiers cannot be represented as bind parameters and must be selected from trusted code.
Package dbx provides context-aware SQL/PostgreSQL connections with OpenTelemetry spans and parameterized CRUD query builders.
Index ¶
- Constants
- func BuildDelete(table Identifier, where []Predicate) (string, []any, error)
- func BuildInsert(table Identifier, values []Assignment) (string, []any, error)
- func BuildSelect(table Identifier, columns []Identifier, where []Predicate, limit int) (string, []any, error)
- func BuildUpdate(table Identifier, values []Assignment, where []Predicate) (string, []any, error)
- func CopyFrom(ctx context.Context, executor CopyFromExecutor, table Identifier, ...) (int64, error)
- func NewPGXQueryTracer() pgx.QueryTracer
- func OpenPGXPool(ctx context.Context, dataSourceName string, configure func(*pgxpool.Config)) (*pgxpool.Pool, error)
- func OpenPostgres(ctx context.Context, dataSourceName string, cfg SQLConfig) (*sql.DB, error)
- func OpenSQL(ctx context.Context, driverName, dataSourceName, dbSystem string, ...) (*sql.DB, error)
- type Assignment
- type CopyFromExecutor
- type Identifier
- type Predicate
- type SQLConfig
Constants ¶
Variables ¶
This section is empty.
Functions ¶
func BuildDelete ¶
func BuildDelete(table Identifier, where []Predicate) (string, []any, error)
BuildDelete creates a parameterized DELETE and requires at least one predicate, preventing accidental table-wide deletion.
func BuildInsert ¶
func BuildInsert(table Identifier, values []Assignment) (string, []any, error)
BuildInsert creates a parameterized INSERT with a RETURNING-free statement. Use QueryRowContext with an explicit RETURNING query when the caller needs returned values, composing only validated identifiers and placeholders.
func BuildSelect ¶
func BuildSelect(table Identifier, columns []Identifier, where []Predicate, limit int) (string, []any, error)
BuildSelect creates a parameterized SELECT. A non-positive limit means no LIMIT clause; negative limits are rejected.
func BuildUpdate ¶
func BuildUpdate(table Identifier, values []Assignment, where []Predicate) (string, []any, error)
BuildUpdate creates a parameterized UPDATE and requires at least one predicate, preventing accidental table-wide updates.
func CopyFrom ¶
func CopyFrom(ctx context.Context, executor CopyFromExecutor, table Identifier, columns []Identifier, source pgx.CopyFromSource) (int64, error)
CopyFrom performs PostgreSQL binary COPY using validated identifiers and pgx's typed row source. Open the pool with OpenPGXPool so the operation is traced. Row values travel through pgx's COPY protocol; they are never interpolated into SQL. Resolve external names through a fixed allowlist before calling Ident, because syntactic validation is not authorization.
func NewPGXQueryTracer ¶
func NewPGXQueryTracer() pgx.QueryTracer
NewPGXQueryTracer returns a privacy-conscious pgx QueryTracer. It can be assigned to pgx.ConnConfig.Tracer when an application owns pool creation.
func OpenPGXPool ¶
func OpenPGXPool(ctx context.Context, dataSourceName string, configure func(*pgxpool.Config)) (*pgxpool.Pool, error)
OpenPGXPool opens a native pgx pool and installs a query tracer which records operation kind, duration, and success status only. SQL text, parameters, connection details, and raw error messages are never added to spans.
func OpenPostgres ¶
OpenPostgres opens a PostgreSQL database/sql pool using pgx's stdlib driver. SQL values remain bind parameters; do not interpolate untrusted values into SQL strings. For SQL injection-resistant CRUD, use the builders in builder.go.
func OpenSQL ¶
func OpenSQL(ctx context.Context, driverName, dataSourceName, dbSystem string, cfg SQLConfig) (*sql.DB, error)
OpenSQL opens a database/sql connection using an already-registered driver. It instruments operations without exporting SQL text, bind arguments, DSNs, or raw driver error messages. The caller owns db.Close().
Types ¶
type Assignment ¶
type Assignment struct {
Column Identifier
Value any
}
Assignment binds a value to a trusted column identifier.
type CopyFromExecutor ¶
type CopyFromExecutor interface {
CopyFrom(context.Context, pgx.Identifier, []string, pgx.CopyFromSource) (int64, error)
}
CopyFromExecutor is implemented by pgx connections, transactions, and pools.
type Identifier ¶
type Identifier struct {
// contains filtered or unexported fields
}
Identifier is a validated and quoted PostgreSQL identifier. Construct it from trusted application schema names, never directly from request input.
func Ident ¶
func Ident(name string) (Identifier, error)
Ident validates one identifier or a schema-qualified identifier such as "public.users". Each part is quoted separately in generated SQL.
type Predicate ¶
type Predicate struct {
// contains filtered or unexported fields
}
Eq creates a parameterized equality predicate. A nil value is rendered as IS NULL, avoiding PostgreSQL's three-valued NULL comparison behavior.
func Eq ¶
func Eq(column Identifier, value any) Predicate
type SQLConfig ¶
type SQLConfig struct {
MaxOpenConnections int
MaxIdleConnections int
ConnMaxLifetime time.Duration
ConnMaxIdleTime time.Duration
PingTimeout time.Duration
}
SQLConfig controls database/sql pool behavior. Zero values use bounded defaults: 10 open / 5 idle connections, 30-minute max lifetime, 5-minute idle timeout, and a 5-second initial ping. A negative MaxIdleConnections disables idle connections.