spannerutil

package
v1.119.2 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: AGPL-3.0 Imports: 37 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 MustSplitSQLStatements added in v1.118.4

func MustSplitSQLStatements(s string) (statements []string)

MustSplitSQLStatements works like SplitSQLStatements, but panics on error.

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 QueryData added in v1.118.4

func QueryData(ctx context.Context, db dbschema.Queryer, schema *dbschema.Schema) (*dbschema.Data, error)

QueryData loads all data from tables.

func QuerySchema added in v1.118.4

func QuerySchema(ctx context.Context, db dbschema.Queryer) (*dbschema.Schema, error)

QuerySchema loads the schema from postgres database.

func QuerySnapshot added in v1.118.4

func QuerySnapshot(ctx context.Context, db dbschema.Queryer) (*dbschema.Snapshot, error)

QuerySnapshot loads snapshot from database.

func QuoteIdentifier added in v1.118.4

func QuoteIdentifier(identifier string) string

QuoteIdentifier quotes an identifier appropriately for use by Spanner.

func SplitSQLStatements added in v1.118.4

func SplitSQLStatements(s string) (statements []string, err error)

SplitSQLStatements splits a string into SQL statements, honoring string literals and comments. The semicolons used to separate statements are not included in the returned statements.

Comments are stripped from the returned statements, as in some cases Spanner can't parse them. Maybe it's just the emulator? They're documented as being supported.

Empty statements (consisting of only whitespace) are also stripped. Whitespace in other respects is maintained.

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

func UnderlyingClient added in v1.118.4

func UnderlyingClient(ctx context.Context, db tagsql.DB, fn func(client *spanner.Client) error) error

UnderlyingClient implements exposing *spanner.Client from a tagsql.DB.

Types

type ConnParams added in v1.115.1

type ConnParams struct {
	Host string

	Project  string
	Instance string
	Database string

	Emulator bool

	CreateInstance ConnParamsCreateInstance
}

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 ConnParamsCreateInstance added in v1.117.3

type ConnParamsCreateInstance struct {
	DisplayName     string
	Config          string
	NodeCount       int32
	ProcessingUnits int32
}

ConnParamsCreateInstance contains arguments for creating a new instance.

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.

type MultiExecDBWrapper added in v1.118.4

type MultiExecDBWrapper struct {
	tagsql.DB
}

MultiExecDBWrapper wraps a tagsql.DB to override ExecContext behavior; namely, it splits up queries containing multiple statements and executes them individually.

This must only be used in cases where it is acceptable for some statements to succeed and others to fail. There is (currently) no way to get transactional behavior for multiple DDL statements in Spanner.

func (*MultiExecDBWrapper) ExecContext added in v1.118.4

func (m *MultiExecDBWrapper) ExecContext(ctx context.Context, query string, args ...interface{}) (result sql.Result, err error)

ExecContext executes all statements in a query, separated by semicolons. Important: the result returned is that of the _last_ statement, not any sort of combination of all results.

Jump to

Keyboard shortcuts

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