postgresql

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2025 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package postgresql provides tools for easy and quick access to PostgreSQL via Connector.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildDsn

func BuildDsn(config Config) string

func CloseConnectionContext

func CloseConnectionContext(ctx context.Context, connection *sql.Conn, logger logging.Logger)

func GetEntityColumns

func GetEntityColumns(entity any) []any

GetEntityColumns receives a POINTER on entity (NOT A VALUE), parses is using reflection and returns a slice of columns for postgresql/sql Query() method purpose for retrieving data from result rows. https://stackoverflow.com/questions/56525471/how-to-use-rows-scan-of-gos-database-sql

Types

type CommonConnector

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

CommonConnector is base connector to work with database.

func New

func New(dsn, driver string, logger logging.Logger, opts ...PoolOption) (*CommonConnector, error)

New is constructor of CommonConnector. Gets database Config and logging.Logger to create an instance.

func (*CommonConnector) Close

func (connector *CommonConnector) Close() error

Close closes pool of connections.

func (*CommonConnector) Connection

func (connector *CommonConnector) Connection(ctx context.Context) (*sql.Conn, error)

Connection creates connection with database, if not exists. Returns connection for external usage.

func (*CommonConnector) Pool

func (connector *CommonConnector) Pool() *sql.DB

Pool returns database connections pool.

func (*CommonConnector) Transaction

func (connector *CommonConnector) Transaction(
	ctx context.Context,
	opts ...TransactionOption,
) (*sql.Tx, error)

Transaction return database transaction object for external usage with atomicity of operations.

type Config

type Config struct {
	Host         string
	Port         int
	User         string
	Password     string
	DatabaseName string
	SSLMode      string
	Driver       string
	Pool         PoolConfig
}

Config is a database config, on base of which new Connector is created.

type Connector

type Connector interface {
	Close() error
	Transaction(ctx context.Context, opts ...TransactionOption) (*sql.Tx, error)
	Connection(ctx context.Context) (*sql.Conn, error)
	Pool() *sql.DB
}

Connector interface is created for usage in external application according to "dependency inversion principle" of SOLID due to working via abstractions.

type NilDBConnectionError

type NilDBConnectionError struct {
	Message string
	BaseErr error
}

NilDBConnectionError is an error, representing not being able to connect to database and create a connection pool.

func (NilDBConnectionError) Error

func (e NilDBConnectionError) Error() string

func (NilDBConnectionError) Unwrap

func (e NilDBConnectionError) Unwrap() error

type PoolConfig

type PoolConfig struct {
	MaxOpenConnections    int
	MaxIdleConnections    int
	MaxConnectionLifetime time.Duration
	MaxConnectionIdleTime time.Duration
}

type PoolOption

type PoolOption func(options *poolOptions) error

PoolOption represents golang functional option pattern func for connections pool configuration.

func WithMaxConnectionIdleTime

func WithMaxConnectionIdleTime(idleTime time.Duration) PoolOption

WithMaxConnectionIdleTime sets maximum connection idle time (without usage) before closure in database connections pool.

func WithMaxConnectionLifetime

func WithMaxConnectionLifetime(lifetime time.Duration) PoolOption

WithMaxConnectionLifetime sets maximum connection lifetime before closure in database connections pool.

func WithMaxIdleConnections

func WithMaxIdleConnections(num int) PoolOption

WithMaxIdleConnections sets maximum idle connections in database connections pool.

func WithMaxOpenConnections

func WithMaxOpenConnections(num int) PoolOption

WithMaxOpenConnections sets maximum opened connections in database connections pool.

type TransactionOption

type TransactionOption func(options *transactionOptions) error

TransactionOption represents golang functional option pattern func for transaction configuration.

func WithTransactionIsolationLevel

func WithTransactionIsolationLevel(isolationLevel sql.IsolationLevel) TransactionOption

WithTransactionIsolationLevel sets transaction isolation level for database transaction.

func WithTransactionReadOnly

func WithTransactionReadOnly(readOnly bool) TransactionOption

WithTransactionReadOnly sets readOnly attribute for database transaction.

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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