db

package
v0.0.0-...-08046e1 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2018 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Begin

func Begin(
	ctx context.Context,
	tag string,
) context.Context

Begin returns a new context with a new transaction set.

func Commit

func Commit(
	ctx context.Context,
)

Commit commits the transaction in the current context.

func CreateDBTables

func CreateDBTables(
	ctx context.Context,
	tag string,
	db *sqlx.DB,
) error

CreateDBTables creates the Mint DB tables if they don't exist.

func Ext

func Ext(
	ctx context.Context,
	tag string,
) sqlx.Ext

Ext returns the current Ext (a transaction if one has begin, or the DB otherwise).

func GetDB

func GetDB(
	ctx context.Context,
	tag string,
) *sqlx.DB

GetDB returns the db currently stored in the context under tag.

func GetDBMap

func GetDBMap(
	ctx context.Context,
) map[string]*sqlx.DB

GetDBMap returns the db map currently stored in the context.

func LoggedRollback

func LoggedRollback(ctx context.Context)

LoggedRollback logs a rollback a commit or another rollback didn't take place before this call. Used in general with defer right after calling `Begin`. ```

ctx = tx.Begin(ctx)
defer tx.LoggedRollback(ctx)

```

func Middleware

func Middleware(
	m map[string]*sqlx.DB,
) func(http.Handler) http.Handler

Middleware returns a middleware that injects the specified DB in requests.

func NewDBForDSN

func NewDBForDSN(
	ctx context.Context,
	dsn string,
	defaultDSN string,
) (*sqlx.DB, error)

NewDBForDSN parses the provided DSN and returns the initialized DB for it. Supported DSN are: ```

sqlite3:///home/spolu/foo.db
sqlite3://:memory:
postgres://foo:password@localhost/mydb?sslmode=verify-full

``` If no DSN is specified, the default DSN is used instead.

func NewPostgresDBForDSN

func NewPostgresDBForDSN(
	ctx context.Context,
	dsn string,
) (*sqlx.DB, error)

NewPostgresDBForDSN returns a new Postgres DB found at the DSN provided.

func NewSqlite3DBForPath

func NewSqlite3DBForPath(
	ctx context.Context,
	path string,
) (*sqlx.DB, error)

NewSqlite3DBForPath returns a new sqlite3 DB stored at the provided path or defaulting to `~/.mint/mint-$env.dr`.

func NewSqlite3DBInMemory

func NewSqlite3DBInMemory(
	ctx context.Context,
) (*sqlx.DB, error)

NewSqlite3DBInMemory returns a new in-memory sqlite3 DB.

func RegisterSchema

func RegisterSchema(
	tag string,
	table string,
	schema string,
)

RegisterSchema lets schemas register themselves.

func WithDB

func WithDB(
	ctx context.Context,
	tag string,
	db *sqlx.DB,
) context.Context

WithDB stores the db in the provided context under tag.

func WithDBMap

func WithDBMap(
	ctx context.Context,
	m map[string]*sqlx.DB,
) context.Context

WithDBMap stores the db map in the provided context.

func WithTransaction

func WithTransaction(
	ctx context.Context,
	transaction Transaction,
) context.Context

WithTransaction stores the transaction in the provided context.

Types

type ContextKey

type ContextKey string

ContextKey is the type of the key used with context to carry the contextual db and transaction.

type Transaction

type Transaction struct {
	Tx    *sqlx.Tx
	Token string
}

Transaction stores the current mintDB transaction.

func GetTransaction

func GetTransaction(
	ctx context.Context,
) Transaction

GetTransaction retrieves the current transaction form the context.

Jump to

Keyboard shortcuts

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