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 ¶
- Variables
- type Conn
- type DB
- func (d *DB) Begin(ctx context.Context) (*Tx, error)
- func (d *DB) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (d *DB) Query(ctx context.Context, query string, args ...interface{}) (*Rows, error)
- func (d *DB) QueryRow(ctx context.Context, query string, args ...interface{}) *Row
- type DBHandler
- type Factory
- type Manager
- type Option
- type Row
- type RowHandler
- type Rows
- type RowsHandler
- type SQLDBWrapper
- func (s *SQLDBWrapper) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
- func (s *SQLDBWrapper) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (s *SQLDBWrapper) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)
- func (s *SQLDBWrapper) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row
- type SQLTxWrapper
- func (s *SQLTxWrapper) Commit() error
- func (s *SQLTxWrapper) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (s *SQLTxWrapper) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)
- func (s *SQLTxWrapper) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row
- func (s *SQLTxWrapper) Rollback() error
- type TXHandler
- type Tx
- func (t *Tx) Commit() error
- func (t *Tx) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (t *Tx) Query(ctx context.Context, query string, args ...interface{}) (*Rows, error)
- func (t *Tx) QueryRow(ctx context.Context, query string, args ...interface{}) *Row
- func (t *Tx) Rollback() error
Constants ¶
This section is empty.
Variables ¶
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 NewDBWithHandler ¶
NewDBWithHandler creates a new DB with a custom handler.
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 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 ¶
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.
type Row ¶
type Row struct {
// contains filtered or unexported fields
}
func NewRow ¶
func NewRow(handler RowHandler) *Row
type RowHandler ¶
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
type RowsHandler ¶
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) 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) 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 NewTxWithHandler ¶
NewTxWithHandler creates a new Tx with a custom handler.