Documentation
¶
Overview ¶
Package db provides a PostgreSQL database client wrapper that integrates sqlx with pgx, offering transaction support, query tracing, and automatic snake_case field mapping for struct tags.
Index ¶
- func ToSnakeCase(s string) string
- type Client
- func (db *Client) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (db *Client) ExecNamed(ctx context.Context, query string, arg any) (sql.Result, error)
- func (db *Client) IsReadOnly(ctx context.Context) (bool, error)
- func (db *Client) RunInTransaction(ctx context.Context, txFunc TxFunc, opts ...TxOption) (err error)
- func (db *Client) Select(ctx context.Context, ptr any, query string, args ...any) error
- func (db *Client) SelectRow(ctx context.Context, ptr any, query string, args ...any) error
- type DB
- type Option
- type Transactional
- type Tx
- func (t *Tx) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (t *Tx) ExecNamed(ctx context.Context, query string, arg any) (sql.Result, error)
- func (t *Tx) Select(ctx context.Context, ptr any, query string, args ...any) error
- func (t *Tx) SelectRow(ctx context.Context, ptr any, query string, args ...any) error
- type TxFunc
- type TxOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ToSnakeCase ¶
ToSnakeCase converts a string from camelCase or PascalCase to snake_case. This is used for automatic mapping of struct field names to database column names.
Types ¶
type Client ¶
Client wraps a sqlx.DB instance with additional functionality for query tracing and transaction management. It is safe for concurrent use.
func Open ¶
Open establishes a connection to a PostgreSQL database using the provided DSN. It applies the given options, validates the connection, and returns a Client. Returns an error if the configuration cannot be parsed or the connection fails.
func (*Client) Exec ¶
Exec executes a query that does not return rows, such as INSERT, UPDATE, or DELETE. Returns the sql.Result and any error that occurs during execution.
func (*Client) ExecNamed ¶
ExecNamed executes a named-parameter query that does not return rows. The arg parameter should be a struct or map that provides values for named placeholders. Returns the sql.Result and any error that occurs during execution.
func (*Client) IsReadOnly ¶ added in v1.50.1
IsReadOnly checks whether the current database connection is in read-only mode. Returns true if the connection is read-only, false otherwise. Returns an error if the query fails.
func (*Client) RunInTransaction ¶
func (db *Client) RunInTransaction(ctx context.Context, txFunc TxFunc, opts ...TxOption) (err error)
RunInTransaction executes the provided function within a database transaction. It automatically commits on success and rolls back on error or panic. Transaction options can be provided to configure isolation level and other settings. Returns an error if the transaction cannot be started, committed, or rolled back.
type DB ¶
type DB interface {
Select(ctx context.Context, ptr any, query string, args ...any) error
SelectRow(ctx context.Context, ptr any, query string, args ...any) error
Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
ExecNamed(ctx context.Context, query string, arg any) (sql.Result, error)
}
DB defines the interface for basic database operations.
type Option ¶
type Option func(db *Client)
Option is a function that configures a Client.
func WithQueryTracer ¶
func WithQueryTracer(tracers ...pgx.QueryTracer) Option
WithQueryTracer registers one or more pgx QueryTracers for query lifecycle events. Multiple tracers are invoked sequentially for each query.
type Transactional ¶
type Transactional interface {
RunInTransaction(ctx context.Context, txFunc TxFunc, opts ...TxOption) error
}
Transactional defines the interface for transactional database operations.
type Tx ¶
Tx wraps a sqlx.Tx with convenience methods for database operations.
type TxOption ¶
type TxOption func(options *txOptions)
TxOption is a function that configures transaction options.
func IsolationLevel ¶
func IsolationLevel(level sql.IsolationLevel) TxOption
IsolationLevel sets the transaction isolation level.
func MetricsLabel ¶ added in v1.55.0
MetricsLabel sets a label for transaction metrics.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package jsonb provides a custom type for handling JSONB data in PostgreSQL.
|
Package jsonb provides a custom type for handling JSONB data in PostgreSQL. |
|
Package query provides a fluent query builder for PostgreSQL using the Masterminds/squirrel library with dollar-sign placeholder formatting.
|
Package query provides a fluent query builder for PostgreSQL using the Masterminds/squirrel library with dollar-sign placeholder formatting. |