mysql

package module
v1.0.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 mysql provides database/sql-backed MySQL access with explicit transactions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Classify

func Classify(err error) error

Classify maps supported native error codes while preserving the original cause.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client owns its configured infrastructure client and supports concurrent operations.

func New

func New(ctx context.Context, c Config) (*Client, error)

New constructs and validates an explicitly owned instance. See package documentation for defaults and cleanup.

func (*Client) Batch

func (c *Client) Batch(ctx context.Context, ops []Operation) ([]sql.Result, error)

Batch executes commands sequentially in one transaction, rolling back on any error.

func (*Client) BeginTx

func (c *Client) BeginTx(ctx context.Context, o TxOptions) (*Tx, error)

BeginTx opens a transaction that the caller must commit or roll back.

func (*Client) Close

func (c *Client) Close() error

Close releases owned resources. See package documentation for shutdown ordering.

func (*Client) Exec

func (c *Client) Exec(ctx context.Context, q string, a ...any) (sql.Result, error)

Exec executes a parameterized SQL command and returns its result.

func (*Client) Get

func (c *Client) Get(ctx context.Context, dst any, q string, a ...any) error

Get reads one value into the requested representation.

func (*Client) Native

func (c *Client) Native() *sql.DB

Native provides an explicit advanced driver escape hatch; preserve SDK ownership rules.

func (*Client) Ping

func (c *Client) Ping(ctx context.Context) error

Ping checks connectivity using the supplied context.

func (*Client) Prepare

func (c *Client) Prepare(ctx context.Context, q string) (*Statement, error)

Prepare creates an owned prepared statement that must be closed.

func (*Client) Query

func (c *Client) Query(ctx context.Context, q string, a ...any) (db.Rows, error)

Query opens a row stream; the caller must close it.

func (*Client) QueryRow

func (c *Client) QueryRow(ctx context.Context, q string, a ...any) db.Row

QueryRow returns a single-row scanner; call Scan to execute or observe its error.

func (*Client) Select

func (c *Client) Select(ctx context.Context, dst any, q string, a ...any) error

Select scans all result rows into a slice pointer.

func (*Client) Stats

func (c *Client) Stats() sql.DBStats

Stats returns a concurrency-safe statistics snapshot.

func (*Client) WithinTx

func (c *Client) WithinTx(ctx context.Context, o TxOptions, fn func(*Tx) error) (err error)

WithinTx runs a callback transactionally, rolling back on failure or panic.

type Cluster

type Cluster struct {
	// contains filtered or unexported fields
}

Cluster owns all configured clients. Writes/transactions belong on Writer; Reader is an explicit consistency choice.

func NewCluster

func NewCluster(ctx context.Context, cfg ClusterConfig) (*Cluster, error)

NewCluster opens pools atomically, cleaning up previously opened pools on any initialization failure.

func (*Cluster) Close

func (c *Cluster) Close() error

Close releases replicas in reverse order and joins close errors.

func (*Cluster) Reader

func (c *Cluster) Reader(consistency Consistency) (*Client, error)

Reader rotates replicas for Eventual, falling back to primary when none are configured. Strong always uses primary. Replication lag, concurrent writes and failover can invalidate read-after-write assumptions on replicas.

func (*Cluster) Writer

func (c *Cluster) Writer() *Client

Writer returns the primary client for writes and read-after-write consistency.

type ClusterConfig

type ClusterConfig struct {
	Primary  Config
	Replicas []Config
}

ClusterConfig configures independently owned primary/replica pools. Replicas are explicit and are never guessed from SQL.

type Config

type Config struct {
	Hooks          db.Hooks
	QueryLogger    *slog.Logger
	QueryTimeout   time.Duration
	AllowPlaintext bool
	Observer       instrument.Hook
	DSN            string
	// MaxOpenConnections bounds concurrent connections; zero defaults to 32.
	MaxOpenConnections int
	// MaxIdleConnections must not exceed the effective open-connection bound.
	MaxIdleConnections                       int
	MaxLifetime, MaxIdleTime, ConnectTimeout time.Duration
	TLS                                      *tls.Config
}

Config contains explicit constructor settings; see package documentation for defaults.

type Consistency

type Consistency int

Consistency selects primary freshness or potentially stale replica reads.

const (
	Strong Consistency = iota
	Eventual
)

type Operation

type Operation struct {
	SQL  string
	Args []any
}

Operation contains a parameterized SQL command and its arguments.

type Statement

type Statement struct {
	// contains filtered or unexported fields
}

Statement owns a prepared SQL statement and borrows the Client's pool and instrumentation.

func (*Statement) Close

func (s *Statement) Close() error

Close releases this statement. The client remains caller-owned.

func (*Statement) Exec

func (s *Statement) Exec(ctx context.Context, args ...any) (sql.Result, error)

Exec executes prepared bound arguments with configured timeout and diagnostics.

func (*Statement) Get

func (s *Statement) Get(ctx context.Context, dest any, args ...any) error

Get scans one record, returning sql.ErrNoRows for an empty result.

func (*Statement) Native

func (s *Statement) Native() *sql.Stmt

Native exposes the standard SQL handle as an advanced escape hatch.

func (*Statement) Query

func (s *Statement) Query(ctx context.Context, args ...any) (db.Rows, error)

Query opens a prepared row stream. Close releases stream ownership and instrumentation.

func (*Statement) QueryRow

func (s *Statement) QueryRow(ctx context.Context, args ...any) db.Row

QueryRow executes only on Scan, avoiding resource acquisition for an unused handle.

func (*Statement) Select

func (s *Statement) Select(ctx context.Context, dest any, args ...any) error

Select replaces the destination slice on successful result consumption.

type Tx

type Tx struct {
	// contains filtered or unexported fields
}

Tx owns a transaction; do not share it concurrently.

func (*Tx) Commit

func (t *Tx) Commit() error

Commit commits the transaction and reports commit errors.

func (*Tx) CommitContext

func (t *Tx) CommitContext(ctx context.Context) error

CommitContext checks cancellation before committing. The SQL transaction remains tied to its BeginTx context.

func (*Tx) Exec

func (t *Tx) Exec(ctx context.Context, q string, a ...any) (sql.Result, error)

Exec executes a parameterized SQL command and returns its result.

func (*Tx) Get

func (t *Tx) Get(ctx context.Context, d any, q string, a ...any) error

Get reads one value into the requested representation.

func (*Tx) Native

func (t *Tx) Native() *sql.Tx

Native provides an explicit advanced driver escape hatch; preserve SDK ownership rules.

func (*Tx) Query

func (t *Tx) Query(ctx context.Context, q string, a ...any) (db.Rows, error)

Query opens a row stream; the caller must close it.

func (*Tx) QueryRow

func (t *Tx) QueryRow(ctx context.Context, q string, a ...any) db.Row

QueryRow returns a single-row scanner; call Scan to execute or observe its error.

func (*Tx) Rollback

func (t *Tx) Rollback() error

Rollback rolls back the transaction.

func (*Tx) RollbackContext

func (t *Tx) RollbackContext(ctx context.Context) error

RollbackContext always attempts cleanup, including for a canceled context. Context is used for diagnostic correlation.

func (*Tx) Select

func (t *Tx) Select(ctx context.Context, d any, q string, a ...any) error

Select scans all result rows into a slice pointer.

type TxOptions

type TxOptions struct {
	Isolation sql.IsolationLevel
	ReadOnly  bool
}

TxOptions configures transaction isolation and access.

Jump to

Keyboard shortcuts

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