db

package
v0.0.0-...-9d47661 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package db provides support for access the database.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDBNotFound        = sql.ErrNoRows
	ErrDBDuplicatedEntry = errors.New("duplicated entry")
	ErrUndefinedTable    = errors.New("undefined table")
)

Set of error variables for CRUD operations.

Functions

func ExecContext

func ExecContext(ctx context.Context, log *zap.SugaredLogger, db sqlx.ExtContext, query string) error

ExecContext is a helper function to execute a CUD operation with logging and tracing.

func NamedExecContext

func NamedExecContext(ctx context.Context, log *zap.SugaredLogger, db sqlx.ExtContext, query string, data any) error

NamedExecContext is a helper function to execute a CUD operation with logging and tracing where field replacement is necessary.

func NamedQuerySlice

func NamedQuerySlice[T any](ctx context.Context, log *zap.SugaredLogger, db sqlx.ExtContext, query string, data any, dest *[]T) error

NamedQuerySlice is a helper function for executing queries that return a collection of data to be unmarshalled into a slice where field replacement is necessary.

func NamedQuerySliceUsingIn

func NamedQuerySliceUsingIn[T any](ctx context.Context, log *zap.SugaredLogger, db sqlx.ExtContext, query string, data any, dest *[]T) error

NamedQuerySliceUsingIn is a helper function for executing queries that return a collection of data to be unmarshalled into a slice where field replacement is necessary. Use this if the query has an IN clause.

func NamedQueryStruct

func NamedQueryStruct(ctx context.Context, log *zap.SugaredLogger, db sqlx.ExtContext, query string, data any, dest any) error

NamedQueryStruct is a helper function for executing queries that return a single value to be unmarshalled into a struct type where field replacement is necessary.

func NamedQueryStructUsingIn

func NamedQueryStructUsingIn(ctx context.Context, log *zap.SugaredLogger, db sqlx.ExtContext, query string, data any, dest any) error

NamedQueryStructUsingIn is a helper function for executing queries that return a single value to be unmarshalled into a struct type where field replacement is necessary. Use this if the query has an IN clause.

func Open

func Open(cfg Config) (*sqlx.DB, error)

Open knows how to open a database connection based on the configuration.

func QuerySlice

func QuerySlice[T any](ctx context.Context, log *zap.SugaredLogger, db sqlx.ExtContext, query string, dest *[]T) error

QuerySlice is a helper function for executing queries that return a collection of data to be unmarshalled into a slice.

func QueryStruct

func QueryStruct(ctx context.Context, log *zap.SugaredLogger, db sqlx.ExtContext, query string, dest any) error

QueryStruct is a helper function for executing queries that return a single value to be unmarshalled into a struct type where field replacement is necessary.

func StatusCheck

func StatusCheck(ctx context.Context, db *sqlx.DB) error

StatusCheck returns nil if it can successfully talk to the database. It returns a non-nil error otherwise.

func WithinTran

func WithinTran(ctx context.Context, log *zap.SugaredLogger, db *sqlx.DB, fn func(*sqlx.Tx) error) error

WithinTran runs passed functin and do commit/rollback at the end.

Types

type Config

type Config struct {
	User         string
	Password     string
	Host         string
	Name         string
	Schema       string
	MaxIdleConns int
	MaxOpenConns int
	DisableTLS   bool
}

Config is the required properties to use the database.

Directories

Path Synopsis
Package dbarray provides support for database array types.
Package dbarray provides support for database array types.

Jump to

Keyboard shortcuts

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