pgxpool

package
v0.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2025 License: GPL-3.0 Imports: 7 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateTransaction

func CreateTransaction(
	pool *pgxpool.Pool,
	fn TransactionWithCtxFn,
) error

CreateTransaction creates a transaction for the database

Parameters:

  • pool: The pgxpool.Pool instance
  • fn: The function to execute within the transaction

Returns:

  • error: An error if the transaction fails, otherwise nil

func CreateTransactionWithCtx

func CreateTransactionWithCtx(
	ctx context.Context,
	pool *pgxpool.Pool,
	fn TransactionWithCtxFn,
) error

CreateTransactionWithCtx creates a transaction for the database with context

Parameters:

  • ctx: The context for the transaction
  • pool: The pgxpool.Pool instance
  • fn: The function to execute within the transaction

Returns:

  • error: An error if the transaction fails, otherwise nil

Types

type Config

type Config interface {
	DataSourceName() string
	MaxOpenConnections() int
	MaxIdleConnections() int
	ConnectionMaxLifetime() time.Duration
	ConnectionMaxIdleTime() time.Duration
	HealthCheckPeriod() time.Duration
	ConnectionMaxLifetimeJitter() time.Duration
	ParsedConfig() (*pgxpool.Config, error)
}

Config interface

type DefaultPoolHandler

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

DefaultPoolHandler struct

func NewDefaultPoolHandler

func NewDefaultPoolHandler(
	config Config,
) (*DefaultPoolHandler, error)

NewDefaultPoolHandler creates a new connection

func (*DefaultPoolHandler) Connect

func (d *DefaultPoolHandler) Connect() (*pgxpool.Pool, error)

Connect returns a new connection pool

func (*DefaultPoolHandler) Disconnect

func (d *DefaultPoolHandler) Disconnect()

Disconnect closes the connection pool

func (*DefaultPoolHandler) Pool

func (d *DefaultPoolHandler) Pool() (*pgxpool.Pool, error)

Pool returns the connection pool

Returns:

  • *pgxpool.Pool: the connection pool
  • error: if the connection is not established

type DefaultService

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

DefaultService is the default service struct

func NewDefaultService

func NewDefaultService(pool *pgxpool.Pool) (
	instance *DefaultService,
	err error,
)

NewDefaultService creates a new default service

Parameters:

  • pool: the pgxpool.Pool instance

Returns:

  • *DefaultService: the DefaultService instance
  • error: if any error occurs

func (*DefaultService) ClearStatTicker added in v0.5.6

func (d *DefaultService) ClearStatTicker()

ClearStatTicker clears the stat ticker

func (*DefaultService) CreateTransaction

func (d *DefaultService) CreateTransaction(
	fn TransactionWithCtxFn,
) error

CreateTransaction creates a transaction for the database

Parameters:

  • fn: the function to execute within the transaction

Returns:

  • error: if any error occurs

func (*DefaultService) CreateTransactionWithCtx

func (d *DefaultService) CreateTransactionWithCtx(
	ctx context.Context,
	fn TransactionWithCtxFn,
) error

CreateTransactionWithCtx creates a transaction for the database with a context

Parameters:

  • ctx: the context to use
  • fn: the function to execute within the transaction

Returns:

  • error: if any error occurs

func (*DefaultService) Exec

func (d *DefaultService) Exec(query *string, params ...interface{}) (
	*pgconn.CommandTag,
	error,
)

Exec executes a query with parameters and returns the result

Parameters:

  • query: the SQL query to execute
  • params: the parameters for the SQL query

Returns:

  • *pgconn.CommandTag: the command tag result
  • error: if any error occurs

func (*DefaultService) ExecWithCtx

func (d *DefaultService) ExecWithCtx(
	ctx context.Context,
	query *string,
	params ...interface{},
) (
	*pgconn.CommandTag,
	error,
)

ExecWithCtx executes a query with parameters and returns the result with a context

Parameters:

  • ctx: the context to use
  • query: the SQL query to execute
  • params: the parameters for the SQL query

Returns:

  • *pgconn.CommandTag: the command tag result
  • error: if any error occurs

func (*DefaultService) Migrate

func (d *DefaultService) Migrate(queries ...string) error

Migrate migrates the database

Parameters:

  • queries: the SQL queries to execute

Returns:

  • error: if any error occurs

func (*DefaultService) Pool

func (d *DefaultService) Pool() *pgxpool.Pool

Pool returns the pool

Returns:

  • *pgxpool.Pool: the pgxpool.Pool instance

func (*DefaultService) Query added in v0.5.4

func (d *DefaultService) Query(
	query *string,
	params ...interface{},
) (pgx.Rows, error)

Query runs a query with parameters and returns the result

Parameters:

  • query: the SQL query to execute
  • params: the parameters for the SQL query

Returns:

  • pgx.Rows: the result rows
  • error: if any error occurs

func (*DefaultService) QueryRow

func (d *DefaultService) QueryRow(
	query *string,
	params ...interface{},
) (pgx.Row, error)

QueryRow runs a query row with parameters and returns the result row

Parameters:

  • query: the SQL query to execute
  • params: the parameters for the SQL query

Returns:

  • pgx.Row: the result row
  • error: if any error occurs

func (*DefaultService) QueryRowWithCtx

func (d *DefaultService) QueryRowWithCtx(
	ctx context.Context,
	query *string,
	params ...interface{},
) (pgx.Row, error)

QueryRowWithCtx runs a query row with parameters and returns the result row with a context

Parameters:

  • ctx: the context to use
  • query: the SQL query to execute
  • params: the parameters for the SQL query

Returns:

  • pgx.Row: the result row
  • error: if any error occurs

func (*DefaultService) QueryWithCtx added in v0.5.4

func (d *DefaultService) QueryWithCtx(
	ctx context.Context,
	query *string,
	params ...interface{},
) (pgx.Rows, error)

QueryWithCtx runs a query with parameters and returns the result with a context

Parameters:

  • ctx: the context to use
  • query: the SQL query to execute
  • params: the parameters for the SQL query

Returns:

  • pgx.Rows: the result rows

func (*DefaultService) ScanRow

func (d *DefaultService) ScanRow(
	row pgx.Row,
	destinations ...interface{},
) error

ScanRow scans a row

Parameters:

- row: the pgx.Row instance - destinations: the destinations to scan the row into

Returns:

- error: if any error occurs

func (*DefaultService) SetStatTicker added in v0.5.6

func (d *DefaultService) SetStatTicker(
	duration time.Duration,
	fn func(*pgxpool.Stat),
)

SetStatTicker sets a stat ticker

Parameters:

  • duration: the duration of the ticker
  • fn: the function to execute on each tick, receiving the pgxpool.Stat

type PoolConfig

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

PoolConfig struct

func NewPoolConfig

func NewPoolConfig(
	dataSourceName string,
	maxOpenConnections,
	maxIdleConnections int,
	connectionMaxIdleTime,
	connectionMaxLifetime,
	healthCheckPeriod,
	connectionMaxLifetimeJitter time.Duration,
) (*PoolConfig, error)

NewPoolConfig creates a new pool configuration

Parameters:

  • dataSourceName: the data source name
  • maxOpenConnections: the maximum number of open connections
  • maxIdleConnections: the maximum number of idle connections
  • connectionMaxIdleTime: the maximum idle time for a connection
  • connectionMaxLifetime: the maximum lifetime for a connection
  • healthCheckPeriod: the period for health checks
  • connectionMaxLifetimeJitter: the jitter for the maximum lifetime of a connection

Returns:

  • *PoolConfig: the pool configuration
  • error: if any error occurs

func (*PoolConfig) ConnectionMaxIdleTime

func (p *PoolConfig) ConnectionMaxIdleTime() time.Duration

ConnectionMaxIdleTime returns the connection max idle time

Returns:

  • time.Duration: the connection max idle time

func (*PoolConfig) ConnectionMaxLifetime

func (p *PoolConfig) ConnectionMaxLifetime() time.Duration

ConnectionMaxLifetime returns the connection max lifetime

Returns:

  • time.Duration: the connection max lifetime

func (*PoolConfig) ConnectionMaxLifetimeJitter

func (p *PoolConfig) ConnectionMaxLifetimeJitter() time.Duration

ConnectionMaxLifetimeJitter returns the connection max lifetime jitter

Returns:

  • time.Duration: the connection max lifetime jitter

func (*PoolConfig) DataSourceName

func (p *PoolConfig) DataSourceName() string

DataSourceName returns the data source name

Returns:

  • string: the data source name

func (*PoolConfig) HealthCheckPeriod

func (p *PoolConfig) HealthCheckPeriod() time.Duration

HealthCheckPeriod returns the health check period

Returns:

  • time.Duration: the health check period

func (*PoolConfig) MaxIdleConnections

func (p *PoolConfig) MaxIdleConnections() int

MaxIdleConnections returns the maximum idle connections

Returns:

  • int: the maximum idle connections

func (*PoolConfig) MaxOpenConnections

func (p *PoolConfig) MaxOpenConnections() int

MaxOpenConnections returns the maximum open connections

Returns:

  • int: the maximum open connections

func (*PoolConfig) ParsedConfig

func (p *PoolConfig) ParsedConfig() (*pgxpool.Config, error)

ParsedConfig returns the parsed configuration

Returns:

  • *pgxpool.Config: the parsed configuration

type PoolHandler

type PoolHandler interface {
	Connect() (*pgxpool.Pool, error)
	Pool() (*pgxpool.Pool, error)
	Disconnect()
}

PoolHandler interface

type Service

type Service interface {
	Pool() *pgxpool.Pool
	Migrate(queries ...string) error
	CreateTransaction(fn TransactionWithCtxFn) error
	CreateTransactionWithCtx(
		ctx context.Context,
		fn TransactionWithCtxFn,
	) error
	Exec(query *string, params ...interface{}) (*pgconn.CommandTag, error)
	ExecWithCtx(
		ctx context.Context,
		query *string,
		params ...interface{},
	) (*pgconn.CommandTag, error)
	Query(query *string, params ...interface{}) (pgx.Rows, error)
	QueryWithCtx(
		ctx context.Context,
		query *string,
		params ...interface{},
	) (pgx.Rows, error)
	QueryRow(query *string, params ...interface{}) (pgx.Row, error)
	QueryRowWithCtx(
		ctx context.Context,
		query *string,
		params ...interface{},
	) (pgx.Row, error)
	ScanRow(row pgx.Row, destinations ...interface{}) error
	SetStatTicker(
		duration time.Duration,
		fn func(*pgxpool.Stat),
	)
	ClearStatTicker()
}

Service is the interface for the service

type TransactionWithCtxFn added in v0.6.1

type TransactionWithCtxFn func(ctx context.Context, tx pgx.Tx) error

TransactionWithCtxFn is the function type for transactions with context

Jump to

Keyboard shortcuts

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