db

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

README

db

Contains the Postgres class to handle Postgres connection and some utility functions for SQL specific nullable types.

Install

go get github.com/blutspende/libs-db

Postgres

Postgres is a class for handling Postgres connections. It provides methods for connecting, disconnecting, and obtaining the underlying raw SQL connection *sqlx.DB. NewPostgres is used to create a new instance, which requires a PgConfig as input:

type PgConfig struct {
    ApplicationName              string
    Host                         string
    Port                         uint32
    User                         string
    Pass                         string
    Database                     string
    SSLMode                      string
    MaxOpenConnections           *int
    MaxIdleConnections           *int
    ConnectionMaxLifetimeSeconds *int
    ConnectionMaxIdleTimeSeconds *int
}

The *int types can be set to nil to avoid setting those configurations on the database connection.

DbConnection

DbConnection is a class for transaction and query handling. It allows direct execution of queries, as well as transaction management with BeginTx, Commit and Rollback methods. Specific error codes and code conversions are also provided.

Utility functions

func NullStringToString(value sql.NullString) string
func NullStringToStringPointer(value sql.NullString) *string
func NullTimeToTimePointer(value sql.NullTime) *time.Time
func TimePointerToNullTime(value *time.Time) sql.NullTime

Documentation

Index

Constants

View Source
const (
	DuplicateKeyErrorCode        = pq.ErrorCode("23505")
	ForeignKeyViolationErrorCode = pq.ErrorCode("23503")
)
View Source
const MaxQueryParams = 65534

Variables

View Source
var (
	ErrBeginTransactionFailed     = errors.New("begin transaction failed")
	ErrCommitTransactionFailed    = errors.New("commit transaction failed")
	ErrRollbackTransactionFailed  = errors.New("revert transaction failed")
	ErrCommitWithoutTransaction   = errors.New("invalid transaction, can not perform commit without transaction")
	ErrRollbackWithoutTransaction = errors.New("invalid transaction, can not perform rollback without transaction")
	ErrNoPgConnection             = errors.New("postgres connection is not established")
)

Functions

func IsErrorCode

func IsErrorCode(err error, errCode pq.ErrorCode) bool

func NullStringToString

func NullStringToString(value sql.NullString) string

func NullStringToStringPointer

func NullStringToStringPointer(value sql.NullString) *string

func NullTimeToTimePointer

func NullTimeToTimePointer(value sql.NullTime) *time.Time

func TimePointerToNullTime

func TimePointerToNullTime(value *time.Time) sql.NullTime

func TryCastErrorToPgError

func TryCastErrorToPgError(err error) any

Types

type DbConnection

type DbConnection interface {
	SetSqlConnection(db *sqlx.DB)
	EnableQueryLogging()
	Ping() error
	BeginTx(ctx context.Context) (DbConnection, error)
	Commit() error
	Rollback() error
	Rebind(query string) string
	PrepareNamed(query string) (*sqlx.NamedStmt, error)
	Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	Get(ctx context.Context, dest interface{}, query string, args ...interface{}) error
	NamedExec(ctx context.Context, query string, arg interface{}) (sql.Result, error)
	NamedQuery(ctx context.Context, query string, arg interface{}) (*sqlx.Rows, error)
	Queryx(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)
	QueryRowx(ctx context.Context, query string, args ...interface{}) *sqlx.Row
}

func NewDbConnection

func NewDbConnection(db *sqlx.DB) DbConnection

func NewEmptyDbConnection

func NewEmptyDbConnection() DbConnection

func NewFakeDbConnection

func NewFakeDbConnection() DbConnection

NewFakeDbConnection creates a new instance of a fake database connection that implements the DbConnection interface. All methods return nil value for sql.Result, *sqlx.Row, *sqlx.Rows, and nil error. Intended to be used in repository mocks in tests with service-layer transaction logic.

type PgConfig

type PgConfig struct {
	ApplicationName              string
	Host                         string
	Port                         uint32
	User                         string
	Pass                         string
	Database                     string
	SSLMode                      string
	MaxOpenConnections           *int
	MaxIdleConnections           *int
	ConnectionMaxLifetimeSeconds *int
	ConnectionMaxIdleTimeSeconds *int
	UseOpenTelemetry             bool
}

type Postgres

type Postgres interface {
	Connect(ctx context.Context) (*sqlx.DB, error)
	GetSqlConnection() (*sqlx.DB, error)
	Close() error
}

func NewPostgres

func NewPostgres(config PgConfig) Postgres

Jump to

Keyboard shortcuts

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