Documentation
¶
Overview ¶
package spxapi implements the core logic of the API type and exposes the functionality available in the package. With the API type, users can create a custom API instance and override default settings to configure spxscan.
Index ¶
- Variables
- func NotFound(err error) bool
- func ScanSeq[T any](api *API, rowIter *spanner.RowIterator) iter.Seq2[*T, error]
- func SelectSeq[T any](ctx context.Context, api *API, db Querier, statement spanner.Statement) iter.Seq2[*T, 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 Default = &API{lenient: false} //nolint:gochecknoglobals // Default is a package-level singleton providing default configuration.
Default 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 NotFound ¶
NotFound returns true if err is a not found error. This error is returned by ScanOne if there were no rows.
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 ¶
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 ¶
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.