database

package
v0.0.0-...-cb8f91d Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2020 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const ContextTransactionKey = "db.transaction"

Variables

This section is empty.

Functions

This section is empty.

Types

type Database

type Database struct {
	*sqlx.DB
	// contains filtered or unexported fields
}

func NewDatabase

func NewDatabase(c DatabaseConfig) (*Database, error)

func (*Database) GetReadReplicaDb

func (d *Database) GetReadReplicaDb() *sqlx.DB

func (*Database) TxOrDbFromContext

func (db *Database) TxOrDbFromContext(ctx context.Context) TxOrDb

абстракция для tx или db, из контекста если в контексте есть транзакция, то возвращает ее (как интерфейс TxOrDb) если транзакции нет, то возвращает db (как интерфейс TxOrDb)

func (*Database) WithTransaction

func (db *Database) WithTransaction(ctx context.Context, fn func(ctx context.Context) error) (err error)

type DatabaseConfig

type DatabaseConfig struct {
	Server       string   `envconfig:"server"`
	ReadReplicas []string `envconfig:"readreplicas"`
	User         string   `envconfig:"user"`
	Password     string   `envconfig:"password"`
	Port         int      `envconfig:"port"`
	DatabaseName string   `envconfig:"database"`
	MaxOpenConns int      `envconfig:"maxopenconns"`
	MaxIdleConns int      `envconfig:"maxidleconns"`
}

type TxOrDb

type TxOrDb interface {
	sqlx.Execer
	sqlx.ExecerContext
	sqlx.Queryer
	sqlx.QueryerContext
	sqlx.Preparer
	sqlx.PreparerContext
	BindNamed(query string, arg interface{}) (string, []interface{}, error)
	DriverName() string
	Get(dest interface{}, query string, args ...interface{}) error
	MustExec(query string, args ...interface{}) sql.Result
	NamedExec(query string, arg interface{}) (sql.Result, error)
	NamedQuery(query string, arg interface{}) (*sqlx.Rows, error)
	PrepareNamed(query string) (*sqlx.NamedStmt, error)
	Preparex(query string) (*sqlx.Stmt, error)
	Rebind(query string) string
	Select(dest interface{}, query string, args ...interface{}) error
	GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
	SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
}

Jump to

Keyboard shortcuts

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