db

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2022 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, conn *sql.Conn) 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, conn *sql.Conn, 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, conn *sql.Conn) 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 input variables
	In map[string]any
	// The output variables
	Out map[string]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 QueryResult[R]
	// 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
	// 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, conn *sql.Conn) error

Runs the query against the given context and connection.

func (*Query[R]) SetColumns

func (q *Query[R]) SetColumns(columns []string)

Preps the query for receiving rows with the given columns

type QueryResult

type QueryResult[R any] interface {
	Add(result R) bool
}

A type that can receive rows from a query.

type QuerySlice

type QuerySlice[R any] struct {
	Slice *[]R
	Max   int
}

A QueryResult for a slice.

func (*QuerySlice[R]) Add

func (st *QuerySlice[R]) Add(result R) bool

type Runnable

type Runnable interface {
	Run(ctx context.Context, conn *sql.Conn) error
}

A type which runs a query on a given connection.

Jump to

Keyboard shortcuts

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