database

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package database defines shared scanning, hooks and SQL value contracts without importing drivers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReportQuery

func ReportQuery(ctx context.Context, h Hooks, l *slog.Logger, e QueryEvent)

ReportQuery emits only bounded operation/success/duration attributes to the configured logger. SQL and arguments are never logged.

func ReportTransaction

func ReportTransaction(ctx context.Context, h Hooks, operation string, start time.Time, err error)

ReportTransaction invokes transaction instrumentation without changing the database outcome.

func Scan

func Scan(rows Rows, destination any, many bool) error

Scan maps db tags or case-insensitive snake_case names. Anonymous exported struct pointers are allocated only when mapped. The json tag option decodes JSON into a field. Unknown columns are discarded. Select replaces its destination only on success.

Types

type Hooks

type Hooks struct {
	BeforeBegin      func(context.Context) error
	AfterQuery       func(context.Context, QueryEvent)
	AfterTransaction func(context.Context, TransactionEvent)
}

Hooks run synchronously and must be concurrency safe. BeforeBegin can reject a transaction before acquiring resources.

type JSON

type JSON[T any] struct {
	Data  T
	Valid bool
}

JSON is a nullable JSON SQL value. Use it when scanning a whole JSON column without a tagged struct field.

func (*JSON[T]) Scan

func (j *JSON[T]) Scan(src any) error

Scan accepts database text/bytes or SQL NULL and validates the complete JSON representation.

func (JSON[T]) Value

func (j JSON[T]) Value() (driver.Value, error)

Value implements driver.Valuer for safe parameter binding.

type QueryEvent

type QueryEvent struct {
	Operation, Statement string
	Duration             time.Duration
	Err                  error
}

QueryEvent contains query diagnostics without parameter values. Statement is available only to explicit hooks. Never use Statement as a metric label or log SQL containing literal secrets.

type Row

type Row interface{ Scan(...any) error }

Row scans one result record.

type Rows

type Rows interface {
	Next() bool
	Scan(...any) error
	Columns() ([]string, error)
	Err() error
	Close() error
}

Rows owns a result stream. Close it when stopping before exhaustion.

type TransactionEvent

type TransactionEvent struct {
	Operation string
	Duration  time.Duration
	Err       error
}

TransactionEvent reports a begin, commit, rollback, savepoint or release attempt.

Directories

Path Synopsis
mysql module
postgres module

Jump to

Keyboard shortcuts

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