Documentation
¶
Overview ¶
Package postgresql provides tools for easy and quick access to PostgreSQL via Connector.
Index ¶
- func BuildDsn(config Config) string
- func CloseConnectionContext(ctx context.Context, connection *sql.Conn, logger logging.Logger)
- func GetEntityColumns(entity any) []any
- type CommonConnector
- type Config
- type Connection
- type Connector
- type NilDBConnectionError
- type Pool
- type PoolConfig
- type PoolOption
- type Transaction
- type TransactionOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CloseConnectionContext ¶
func GetEntityColumns ¶
GetEntityColumns receives a POINTER on entity (NOT A VALUE), parses is using reflection and returns a slice of columns for postgresql/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 ¶
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 ¶
func (connector *CommonConnector) Close() error
Close closes pool of connections.
func (*CommonConnector) Connection ¶
func (connector *CommonConnector) Connection(ctx context.Context) (Connection, error)
Connection creates connection with database, if not exists. Returns connection for external usage.
func (*CommonConnector) Pool ¶
func (connector *CommonConnector) Pool() Pool
Pool returns database connections pool.
func (*CommonConnector) Transaction ¶
func (connector *CommonConnector) Transaction( ctx context.Context, opts ...TransactionOption, ) (Transaction, 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 Connection ¶ added in v1.13.2
type Connection interface {
PingContext(ctx context.Context) error
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
Raw(f func(driverConn any) error) (err error)
BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
Close() error
}
Connection represents abstraction of Database to execute any operation with Database
type Connector ¶
type Connector interface {
Close() error
Transaction(ctx context.Context, opts ...TransactionOption) (Transaction, error)
Connection(ctx context.Context) (Connection, error)
Pool() Pool
}
Connector represents abstraction to work with Database according dependency inversion principal relying on methods.
type NilDBConnectionError ¶
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 ¶
func (e NilDBConnectionError) Unwrap() error
type Pool ¶ added in v1.13.2
type Pool interface {
PingContext(ctx context.Context) error
Ping() error
Close() error
SetMaxIdleConns(n int)
SetMaxOpenConns(n int)
SetConnMaxLifetime(d time.Duration)
SetConnMaxIdleTime(d time.Duration)
Stats() sql.DBStats
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
Prepare(query string) (*sql.Stmt, error)
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
Exec(query string, args ...any) (sql.Result, error)
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
Query(query string, args ...any) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
QueryRow(query string, args ...any) *sql.Row
BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
Begin() (*sql.Tx, error)
Driver() driver.Driver
Conn(ctx context.Context) (*sql.Conn, error)
}
Pool represents abstraction of Database to work with connections and transactions
type PoolConfig ¶
type PoolOption ¶
type PoolOption func(options *poolOptions) error
PoolOption represents golang functional option pattern func for connections pool configuration.
func WithMaxConnectionIdleTime ¶
func WithMaxConnectionIdleTime(idleTime time.Duration) PoolOption
WithMaxConnectionIdleTime sets maximum connection idle time (without usage) before closure in database connections pool.
func WithMaxConnectionLifetime ¶
func WithMaxConnectionLifetime(lifetime time.Duration) PoolOption
WithMaxConnectionLifetime sets maximum connection lifetime before closure in database connections pool.
func WithMaxIdleConnections ¶
func WithMaxIdleConnections(num int) PoolOption
WithMaxIdleConnections sets maximum idle connections in database connections pool.
func WithMaxOpenConnections ¶
func WithMaxOpenConnections(num int) PoolOption
WithMaxOpenConnections sets maximum opened connections in database connections pool.
type Transaction ¶ added in v1.13.2
type Transaction interface {
Commit() error
Rollback() error
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
Prepare(query string) (*sql.Stmt, error)
StmtContext(ctx context.Context, stmt *sql.Stmt) *sql.Stmt
Stmt(stmt *sql.Stmt) *sql.Stmt
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
Exec(query string, args ...any) (sql.Result, error)
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
Query(query string, args ...any) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
QueryRow(query string, args ...any) *sql.Row
}
Transaction represents abstraction of Database to comply Atomicity principle according dependency inversion principal relying on methods.
type TransactionOption ¶
type TransactionOption func(options *transactionOptions) error
TransactionOption represents golang functional option pattern func for transaction configuration.
func WithTransactionIsolationLevel ¶
func WithTransactionIsolationLevel(isolationLevel sql.IsolationLevel) TransactionOption
WithTransactionIsolationLevel sets transaction isolation level for database transaction.
func WithTransactionReadOnly ¶
func WithTransactionReadOnly(readOnly bool) TransactionOption
WithTransactionReadOnly sets readOnly attribute for database transaction.