pgxpool

package
v0.8.2 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateTransaction

func CreateTransaction(
	ctx context.Context,
	pool *pgxpool.Pool,
	fn TransactionFn,
) error

CreateTransaction 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 struct {
	DataSourceName              string
	MaxOpenConnections          int
	MaxIdleConnections          int
	ConnectionMaxLifetime       time.Duration
	ConnectionMaxIdleTime       time.Duration
	HealthCheckPeriod           time.Duration
	ConnectionMaxLifetimeJitter time.Duration
}

Config struct

func NewConfig added in v0.7.1

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

NewConfig 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:

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

func (*Config) ParsedConfig

func (c *Config) ParsedConfig() (*pgxpool.Config, error)

ParsedConfig returns the parsed configuration

Returns:

  • *pgxpool.Config: the parsed configuration

type DefaultHandler added in v0.7.1

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

DefaultHandler struct

func NewDefaultHandler added in v0.7.1

func NewDefaultHandler(
	config *Config,
) (*DefaultHandler, error)

NewDefaultHandler creates a new connection

Parameters:

  • config *Config: configuration for the connection

Returns:

  • *DefaultHandler: connection handler

func (*DefaultHandler) Connect added in v0.7.1

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

Connect returns a new connection pool

func (*DefaultHandler) Disconnect added in v0.7.1

func (d *DefaultHandler) Disconnect()

Disconnect closes the connection pool

func (*DefaultHandler) IsConnected added in v0.7.5

func (d *DefaultHandler) IsConnected() bool

IsConnected checks if the connection is established

Returns:

  • bool: true if the connection is established, false otherwise

func (*DefaultHandler) Pool added in v0.7.1

func (d *DefaultHandler) 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 {
	Handler
	// contains filtered or unexported fields
}

DefaultService is the default service struct

func NewDefaultService

func NewDefaultService(config *Config, logger *slog.Logger) (
	instance *DefaultService,
	err error,
)

NewDefaultService creates a new default service

Parameters:

  • config *Config: configuration for the connection
  • logger *slog.Logger: the logger 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(
	ctx context.Context,
	fn TransactionFn,
) error

CreateTransaction 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 ...any) (
	*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 ...any,
) (
	*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) Query added in v0.5.4

func (d *DefaultService) Query(
	query *string,
	params ...any,
) (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 ...any,
) (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 ...any,
) (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 ...any,
) (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 ...any,
) 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(
	ctx context.Context,
	duration time.Duration,
	fn func(*pgxpool.Stat),
)

SetStatTicker sets a stat ticker

Parameters:

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

type Handler added in v0.7.1

type Handler interface {
	Connect() (*pgxpool.Pool, error)
	IsConnected() bool
	Pool() (*pgxpool.Pool, error)
	Disconnect()
}

Handler interface

type Service

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

Service is the interface for the service

type TransactionFn added in v0.7.5

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

TransactionFn 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