gsql

package module
v0.0.0-...-77fe795 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2022 License: ISC Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get[T any](ctx context.Context, db sqlx.QueryerContext, query string, args ...any) (T, error)

Get single result or return an error.

func LazyGet

func LazyGet[T any](db sqlx.QueryerContext, query string, args ...any) func(ctx context.Context) (T, error)

LazyGet generates closure which wraps prepared query with arguments and executes query with single result on-demand. The function doesn't cache anything, use Cache for lazy cached queries.

func LazyList

func LazyList[T any](db sqlx.QueryerContext, query string, args ...any) func(ctx context.Context) ([]T, error)

LazyList generates closure which wraps prepared query with arguments and executes query on-demand. The function doesn't cache anything, use Cache for lazy cached queries.

func List

func List[T any](ctx context.Context, db sqlx.QueryerContext, query string, args ...any) ([]T, error)

List creates slice of results based on SQL query. In case of zero results it will return non-nil empty slice.

Types

type Cache

type Cache[T any] struct {
	// contains filtered or unexported fields
}

Cache stores data and factory to get data.

func CachedGet

func CachedGet[T any](db sqlx.QueryerContext, query string, args ...any) *Cache[T]

CachedGet is alias of NewCache(LazyGet) and provides cached information from database.

func CachedList

func CachedList[T any](db sqlx.QueryerContext, query string, args ...any) *Cache[[]T]

CachedList is alias of NewCache(LazyList) and provides cached information from database.

func NewCache

func NewCache[T any](factory func(ctx context.Context) (T, error)) *Cache[T]

NewCache creates new concurrent-safe cache for internal data.

func (*Cache[T]) Get

func (ct *Cache[T]) Get(ctx context.Context) (T, error)

Get content from cache or from storage. Once data fetched, it will be stored internally.

func (*Cache[T]) Invalidate

func (ct *Cache[T]) Invalidate()

Invalidate cache and cause refresh on next Cache.Get operation.

func (*Cache[T]) Refresh

func (ct *Cache[T]) Refresh(ctx context.Context) error

Refresh cache regardless of validity.

type Iterator

type Iterator[T any] struct {
	// contains filtered or unexported fields
}

Iterator is typed wrapper around sql.Rows which is automatically scans row to the provided type.

func Iterate

func Iterate[T any](ctx context.Context, db sqlx.QueryerContext, query string, args ...any) *Iterator[T]

Iterate over multiple results and automatically scans each row to the provided type.

func Rows

func Rows[T any](rows *sqlx.Rows) *Iterator[T]

Rows is a simple wrapper around sqlx.Rows which is automatically scans row to the provided type.

func (*Iterator[T]) Close

func (it *Iterator[T]) Close() error

Close database cursor and allocated resources.

func (*Iterator[T]) Collect

func (it *Iterator[T]) Collect() ([]T, error)

Collect all results as slice.

func (*Iterator[T]) Err

func (it *Iterator[T]) Err() error

Err returns an error from the rows or from the initial query.

func (*Iterator[T]) Get

func (it *Iterator[T]) Get() (T, error)

Get row and scan data to the type.

func (*Iterator[T]) Next

func (it *Iterator[T]) Next() bool

Next is reads next row and returns true if data is available.

type Selector

type Selector interface {
	QueryContext(context.Context, string, ...any) (*sql.Rows, error)
}

Jump to

Keyboard shortcuts

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