sqlrepo

package
v0.0.0-...-bcf6333 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2018 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const Placeholder = "?"

Placeholder is the only placeholder recognized by this package. It is transparently parsed in queries into the Dialect specific representation.

Variables

This section is empty.

Functions

func IdsPlaceholdersArgs

func IdsPlaceholdersArgs(ids []data.Id) (string, []interface{})

IdsPlaceholdersArgs returns a list of placeholders (from List(Placeholder, len(ids)) and a slice of arguments that is a copy of ids.

func List

func List(value string, count int) string

List returns a comma separated list of value repeated count times. A non-positive count results in the empty string.

func Normalize

func Normalize(d Dialect, query string) string

Normalize returns query with every instance of Placeholder replaced by a call to d.Placeholder(index) where index is the occurance index of Placeholder in query.

Types

type Dialect

type Dialect interface {
	//Placeholder should return the DBMS specific placeholder for index.
	//Index is zero-based.
	Placeholder(index int) string
}

Dialect allows client code to use this package in a DBMS independent way by providing methods to abstract the differences betwenn DBMSs.

type ExecerContext

type ExecerContext interface {
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
}

ExecerContext provides the execute methods with Contexts for a sql database.

type PostgresqlDialect

type PostgresqlDialect struct{}

PostgresqlDialect is a Dialect that understands the Postgresql DBMS.

func (PostgresqlDialect) Placeholder

func (p PostgresqlDialect) Placeholder(index int) string

Placeholder is the Dialect implementation.

type QueryExecerContext

type QueryExecerContext interface {
	QueryerContext
	ExecerContext
}

QueryExecerContext provides the query and exec with Contexts methods for a sql database.

type QueryerContext

type QueryerContext interface {
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}

QueryerContext provides the query methods with Contexts for a sql database.

type Repo

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

Repo provides utility methods to help working with the SQL package.

func New

func New(db *sql.DB, d Dialect) *Repo

New returns a new Repo that connects to db and uses the Dialect d.

func (*Repo) BeginContext

func (r *Repo) BeginContext(ctx context.Context) (Tx, error)

BeginContext starts a transaction in r. The interface returned wraps the QueryExecerContext and the necessary methods on the sql.Tx type. it calls BeginTx on r's underlying sql.DB.

func (*Repo) ExecContext

func (r *Repo) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

ExecContext is the QueryExecerContext implementation. It calls ExecContext on r's underlying sql.DB.

func (*Repo) QueryContext

func (r *Repo) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

QueryContext is the QueryerContext implementation. It calls QueryContext on r's underlying sql.DB.

func (*Repo) QueryRowContext

func (r *Repo) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

QueryRowContext is the QueryerContext implementation. It calls QueryRowContext on r's underlying sql.DB.

func (*Repo) TxWorkContext

func (r *Repo) TxWorkContext(ctx context.Context, work func(QueryExecerContext) error) error

TxWorkContext executes work inside of a transaction with ctx with committing and rollback handled for you.

type Scanner

type Scanner interface {
	//Scan is the method to get a row into data.
	Scan(dst ...interface{}) error
}

Scanner is a wrapper interface around sql.Rows and sql.Row.

type Tx

type Tx interface {
	//QueryExecerContext provides the query and exec context methods.
	QueryExecerContext

	//Commit commits the transaction.
	Commit() error

	//Rollback rolls back the transaction.
	Rollback() error
}

Tx is a QueryExecer with the Commit and Rollback methods required for transactions. It also provides a Stmt method so prepared statements can be executed within this transaction.

type UTCTime

type UTCTime struct {
	Time time.Time
}

UTCTime is a helper type that forces value-ing and scanning Time values to and from the UTC Timezone.

func (*UTCTime) Scan

func (t *UTCTime) Scan(src interface{}) error

Scan attempts to scan src into t.Time. If scanning is successful, t.Time will be in UTC Timezone via time.Time.UTC().

func (UTCTime) Value

func (t UTCTime) Value() (driver.Value, error)

Value returns t.Time.In(time.UTC), nil.

Jump to

Keyboard shortcuts

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