sqln

package module
v0.0.0-...-0438637 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2019 License: MIT Imports: 5 Imported by: 0

README

sqln

This package wraps sqlx and manages a map of named statements.

See test file for example usage.

Testing

# Create a docker container to run tests against.
docker run --name sqln-test-postgres -d -p 5432:5432 postgres

go test .

Documentation

Overview

Package sqln maintains a map of queries to named statements and uses named statements for all operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB interface {
	Exec(ctx context.Context, query string, params interface{}) (sql.Result, error)
	Get(ctx context.Context, query string, dest, params interface{}) error
	Select(ctx context.Context, query string, dest, params interface{}) error

	// Stmt creates a named statement if one does not exist. It is not safe
	// to Close the returned statement.
	Stmt(query string) (*sqlx.NamedStmt, error)

	// TODO: Implement nested transactions.
	Transact(ctx context.Context, opts sql.TxOptions, f func(DB) error) error
}

DB is an interface to allow for the Transact method to return a non-concrete type which is useful when wrapping this implementation.

type Database

type Database struct {
	X *sqlx.DB
	// contains filtered or unexported fields
}

Database wraps a sqlx.DB and manages NamedStmt's.

func New

func New(dbx *sqlx.DB) *Database

New wraps a sqlx database.

func (*Database) Close

func (d *Database) Close() error

Close all managed named statements. Does not close underlying *sqlx.DB.

func (*Database) Exec

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

Exec a SQL statement.

func (*Database) Get

func (d *Database) Get(ctx context.Context, query string, dest, params interface{}) error

Get a single record.

func (*Database) Select

func (d *Database) Select(ctx context.Context, query string, dest, params interface{}) error

Select multiple records.

func (*Database) Stmt

func (d *Database) Stmt(query string) (*sqlx.NamedStmt, error)

Stmt creates and/or retrieves a named statement.

func (*Database) Transact

func (d *Database) Transact(ctx context.Context, opts sql.TxOptions, f func(DB) error) error

Transact will run the function that is passed in, rolling back all SQL statements if an error is returned. NOTE: A non-nil TxOptions struct is accepted to encourage thoughtful selection of transaction isolation levels. NOTE: Nested transactions are not currently supported and will return an error.

Jump to

Keyboard shortcuts

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