models

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2024 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DBLog = func(string, ...any) {}

DBLog provides the log func used by generated queries.

View Source
var DatabaseLatency = promauto.NewHistogramVec(
	prometheus.HistogramOpts{
		Name: "database_latency",
		Help: "Duration of database queries",
	},
	[]string{"query"},
)

DatabaseLatency is the duration of database queries.

View Source
var ErrConstraintViolation = errors.New("constraint violation")

ErrConstraintViolation is returned if a constraint is violated

View Source
var ErrDuplicate = errors.New("duplicate entry")

ErrDuplicate is returned if a duplicate entry is found

View Source
var ErrNoAffectedRows = errors.New("no affected rows")

ErrNoAffectedRows is returned if a model update affected no rows

View Source
var XOLog = func(msg string, args ...any) {
	DBLog(msg, args...)
}

XOLog is a compat shim for DBLog

Functions

func IsKeySet

func IsKeySet(x interface{}) bool

IsKeySet returns true if 1. x is an integer and greater than zero. 2. x not an integer and is not the zero value. Otherwise, returns false

Types

type DB

type DB interface {
	Exec(string, ...any) (sql.Result, error)
	Query(string, ...any) (*sql.Rows, error)
	QueryRow(string, ...any) *sql.Row
	// Additional sqlx methods we like
	Get(dest any, query string, args ...interface{}) error
	Select(dest any, query string, args ...interface{}) error
}

DB is the common interface for database operations

This should work with database/sql.DB and database/sql.Tx.

type DBTransactionHandler

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

DBTransactionHandler handles a transaction that will return any error.

func NewDBTransactionHandler

func NewDBTransactionHandler(db Transactioner) *DBTransactionHandler

NewDBTransactionHandler returns a configured instance of DBTransactionHandler

func (*DBTransactionHandler) Handle

Handle implements the TransactionHandler interface.

type DBTransactioner

type DBTransactioner interface {
	DB
	Transactioner
}

DBTransactioner is the interface that database connections that can utilise transactions should implement.

type Deletable

type Deletable interface {
	Delete(db DB) error
}

Deletable is the interface implemented by types which can delete themselves from the database.

type LoggableDBTransactionHandler

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

LoggableDBTransactionHandler handles a transaction and logs any error.

func NewLoggableDBTransactionHandler

func NewLoggableDBTransactionHandler(db Transactioner, l *slog.Logger) *LoggableDBTransactionHandler

NewLoggableDBTransactionHandler returns a configured instance of LoggableDBTransactionHandler

type PostDeletable

type PostDeletable interface {
	PostDelete() error
}

PostDeletable is the interface implemented by types which run a post delete step.

type PostSaveable

type PostSaveable interface {
	PostSave() error
}

PostSaveable is the interface implemented by types which run a post save step.

type PreDeletable

type PreDeletable interface {
	PreDelete() error
}

PreDeletable is the interface implemented by types which run a pre delete step.

type PreSaveable

type PreSaveable interface {
	PreSave(db DB) error
}

PreSaveable is the interface implemented by types which run a pre save step.

type Saveable

type Saveable interface {
	Save(db DB) error
}

Saveable is the interface implemented by types which can save themselves to the database.

type SetLogger

type SetLogger interface {
	SetLog(l *slog.Logger)
}

SetLogger is the interface implemented by types which have the ability to configure their log entry.

type TransactionFunc

type TransactionFunc func(db DB) error

TransactionFunc is a function to be called within a transaction.

type TransactionHandler

type TransactionHandler interface {
	Handle(TransactionFunc) error
}

type Transactioner

type Transactioner interface {
	Beginx() (*sqlx.Tx, error)
}

Transactioner is the interface that a database connection that can start a transaction should implement.

type XODB

type XODB = DB

XODB is a compat alias to DB

Jump to

Keyboard shortcuts

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