database

package
v0.0.0-...-18ada3e Latest Latest
Warning

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

Go to latest
Published: May 31, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

database is a package that contains the database interface. It is used to make it easier to use transactions. TX is a transaction interface and complies with the DB interface.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTransactorNotSet indicates that the transactor is not set.
	ErrTransactorNotSet = errors.New("transactor not set")
)

Functions

This section is empty.

Types

type Conn

type Conn interface {
	Query(ctx context.Context, query string, args ...interface{}) (*Rows, error)
	QueryRow(ctx context.Context, query string, args ...interface{}) *Row
	Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
}

Conn is a database connection or transaction. Use this interface as a dependency in your code.

type DB

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

DB is a database connection that uses a DBHandler.

func NewDB

func NewDB(sqlDB *sql.DB, opt ...Option) *DB

NewDB creates a new DB with the given sql.DB.

func NewDBWithHandler

func NewDBWithHandler(handler DBHandler, opt ...Option) *DB

NewDBWithHandler creates a new DB with a custom handler.

func (*DB) Begin

func (d *DB) Begin(ctx context.Context) (*Tx, error)

Begin starts a new transaction.

func (*DB) Exec

func (d *DB) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

Exec executes a query without returning any rows.

func (*DB) Query

func (d *DB) Query(ctx context.Context, query string, args ...interface{}) (*Rows, error)

Query executes a query that returns rows, typically a SELECT statement.

func (*DB) QueryRow

func (d *DB) QueryRow(ctx context.Context, query string, args ...interface{}) *Row

QueryRow executes a query that is expected to return at most one row.

type DBHandler

type DBHandler interface {
	QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
}

DBHandler is a database interface and should comply with *sql.DB.

type Factory

type Factory[T any] func(c Conn) T

Factory is a function that initializes a repository with a database connection.

type Manager

type Manager[T any] struct {
	// contains filtered or unexported fields
}

Manager manages database connections and transactions for a specific type of repository.

func NewManager

func NewManager[T any](db *DB, register Factory[T]) *Manager[T]

NewManager creates a new Manager instance, initializing it with a database connection, a transaction manager, and a slice of repository registration functions.

func (*Manager[T]) BeginOp

func (m *Manager[T]) BeginOp(ctx context.Context, operation func(ctx context.Context, r T) error) error

BeginOp starts a new operation with a transaction and commits the transaction if all operations succeed; it rolls back the transaction otherwise.

func (*Manager[T]) Op

func (m *Manager[T]) Op() T

Op initializes a new operation with the database connection.

func (*Manager[T]) SetFailingRollbackHandler

func (m *Manager[T]) SetFailingRollbackHandler(handler func(ctx context.Context, err error))

SetFailingRollbackHandler sets the handler function that is called when a transaction fails to roll back.

type Option

type Option func(*DB)

type Row

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

func NewRow

func NewRow(handler RowHandler) *Row

func (*Row) Err

func (r *Row) Err() error

Err returns the error, if any, that was encountered during iteration.

func (*Row) Scan

func (r *Row) Scan(dest ...interface{}) error

Scan copies the columns in the current row into the values pointed at by dest.

type RowHandler

type RowHandler interface {
	Err() error
	Scan(dest ...interface{}) error
}

RowHandler is a database row and should comply with *sql.Row.

type Rows

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

func NewRows

func NewRows(handler RowsHandler) *Rows

func (*Rows) Close

func (r *Rows) Close() error

Close closes the Rows, preventing further enumeration.

func (*Rows) Err

func (r *Rows) Err() error

Err returns the error, if any, that was encountered during iteration.

func (*Rows) Next

func (r *Rows) Next() bool

Next prepares the next result row for reading.

func (*Rows) Scan

func (r *Rows) Scan(dest ...interface{}) error

Scan copies the columns in the current row into the values pointed at by dest.

type RowsHandler

type RowsHandler interface {
	Close() error
	Err() error
	Next() bool
	Scan(dest ...interface{}) error
}

type SQLDBWrapper

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

SQLDBWrapper wraps a sql.DB to implement DBHandler.

func NewSQLDBWrapper

func NewSQLDBWrapper(db *sql.DB) *SQLDBWrapper

NewSQLDBWrapper creates a new SQLDBWrapper.

func (*SQLDBWrapper) BeginTx

func (s *SQLDBWrapper) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)

BeginTx implements DBHandler.

func (*SQLDBWrapper) ExecContext

func (s *SQLDBWrapper) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

ExecContext implements DBHandler.

func (*SQLDBWrapper) QueryContext

func (s *SQLDBWrapper) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)

QueryContext implements DBHandler.

func (*SQLDBWrapper) QueryRowContext

func (s *SQLDBWrapper) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row

QueryRowContext implements DBHandler.

type SQLTxWrapper

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

SQLTxWrapper wraps a sql.Tx to implement TXHandler.

func NewSQLTxWrapper

func NewSQLTxWrapper(tx *sql.Tx) *SQLTxWrapper

NewSQLTxWrapper creates a new SQLTxWrapper.

func (*SQLTxWrapper) Commit

func (s *SQLTxWrapper) Commit() error

Commit implements TXHandler.

func (*SQLTxWrapper) ExecContext

func (s *SQLTxWrapper) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

ExecContext implements TXHandler.

func (*SQLTxWrapper) QueryContext

func (s *SQLTxWrapper) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)

QueryContext implements TXHandler.

func (*SQLTxWrapper) QueryRowContext

func (s *SQLTxWrapper) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row

QueryRowContext implements TXHandler.

func (*SQLTxWrapper) Rollback

func (s *SQLTxWrapper) Rollback() error

Rollback implements TXHandler.

type TXHandler

type TXHandler interface {
	QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	Commit() error
	Rollback() error
}

TXHandler is a database transaction and should comply with *sql.Tx.

type Tx

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

Tx is a transaction that uses a TXHandler.

func NewTx

func NewTx(sqlTx *sql.Tx) *Tx

NewTx creates a new Tx with the given sql.Tx.

func NewTxWithHandler

func NewTxWithHandler(handler TXHandler) *Tx

NewTxWithHandler creates a new Tx with a custom handler.

func (*Tx) Commit

func (t *Tx) Commit() error

Commit commits the transaction.

func (*Tx) Exec

func (t *Tx) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

Exec executes a query without returning any rows.

func (*Tx) Query

func (t *Tx) Query(ctx context.Context, query string, args ...interface{}) (*Rows, error)

Query executes a query that returns rows, typically a SELECT statement.

func (*Tx) QueryRow

func (t *Tx) QueryRow(ctx context.Context, query string, args ...interface{}) *Row

QueryRow executes a query that is expected to return at most one row.

func (*Tx) Rollback

func (t *Tx) Rollback() error

Rollback rolls back the transaction.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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