db

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2024 License: GPL-3.0 Imports: 6 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) (*CommonDBConnector, error)

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

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
	BaseErr error
}

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

Jump to

Keyboard shortcuts

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