gsql

package module
v1.1.1 Latest Latest
Warning

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

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

README

Generic SQL

Go Reference

Tiny wrapper built around standard Go SQL library and SQLx with generic support.

Installation

go get -v github.com/reddec/gsql

Examples

Schema for the example

CREATE TABLE book
(
    id     INTEGER NOT NULL PRIMARY KEY,
    title  TEXT    NOT NULL,
    author TEXT,
    year   INTEGER NOT NULL
)

Type in Go

type Book struct {
    ID     int64
    Title  string
    Author string
    Year   int
}

Initialization

// ...
conn, err := sqlx.Open("sqlite", "database.sqlite")
// ...
Get

Get single book by ID

book, err := gsql.Get[Book](ctx, conn, "SELECT * FROM book WHERE id = ?", 1234)
List

List books by author

books, err := gsql.List[Book](ctx, conn, "SELECT * FROM book WHERE author = ?", "O'Really")
Iterate

Iterate each book one by one

iter := gsql.Iterate[Book](ctx, conn, "SELECT * FROM book LIMIT ?", 100)
defer iter.Close()
for iter.Next() {
    book, err := iter.Get()
    // ...
}
if iter.Err() != nil {
    panic(iter.Err()) // use normal handling in real application
}
LazyGet

Save query which returns one object and execute it later

getBooks := gsql.LazyGet[Book](conn, "SELECT * FROM book WHERE id = ?", 123)
// ...
book, err := getBooks(ctx) // can be called many times
LazyList

Save query which returns multiple objects and execute it later

listBooks := gsql.LazyList[Book](conn, "SELECT * FROM book")
// ...
books, err := listBooks(ctx) // can be called many times
CachedGet

Save query which returns one object and execute it later. Once query executed, result will be cached until Invalidate or Refresh called.

cache := gsql.CachedGet[Book](conn, "SELECT * FROM book WHERE id = ?", 123)
book, err := cache.Get(ctx) // first time it will execute the query
//...
book2, err := cache.Get(ctx) // second time it will return cached information
//...
cache.Invalidate() // reset cache, the following Get will again execute the query
CachedList

Save query which returns multiple objects and execute it later. Once query executed, result will be cached until Invalidate or Refresh called.

cache := gsql.CachedList[Book](conn, "SELECT * FROM book WHERE id = ?", 123)
books, err := cache.Get(ctx) // first time it will execute the query
//...
books2, err := cache.Get(ctx) // second time it will return cached information
//...
cache.Invalidate() // reset cache, the following Get will again execute the query

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. It will automatically [Close] iterator.

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)
}

Directories

Path Synopsis
pkg module

Jump to

Keyboard shortcuts

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