postgres

package module
v2.2.7 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2024 License: MIT Imports: 13 Imported by: 2

README

postgres

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Mapper

func Mapper(row *sql.Rows, model reflect.Type) (obj interface{}, err error)

Mapper - map db row to struct

Types

type CreateConnection

type CreateConnection func() (*sql.DB, error)

CreateConnection - Function for create connection

type DBContextFunction

type DBContextFunction func(*sql.DB) error

DBContextFunction - Callback function for receiving db connection

type DBContextWithTransactionFunction

type DBContextWithTransactionFunction func(*sql.Tx) error

DBContextWithTransactionFunction - Callback function for receiving db transaction

type DBFieldMapping

type DBFieldMapping func(*sql.Rows) (interface{}, error)

DBFieldMapping - Callback function for apply field mapping

type Database

type Database struct {
	DbContext
	Host     string  `yaml:"host"`
	Port     int     `yaml:"port"`
	User     string  `yaml:"user"`
	Password string  `yaml:"password"`
	Db       string  `yaml:"db"`
	SslMode  *string `yaml:"sslMode"`
	PoolSize *int    `yaml:"poolSize"`
}

func (*Database) HealthCheck

func (cfg *Database) HealthCheck() (bool, error)

func (*Database) HealthCode

func (cfg *Database) HealthCode() string

func (*Database) HealthName

func (cfg *Database) HealthName() string

func (*Database) Init

func (cfg *Database) Init() error

func (*Database) InitContext added in v2.1.0

func (cfg *Database) InitContext(ctx context.Context) context.Context

type DbContext

type DbContext struct {
	IDbContext
	// contains filtered or unexported fields
}

DbContext - db context

func (*DbContext) ExecuteQuery

func (context *DbContext) ExecuteQuery(query string, params ...interface{}) error

ExecuteQuery - execute query

func (*DbContext) First

func (context *DbContext) First(rows []interface{}) interface{}

First - return firs item from list

func (*DbContext) GetConnection

func (context *DbContext) GetConnection() (*sql.DB, error)

GetConnection - get connection for DB

func (*DbContext) GetListener

func (context *DbContext) GetListener(channel string) (*Listener, error)

GetListener - get listener

func (*DbContext) GetRows

func (context *DbContext) GetRows(query string, mapping DBFieldMapping, params ...interface{}) (data []interface{}, err error)

GetRows - execute query what return list rows

func (*DbContext) GetRowsModel

func (context *DbContext) GetRowsModel(query string, model reflect.Type, params ...interface{}) (data []interface{}, err error)

GetRowsModel - execute query what return list rows with model

func (*DbContext) GetSingleRow

func (context *DbContext) GetSingleRow(query string, mapping DBFieldMapping, params ...interface{}) (data interface{}, err error)

GetSingleRow - execute query what return only one row

func (*DbContext) GetSingleRowModel

func (context *DbContext) GetSingleRowModel(query string, model reflect.Type, params ...interface{}) (data interface{}, err error)

GetSingleRowModel - execute query what return only one row with model

func (*DbContext) Mapper

func (context *DbContext) Mapper(model reflect.Type) DBFieldMapping

Mapper - define rule for mapping sql row to model

func (*DbContext) Mapping

func (context *DbContext) Mapping(rows *sql.Rows, mapping DBFieldMapping) (data []interface{}, err error)

Mapping - mapping sql row to model collection

func (*DbContext) Setup

func (context *DbContext) Setup(host string, port int, user, password, db, sslMode string, poolSize *int,
	timeLife *time.Duration)

Setup - setup Db context

func (*DbContext) Using

func (context *DbContext) Using(query DBContextFunction) error

Using - helper for execute query with connection

func (*DbContext) UsingWithTransaction

func (context *DbContext) UsingWithTransaction(query DBContextWithTransactionFunction) error

UsingWithTransaction - helper for execute query with transaction

type IDbContext

type IDbContext interface {
	// Setup - setup Db context
	Setup(host string, port int, user, password, db, sslMode string, poolSize *int,
		timeLife *time.Duration)
	// GetConnection - Get connection for DB
	GetConnection() (*sql.DB, error)
	// Using - helper for execute query with connection
	Using(query DBContextFunction) error
	// UsingWithTransaction - helper for execute query with transaction
	UsingWithTransaction(query DBContextWithTransactionFunction) error
	// GetRows - execute query what return list rows
	GetRows(query string, mapping DBFieldMapping, params ...interface{}) (data []interface{}, err error)
	// GetRowsModel - execute query what return list rows with model
	GetRowsModel(query string, model reflect.Type, params ...interface{}) (data []interface{}, err error)
	// GetSingleRow - execute query what return only one row
	GetSingleRow(query string, mapping DBFieldMapping, params ...interface{}) (data interface{}, err error)
	// GetSingleRowModel: execute query what return only one row with model
	GetSingleRowModel(query string, model reflect.Type, params ...interface{}) (data interface{}, err error)
	// ExecuteQuery - execute query
	ExecuteQuery(query string, params ...interface{}) error
}

IDbContext - DB context common interface

type Listener

type Listener struct {
	Data chan string
	// contains filtered or unexported fields
}

Listener - class for work with Postgres listener

func (*Listener) Close

func (listener *Listener) Close()

Close - close listener

type Pool

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

Pool - pool of db connection

func (*Pool) Get

func (pool *Pool) Get() (*sql.DB, error)

Get - get connection

func (*Pool) Init

func (pool *Pool) Init(size *int, timeLife *time.Duration, create CreateConnection)

Init - init pool

func (*Pool) Release

func (pool *Pool) Release(db *sql.DB) error

Release - close connection or put it back to pool

type PoolConnection

type PoolConnection struct {
	CreateTime time.Time // time of creation
	// contains filtered or unexported fields
}

PoolConnection - connection in pool

type QueryExecutor

type QueryExecutor interface {
	Query(query string, args ...interface{}) (*sql.Rows, error)
	Exec(query string, args ...interface{}) (sql.Result, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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