spxscan

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2024 License: MIT Imports: 7 Imported by: 3

README

spxscan

The spxscan package was inspired by pgxscan and provides similar functionality. Package spxscan allows scanning data into Go structs and other composite types, when working with the spanner package.

Getting Started

First, get the package by running:

go get github.com/cccteam/spxscan

Documentation

Overview

Package scan allows scanning data into Go structs and other composite types, when working with spanner library native interface.

Index

Constants

This section is empty.

Variables

View Source
var DefaultAPI = &API{}

DefaultAPI is the default instance of API with all configuration settings set to default.

View Source
var ErrNotFound = errors.New("spxscan: no row was found")

ErrNotFound is returned by ScanOne if there were no rows.

Functions

func Get

func Get(ctx context.Context, db Querier, dst any, statement spanner.Statement) error

Get is a package-level helper function that uses the DefaultAPI object. See API.Get for details.

func NotFound

func NotFound(err error) bool

NotFound returns true if err is a not found error. This error is returned by ScanOne if there were no rows.

func Select

func Select(ctx context.Context, db Querier, dst any, statement spanner.Statement) error

Select is a package-level helper function that uses the DefaultAPI object. See API.Select for details.

func UpdateAndGet added in v0.0.3

func UpdateAndGet(ctx context.Context, client TxnRunner, dst any, statement spanner.Statement) error

UpdateAndGet is a package-level helper function that uses the DefaultAPI object. See API.UpdateAndGet for details.

func UpdateAndSelect added in v0.0.3

func UpdateAndSelect(ctx context.Context, client TxnRunner, dst any, statement spanner.Statement) error

UpdateAndSelect is a package-level helper function that uses the DefaultAPI object. See API.UpdateAndSelect for details.

Types

type API

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

API is the core type in spxscan. It implements all the logic and exposes functionality available in the package. With API type users can create a custom API instance and override default settings hence configure spxscan.

func NewAPI

func NewAPI(opts ...APIOption) *API

NewAPI creates a new API object with provided list of options.

func (*API) Get

func (api *API) Get(ctx context.Context, db Querier, dst any, statement spanner.Statement) error

Get is a high-level function that queries rows from Querier and calls the ScanOne function. See ScanOne for details.

func (*API) ScanAll

func (api *API) ScanAll(dst any, iter *spanner.RowIterator) error

ScanAll iterates all rows to the end. After iterating it closes the interator, and propagates any errors that could pop up. It expects that destination should be a slice. For each row it scans data and appends it to the destination slice. ScanAll supports both types of slices: slice of structs by a pointer and slice of structs by value, for example:

type User struct {
    ID    string
    Name  string
}

var usersByPtr []*User
var usersByValue []User

Both usersByPtr and usersByValue are valid destinations for ScanAll function.

Before starting, ScanAll resets the destination slice, so if it's not empty it will overwrite all existing elements.

func (*API) ScanOne

func (api *API) ScanOne(dst any, iter *spanner.RowIterator) error

ScanOne makes sure that there was exactly one row otherwise it returns an error. Use NotFound function to check if there were no rows. After iterating ScanOne closes the interator, and propagates any errors that could pop up. It scans data from that single row into the destination.

func (*API) Select

func (api *API) Select(ctx context.Context, db Querier, dst any, statement spanner.Statement) error

Select is a high-level function that queries rows from Querier and calls the ScanAll function. See ScanAll for details.

func (*API) UpdateAndGet added in v0.0.3

func (api *API) UpdateAndGet(ctx context.Context, client TxnRunner, dst any, statement spanner.Statement) error

UpdateAndGet is a package-level helper function that uses the API.Get() call inside a transaction. This should be used when data is being returned after an update using the THEN RETURN clause.

func (*API) UpdateAndSelect added in v0.0.3

func (api *API) UpdateAndSelect(ctx context.Context, client TxnRunner, dst any, statement spanner.Statement) error

UpdateAndSelect is a high-level helper function that uses the API.Select() call inside a transaction. This should be used when data is being returned after an update using the THEN RETURN clause.

type APIOption

type APIOption func(api *API)

APIOption is a function type that changes API configuration.

func WithStructLenient

func WithStructLenient(lenient bool) APIOption

WithStructLenient uses Row.ToStructLenient() for scanning to destination.

type Querier

type Querier interface {
	Query(ctx context.Context, statement spanner.Statement) *spanner.RowIterator
}

type TxnRunner added in v0.0.3

type TxnRunner interface {
	ReadWriteTransaction(ctx context.Context, f func(context.Context, *spanner.ReadWriteTransaction) error) (commitTimestamp time.Time, err error)
}

Jump to

Keyboard shortcuts

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