spannerutil

package
v1.116.7 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2024 License: AGPL-3.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Error is the default error class for this package.
	Error = errs.Class("spannerutil")
	// ErrMultipleRows is returned when multiple rows are returned from
	// a query that expects no more than one.
	ErrMultipleRows = errs.Class("more than 1 row returned")
)

Functions

func ArrayOf added in v1.111.4

func ArrayOf(typ *spannerpb.Type) *spannerpb.Type

ArrayOf is a convenience method to construct a Spanner protobuf type for an array of elements.

func BytesType added in v1.111.4

func BytesType() *spannerpb.Type

BytesType is a convenience method to define a Spanner BYTES value.

func CollectRow added in v1.106.1

func CollectRow[T any](iter *spanner.RowIterator, scan func(row *spanner.Row, item *T) error) (r T, _ error)

CollectRow scans a single row query. It returns errors if the iterator doesn't have exactly one row.

func CollectRows added in v1.106.1

func CollectRows[T any](iter *spanner.RowIterator, scan func(row *spanner.Row, item *T) error) (rs []T, _ error)

CollectRows scans each row into a slice.

func CreateRandomTestingDatabaseName added in v1.109.1

func CreateRandomTestingDatabaseName(n int) string

CreateRandomTestingDatabaseName creates a random schema name string.

func DateType added in v1.111.4

func DateType() *spannerpb.Type

DateType is a convenience method to define a Spanner DATE value.

func DialDatabase added in v1.115.1

func DialDatabase(ctx context.Context, params ConnParams) (*spanner.Client, error)

DialDatabase creates a new connection to the spanner instance.

func EncodeBytesToValue added in v1.111.4

func EncodeBytesToValue(bytes []byte) *structpb.Value

EncodeBytesToValue encodes a bytes to what Spanner expects via protobuf.

func EncodeDateToValue added in v1.111.4

func EncodeDateToValue(date civil.Date) *structpb.Value

EncodeDateToValue encodes civil.Date type to what Spanner expects via protobuf.

func EncodeFloat64ToValue added in v1.112.2

func EncodeFloat64ToValue(i float64) *structpb.Value

EncodeFloat64ToValue encodes any float64 type to what Spanner expects via protobuf.

func EncodeIntToValue added in v1.111.4

func EncodeIntToValue[T constraints.Integer](i T) *structpb.Value

EncodeIntToValue encodes any integer type to what Spanner expects via protobuf.

func EncodeStringToValue added in v1.112.2

func EncodeStringToValue(str string) *structpb.Value

EncodeStringToValue encodes string type to what Spanner expects via protobuf.

func EncodeTimeToValue added in v1.111.4

func EncodeTimeToValue(t time.Time) *structpb.Value

EncodeTimeToValue encodes a time.Time to what Spanner expects via protobuf.

func FieldOf added in v1.111.4

func FieldOf(name string, typ *spannerpb.Type) *spannerpb.StructType_Field

FieldOf is a convenience method to construct a Spanner protobuf type for a field of a struct.

func Float64Type added in v1.112.2

func Float64Type() *spannerpb.Type

Float64Type is a convenience method to define a Spanner FLOAT64 value.

func Int64Type added in v1.111.4

func Int64Type() *spannerpb.Type

Int64Type is a convenience method to define a Spanner INT64 value.

func IsAlreadyExists added in v1.111.4

func IsAlreadyExists(err error) bool

IsAlreadyExists is true if err code is AlreadyExists.

func OpenUnique added in v1.109.1

func OpenUnique(ctx context.Context, connstr string, databasePrefix string) (*dbutil.TempDatabase, error)

OpenUnique opens a spanner database with a temporary unique schema, which will be cleaned up when closed. It is expected that this should normally be used by way of "storj.io/storj/shared/dbutil/tempdb".OpenUnique() instead of calling it directly.

func SplitDDL added in v1.115.1

func SplitDDL(ddls string) []string

SplitDDL splits a multi-statement ddl into strings.

func StringType added in v1.111.4

func StringType() *spannerpb.Type

StringType is a convenience method to define a Spanner STRING value.

func StructOf added in v1.111.4

func StructOf(fields ...*spannerpb.StructType_Field) *spannerpb.Type

StructOf is a convenience method to construct a Spanner protobuf type for a struct.

func TimestampType added in v1.111.4

func TimestampType() *spannerpb.Type

TimestampType is a convenience method to define a Spanner STRING value.

func TupleGreaterThanSQL added in v1.112.2

func TupleGreaterThanSQL(tup1, tup2 []string, orEqual bool) (string, error)

TupleGreaterThanSQL returns a constructed SQL expression equivalent to a tuple comparison (e.g. (tup1[0], tup1[1], ...) > (tup2[0], tup2[1], ...)).

If orEqual is true, the returned expression will compare the tuples as "greater than or equal" (>=) instead of "greater than" (>).

This is necessary because Spanner does not support comparison of tuples, except with equality (=).

Example:

(a, b, c) >= (d, e, f)

becomes

TupleGreaterThanSQL([]string{"a", "b", "c"}, []string{"d", "e", "f"}, true)

which returns

"((a > d) OR (a = d AND b > e) OR (a = d AND b = e AND c >= f))"

Types

type ConnParams added in v1.115.1

type ConnParams struct {
	Host string

	Project  string
	Instance string
	Database string

	Emulator bool
}

ConnParams contains arguments from a spanner URL.

func ParseConnStr added in v1.109.1

func ParseConnStr(full string) (params ConnParams, err error)

ParseConnStr parses a spanner connection string to return the relevant pieces of the connection.

func (*ConnParams) AllDefined added in v1.115.1

func (params *ConnParams) AllDefined() bool

AllDefined returns whether project, instance and database are all defined.

func (*ConnParams) ClientOptions added in v1.115.1

func (params *ConnParams) ClientOptions() (options []option.ClientOption)

ClientOptions returns arguments for dialing spanner clients.

func (*ConnParams) ConnStr added in v1.115.1

func (params *ConnParams) ConnStr() string

ConnStr returns connection string.

func (*ConnParams) DatabasePath added in v1.115.1

func (params *ConnParams) DatabasePath() string

DatabasePath returns "projects/<Project>/instances/<Instance>/databases/<Database>".

func (*ConnParams) GoSqlSpannerConnStr added in v1.115.1

func (params *ConnParams) GoSqlSpannerConnStr() string

GoSqlSpannerConnStr returns connection string for github.com/googleapis/go-sql-spanner.

func (*ConnParams) InstancePath added in v1.115.1

func (params *ConnParams) InstancePath() string

InstancePath returns "projects/<Project>/instances/<Instance>".

func (*ConnParams) ProjectPath added in v1.115.1

func (params *ConnParams) ProjectPath() string

ProjectPath returns "projects/<Project>".

type EmulatorAdmin added in v1.109.1

type EmulatorAdmin struct {
	Params    ConnParams
	Instances *instance.InstanceAdminClient
	Databases *database.DatabaseAdminClient
}

EmulatorAdmin provides facilities to communicate with the Spanner Emulator to create new instances and databases.

func OpenEmulatorAdmin added in v1.109.1

func OpenEmulatorAdmin(params ConnParams) *EmulatorAdmin

OpenEmulatorAdmin creates a new emulator admin that uses the specified endpoint.

func (*EmulatorAdmin) Close added in v1.109.1

func (admin *EmulatorAdmin) Close() error

Close closes the underlying clients.

func (*EmulatorAdmin) CreateDatabase added in v1.109.1

func (admin *EmulatorAdmin) CreateDatabase(ctx context.Context, params ConnParams, ddls ...string) error

CreateDatabase creates a new database with the specified name.

func (*EmulatorAdmin) CreateInstance added in v1.109.1

func (admin *EmulatorAdmin) CreateInstance(ctx context.Context, params ConnParams) error

CreateInstance creates a new instance with the specified name.

func (*EmulatorAdmin) DeleteInstance added in v1.109.1

func (admin *EmulatorAdmin) DeleteInstance(ctx context.Context, params ConnParams) error

DeleteInstance deletes an instance with the specified name.

func (*EmulatorAdmin) DropDatabase added in v1.109.1

func (admin *EmulatorAdmin) DropDatabase(ctx context.Context, params ConnParams) error

DropDatabase deletes the specified database.

type EphemeralDB added in v1.115.1

type EphemeralDB struct {
	Params ConnParams
	// contains filtered or unexported fields
}

EphemeralDB manages lifecycle of a temporary database.

func CreateEphemeralDB added in v1.115.1

func CreateEphemeralDB(ctx context.Context, connstr string, databasePrefix string, ddls ...string) (*EphemeralDB, error)

CreateEphemeralDB automatically creates instance and database as necessary.

func (*EphemeralDB) Close added in v1.115.1

func (db *EphemeralDB) Close(ctx context.Context) error

Close deletes the created the instance and database.

type IntValueDecoder

type IntValueDecoder[T inty] struct {
	// contains filtered or unexported fields
}

IntValueDecoder is a type wrapping an int pointer so it can decode integer values from Spanner directly (Spanner prefers to work only in int64s).

func Int

func Int[T inty](val *T) IntValueDecoder[T]

Int wraps a pointer to an int-based type in a type that can be decoded directly from Spanner.

In general, it is preferable to add EncodeSpanner/DecodeSpanner methods to our specialized int types, but this can be used for types we don't own or otherwise can't put methods on.

func (IntValueDecoder[T]) DecodeSpanner

func (s IntValueDecoder[T]) DecodeSpanner(input any) error

DecodeSpanner decodes a value from a Spanner-stored type to the appropriate int type. It implements spanner.Decoder.

Jump to

Keyboard shortcuts

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