26 lines
820 B
Go
26 lines
820 B
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
// Querier defines an interface for executing queries, which can be a pgxpool.Pool or a pgx.Tx.
|
|
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
|
|
}
|
|
|
|
// getQuerier retrieves a transaction from the context if it exists, otherwise returns the pool.
|
|
// The transaction is expected to be stored in the context with the key "tx".
|
|
func getQuerier(ctx context.Context, db *pgxpool.Pool) Querier {
|
|
if tx, ok := ctx.Value("tx").(pgx.Tx); ok {
|
|
return tx
|
|
}
|
|
return db
|
|
}
|