Documentation
¶
Overview ¶
Package pgxhelper provides a thin wrapper around pgxpool to simplify common database operations.
Index ¶
- type DBHelper
- func (d *DBHelper) Close()
- func (d *DBHelper) Connect(connStr string, timeout time.Duration) error
- func (d *DBHelper) Exec(ctx context.Context, query string, args ...any) (int64, error)
- func (d *DBHelper) Get(ctx context.Context, dest any, query string, args ...any) error
- func (d *DBHelper) Ping(ctx context.Context) error
- func (d *DBHelper) Querier(ctx context.Context) Querier
- func (d *DBHelper) Select(ctx context.Context, dest any, query string, args ...any) error
- func (d *DBHelper) WithinTransaction(ctx context.Context, fn func(ctx context.Context) error, opt ...pgx.TxOptions) error
- type Option
- type Querier
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DBHelper ¶
type DBHelper struct {
// contains filtered or unexported fields
}
DBHelper is a wrapper around pgxpool.Pool to simplify common database operations.
func (*DBHelper) Close ¶
func (d *DBHelper) Close()
Close closes all connections in the pool and prevents further use.
func (*DBHelper) Connect ¶
Connect establishes a connection to the database using the provided connection string and timeout. It creates a new pgxpool.Pool and pings the database to ensure the connection is live.
func (*DBHelper) Exec ¶
Exec executes a query that doesn't return rows, such as INSERT, UPDATE, or DELETE. It returns the number of rows affected.
func (*DBHelper) Querier ¶
Querier returns the appropriate querier from the context. If the context contains a transaction, it returns the transaction. Otherwise, it returns the connection pool.
type Option ¶
type Option func(*DBHelper)
Option is a functional option for configuring a DBHelper.
func WithScanAPI ¶
WithScanAPI is a functional option to set the pgxscan.API for the DBHelper.
type Querier ¶
type Querier interface {
Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error)
Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)
QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row
SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults
CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)
}
Querier is an interface that abstracts database operations. It is implemented by *pgxpool.Pool, *pgx.Conn, and pgx.Tx. This allows functions to accept any of these types as a querier.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package sqlset provides a pgxhelper wrapper that works with github.com/theprogrammer67/sqlset.
|
Package sqlset provides a pgxhelper wrapper that works with github.com/theprogrammer67/sqlset. |