db

package
v1.1.5 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2024 License: GPL-3.0 Imports: 7 Imported by: 2

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 GetEntityColumns

func GetEntityColumns(entity interface{}) []interface{}

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 CommonDBConnector

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

CommonDBConnector is base connector to work with database.

func New

func New(dsn, driver string, logger *slog.Logger, opts ...PoolOption) (*CommonDBConnector, error)

New is constructor of CommonDBConnector. Gets database Config and *slog.Logger to create an instance.

func (*CommonDBConnector) Close added in v1.1.4

func (connector *CommonDBConnector) Close() error

Close closes pool of connections.

func (*CommonDBConnector) Connection

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

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

func (*CommonDBConnector) Pool added in v1.1.5

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

func (*CommonDBConnector) Transaction added in v1.1.4

func (connector *CommonDBConnector) 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
}

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

type PoolOption added in v1.1.4

type PoolOption func(options *poolOptions) error

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

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 settings.

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.

Jump to

Keyboard shortcuts

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