db

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunConcurrently

func RunConcurrently(many []Runnable, ctx context.Context, able Queryable) error

Runs a list of queries at the same time. If any of them errors it will try to stop all other queries and return as soon as possible.

func RunPooled

func RunPooled(many []Runnable, ctx context.Context, able Queryable, poolSize int) error

Runs a list of queries at the same time. If any of them errors it will try to stop all other queries and return as soon as possible.

func RunSequential

func RunSequential(many []Runnable, ctx context.Context, able Queryable) error

Runs a list of queries sequentially

Types

type Query

type Query[R any] struct {
	// The query, sql statements, or the stored procedure to query/execute.
	Query string
	// If the query will be called multiple times setting this to true
	// will generate a reusable prepared statement that will be used for
	// subsequent runs.
	Prepared bool
	// The named input variables
	In map[string]any
	// The output variables
	Out map[string]any
	// The position input variables
	InArgs []any
	// The position output variables
	OutArgs []any
	// Creates a row to be populated. This may be necessary for complex
	// types with pointers, slices, maps, etc.
	Create func() R
	// Where to place the rows received. The presense of this field
	// indicates that rows should be requested and parsed.
	Results func(result R, index int) bool
	// Where to place a single result row. The presense of this field
	// indicates that a row should be requested and parsed.
	Result *R
	// How many rows were affected on the last query run.
	RowsAffected int64
	// The ID of the last inserted record on the last query run.
	LastID int64
	// The columns returned from a query
	Columns []*sql.ColumnType
	// contains filtered or unexported fields
}

A query to run against a database. For stored procedures the query is the stored procedure name. If Prepared is true the query will generated a prepared statement on the first run. If Results or Result is given those values are populated, otherwise the query is executed and RowsAffected and LastID could be updated.

func (*Query[R]) GetArgs

func (q *Query[R]) GetArgs() []any

Converts the in & out to arguments to pass to the query.

func (Query[R]) GetValues

func (q Query[R]) GetValues(row *R) []any

Gets the references to values in the given row based on the columns prepped.

func (*Query[R]) NewRow

func (q *Query[R]) NewRow() *R

Creates a new row for this query.

func (*Query[R]) Run

func (q *Query[R]) Run(ctx context.Context, able Queryable) error

Runs the query against the given context and connection.

func (*Query[R]) SetColumns

func (q *Query[R]) SetColumns(columns []*sql.ColumnType)

Preps the query for receiving rows with the given columns

type Queryable added in v0.2.0

type Queryable interface {
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

An interface that covers a connection or transaction

type Runnable

type Runnable interface {
	Run(ctx context.Context, able Queryable) error
}

A type which runs a query on a given connection.

type Runner added in v0.2.0

type Runner func(many []Runnable, ctx context.Context, able Queryable) error

A runner is a function which processes many runnable interface

func NewPooledRunner added in v0.2.0

func NewPooledRunner(poolSize int) Runner

Creates a pooled runner

Jump to

Keyboard shortcuts

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