database

package
v0.0.0-...-dc37515 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2021 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ensure

func Ensure(logger logrus.FieldLogger, conn DB, v DatabaseEntity) error

Ensure makes sure that a given DatabaseEntity has its schema in the database.

func MaybeRollback

func MaybeRollback(r *http.Request) error

MaybeRollback tries to roll back a transaction.

If the current connection is not a transaction, or if the transaction is already done then nothing will happen.

Types

type DB

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

DB represents a database connection.

func Connect

func Connect(dbUrl string) (DB, error)

Connect creates a database connection to a PostgreSQL database.

func Get

func Get(r *http.Request) DB

Get returns the database from the request context.

func NewLoggerDB

func NewLoggerDB(logger logrus.FieldLogger, db DB) DB

NewLoggerDB wraps a database connection with a logger.

type DatabaseEntity

type DatabaseEntity interface {
	SchemaSQL() string
}

DatabaseEntity represents an entity that has schema in the database.

type Middleware

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

Middleware stores a database connection in the request context.

func NewMiddleware

func NewMiddleware(conn DB) *Middleware

func (*Middleware) ServeHTTP

func (m *Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

type Transaction

type Transaction interface {
	DB
	Commit() error
	Rollback() error
}

Transaction represents a database connection with an active transaction.

func GetTx

func GetTx(r *http.Request) Transaction

GetTx returns the transaction from the request context.

type TransactionFactory

type TransactionFactory interface {
	Begin() (Transaction, error)
}

TransactionFactory can initiate a transaction.

type TxMiddleware

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

TxMiddleware stores a database transaction in the request context.

func NewTxMiddleware

func NewTxMiddleware(auto bool) *TxMiddleware

NewTxMiddleware creates a TxMiddleware.

The auto parameter tells the middleware to automatically commit or roll back the transaction based on the response code (roll back over 400, commit below).

func (*TxMiddleware) ServeHTTP

func (m *TxMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

Jump to

Keyboard shortcuts

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