db

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2025 License: GPL-3.0 Imports: 7 Imported by: 4

Documentation

Overview

Package db provides tools for easy and quick access to database via Connector.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildDsn added in v1.0.4

func BuildDsn(config Config) string

func CloseConnectionContext added in v1.1.8

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 db/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 added in v1.3.0

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 added in v1.3.0

func (connector *CommonConnector) Close() error

Close closes pool of connections.

func (*CommonConnector) Connection added in v1.3.0

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 added in v1.3.0

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

Pool returns database connections pool.

func (*CommonConnector) Transaction added in v1.3.0

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 added in v1.2.11

func (e NilDBConnectionError) Unwrap() error

type PoolConfig added in v1.1.9

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

type PoolOption added in v1.1.4

type PoolOption func(options *poolOptions) error

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

func WithMaxConnectionIdleTime added in v1.1.4

func WithMaxConnectionIdleTime(idleTime time.Duration) PoolOption

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

func WithMaxConnectionLifetime added in v1.1.4

func WithMaxConnectionLifetime(lifetime time.Duration) PoolOption

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

func WithMaxIdleConnections added in v1.1.4

func WithMaxIdleConnections(num int) PoolOption

WithMaxIdleConnections sets maximum idle connections in database connections pool.

func WithMaxOpenConnections added in v1.1.4

func WithMaxOpenConnections(num int) PoolOption

WithMaxOpenConnections sets maximum opened connections in database connections pool.

type TransactionOption added in v1.1.4

type TransactionOption func(options *transactionOptions) error

TransactionOption represents golang functional option pattern func for transaction configuration.

func WithTransactionIsolationLevel added in v1.1.4

func WithTransactionIsolationLevel(isolationLevel sql.IsolationLevel) TransactionOption

WithTransactionIsolationLevel sets transaction isolation level for database transaction.

func WithTransactionReadOnly added in v1.1.4

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