Documentation ¶
Overview ¶
Package scan allows scanning data into Go structs and other composite types, when working with spanner library native interface.
Index ¶
- Variables
- func Get(ctx context.Context, db Querier, dst any, statement spanner.Statement) error
- func NotFound(err error) bool
- func Select(ctx context.Context, db Querier, dst any, statement spanner.Statement) error
- func UpdateAndGet(ctx context.Context, client TxnRunner, dst any, statement spanner.Statement) error
- func UpdateAndSelect(ctx context.Context, client TxnRunner, dst any, statement spanner.Statement) error
- type API
- func (api *API) Get(ctx context.Context, db Querier, dst any, statement spanner.Statement) error
- func (api *API) ScanAll(dst any, iter *spanner.RowIterator) error
- func (api *API) ScanOne(dst any, iter *spanner.RowIterator) error
- func (api *API) Select(ctx context.Context, db Querier, dst any, statement spanner.Statement) error
- func (api *API) UpdateAndGet(ctx context.Context, client TxnRunner, dst any, statement spanner.Statement) error
- func (api *API) UpdateAndSelect(ctx context.Context, client TxnRunner, dst any, statement spanner.Statement) error
- type APIOption
- type Querier
- type TxnRunner
Constants ¶
This section is empty.
Variables ¶
var DefaultAPI = &API{}
DefaultAPI is the default instance of API with all configuration settings set to default.
var ErrNotFound = errors.New("spxscan: no row was found")
ErrNotFound is returned by ScanOne if there were no rows.
Functions ¶
func Get ¶
Get is a package-level helper function that uses the DefaultAPI object. See API.Get for details.
func NotFound ¶
NotFound returns true if err is a not found error. This error is returned by ScanOne if there were no rows.
func Select ¶
Select is a package-level helper function that uses the DefaultAPI object. See API.Select 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 (*API) Get ¶
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 ¶
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 ¶
WithStructLenient uses Row.ToStructLenient() for scanning to destination.