db

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2024 License: GPL-3.0 Imports: 6 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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(dbConfig Config, logger *slog.Logger) (*CommonDBConnector, error)

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

func NewTestConnector added in v1.0.3

func NewTestConnector(config TestConfig, logger *slog.Logger) (*CommonDBConnector, error)

func (*CommonDBConnector) CloseConnection

func (connector *CommonDBConnector) CloseConnection()

CloseConnection closes stored connection to database.

func (*CommonDBConnector) Connect

func (connector *CommonDBConnector) Connect() error

Connect connects to database and stores database connection for later usage.

func (*CommonDBConnector) GetConnection

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

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

func (*CommonDBConnector) GetTransaction

func (connector *CommonDBConnector) GetTransaction() (*sql.Tx, error)

GetTransaction 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 {
	Connect() error
	CloseConnection()
	GetTransaction() (*sql.Tx, error)
	GetConnection() *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
}

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

func (NilDBConnectionError) Error

func (e NilDBConnectionError) Error() string

type TestConfig added in v1.0.1

type TestConfig struct {
	Driver        string
	DSN           string
	MigrationsDir string
}

func NewTestConfig added in v1.0.1

func NewTestConfig() TestConfig

Jump to

Keyboard shortcuts

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