Documentation
¶
Overview ¶
Package mysql provides database/sql-backed MySQL access with explicit transactions.
Index ¶
- func Classify(err error) error
- type Client
- func (c *Client) Batch(ctx context.Context, ops []Operation) ([]sql.Result, error)
- func (c *Client) BeginTx(ctx context.Context, o TxOptions) (*Tx, error)
- func (c *Client) Close() error
- func (c *Client) Exec(ctx context.Context, q string, a ...any) (sql.Result, error)
- func (c *Client) Get(ctx context.Context, dst any, q string, a ...any) error
- func (c *Client) Native() *sql.DB
- func (c *Client) Ping(ctx context.Context) error
- func (c *Client) Prepare(ctx context.Context, q string) (*Statement, error)
- func (c *Client) Query(ctx context.Context, q string, a ...any) (db.Rows, error)
- func (c *Client) QueryRow(ctx context.Context, q string, a ...any) db.Row
- func (c *Client) Select(ctx context.Context, dst any, q string, a ...any) error
- func (c *Client) Stats() sql.DBStats
- func (c *Client) WithinTx(ctx context.Context, o TxOptions, fn func(*Tx) error) (err error)
- type Cluster
- type ClusterConfig
- type Config
- type Consistency
- type Operation
- type Statement
- func (s *Statement) Close() error
- func (s *Statement) Exec(ctx context.Context, args ...any) (sql.Result, error)
- func (s *Statement) Get(ctx context.Context, dest any, args ...any) error
- func (s *Statement) Native() *sql.Stmt
- func (s *Statement) Query(ctx context.Context, args ...any) (db.Rows, error)
- func (s *Statement) QueryRow(ctx context.Context, args ...any) db.Row
- func (s *Statement) Select(ctx context.Context, dest any, args ...any) error
- type Tx
- func (t *Tx) Commit() error
- func (t *Tx) CommitContext(ctx context.Context) error
- func (t *Tx) Exec(ctx context.Context, q string, a ...any) (sql.Result, error)
- func (t *Tx) Get(ctx context.Context, d any, q string, a ...any) error
- func (t *Tx) Native() *sql.Tx
- func (t *Tx) Query(ctx context.Context, q string, a ...any) (db.Rows, error)
- func (t *Tx) QueryRow(ctx context.Context, q string, a ...any) db.Row
- func (t *Tx) Rollback() error
- func (t *Tx) RollbackContext(ctx context.Context) error
- func (t *Tx) Select(ctx context.Context, d any, q string, a ...any) error
- type TxOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client owns its configured infrastructure client and supports concurrent operations.
func New ¶
New constructs and validates an explicitly owned instance. See package documentation for defaults and cleanup.
func (*Client) Batch ¶
Batch executes commands sequentially in one transaction, rolling back on any error.
func (*Client) Close ¶
Close releases owned resources. See package documentation for shutdown ordering.
func (*Client) Native ¶
Native provides an explicit advanced driver escape hatch; preserve SDK ownership rules.
func (*Client) QueryRow ¶
QueryRow returns a single-row scanner; call Scan to execute or observe its error.
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) 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.
type ClusterConfig ¶
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 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) Exec ¶
Exec executes prepared bound arguments with configured timeout and diagnostics.
func (*Statement) Query ¶
Query opens a prepared row stream. Close releases stream ownership and instrumentation.
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx owns a transaction; do not share it concurrently.
func (*Tx) CommitContext ¶
CommitContext checks cancellation before committing. The SQL transaction remains tied to its BeginTx context.
func (*Tx) Native ¶
Native provides an explicit advanced driver escape hatch; preserve SDK ownership rules.
func (*Tx) QueryRow ¶
QueryRow returns a single-row scanner; call Scan to execute or observe its error.
func (*Tx) RollbackContext ¶
RollbackContext always attempts cleanup, including for a canceled context. Context is used for diagnostic correlation.
type TxOptions ¶
type TxOptions struct {
Isolation sql.IsolationLevel
ReadOnly bool
}
TxOptions configures transaction isolation and access.