snippets

package
v0.0.0-...-e2d27dd Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2025 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbortedTransaction

func AbortedTransaction(projectId, instanceId, databaseId string) error

AbortedTransaction shows how transaction retries work on Spanner. Read/write transactions guarantee the consistency and atomicity of multiple queries and updates on Spanner. Read/write transactions take locks on the rows that are read and updated. Spanner can abort any read/write transaction due to lock conflicts or due to transient failures (e.g. network errors, machine restarts, etc.).

Transactions that fail with an Aborted error should be retried. The Spanner gorm dialect provides the helper function `spannergorm.RunTransaction` for this. It is recommended to run all read/write transactions using this helper function, or add a similar retry function to your own application.

Execute the sample with the command `go run run_sample.go aborted_transaction` from the samples directory.

func ArrayDataType

func ArrayDataType(projectId, instanceId, databaseId string) error

ArrayDataType shows how to map ARRAY columns in Spanner when using gorm.

Execute the sample with the command `go run run_sample.go array_data_type` from this directory.

func AutoSaveAssociations

func AutoSaveAssociations(projectId, instanceId, databaseId string) error

AutoSaveAssociations shows how to create a model with one or more associated models in one Create call. gorm uses an insert-or-update statement for these calls. Spanner only supports INSERT OR UPDATE when *ALL* columns are updated. gorm by default only updates the foreign key value when auto-saving associations. You can work around this by calling `db.Session(&gorm.Session{FullSaveAssociations: true})` before saving the model.

Execute the sample with the command `go run run_sample.go auto_save_associations` from the samples directory.

func BatchDml

func BatchDml(projectId, instanceId, databaseId string) error

BatchDml shows how to use the START BATCH DML / RUN BATCH feature in Spanner to buffer multiple update statements and execute these in one round-trip to Spanner.

Execute the sample with the command `go run run_sample.go batch_dml` from this directory.

func BitReversedSequence

func BitReversedSequence(projectId, instanceId, databaseId string) error

BitReversedSequence shows how to use a bit-reversed sequence to generate the primary key value in gorm. Using a primary key that is generated by Spanner, means that gorm must use a THEN RETURN clause to return the primary key from the server. As a new primary key value is generated for each transaction attempt, it means that transactions must be defined using a transaction runner, or in some other way must be retried using a retry loop.

Execute the sample with the command `go run run_sample.go bit_reversed_sequence` from the samples directory.

func ClientLibrary

func ClientLibrary(projectId, instanceId, databaseId string) error

ClientLibrary shows how to unwrap the underlying client library from a gorm database, so the client can be used to use specific Spanner features.

The underlying Spanner client is stateless and independent of the connection that was used to get it. This means that we can safely use the client after the connection that was used to get it has been closed.

Execute the sample with the command `go run run_sample.go client_library` from the samples directory.

func CreateInBatches

func CreateInBatches(projectId, instanceId, databaseId string) error

CreateInBatches shows how to use the corresponding method in gorm to create multiple records in a limited number of round-trips to Spanner.

Execute the sample with the command `go run run_sample.go hello_world` from this directory.

func CustomSpannerConfig

func CustomSpannerConfig(projectId, instanceId, databaseId string) error

CustomSpannerConfig shows how to connect GORM to Spanner with a custom configuration for the Spanner client.

Execute the sample with the command `go run run_sample.go custom_spanner_config` from this directory.

func DataTypes

func DataTypes(projectId, instanceId, databaseId string) error

DataTypes shows the supported data type mappings for Spanner when using gorm. This sample only contains samples for 'simple' data types. See the following samples for more complex data types: 1. Arrays: array_data_type.go 2. Protobuf: protobuf_columns.go

Execute the sample with the command `go run run_sample.go data_types` from this directory.

func FindInBatches

func FindInBatches(projectId, instanceId, databaseId string) error

FindInBatches shows how to process large amounts of data in smaller batches. This reduces the time that locks are held, and ensures that the Spanner transaction mutation limit is not exceeded.

Execute the sample with the command `go run run_sample.go find_in_batches` from this directory.

func HelloWorld

func HelloWorld(projectId, instanceId, databaseId string) error

HelloWorld is a simple sample application that shows how to connect GORM to Cloud Spanner.

Execute the sample with the command `go run run_sample.go hello_world` from this directory.

func InsertData

func InsertData(projectId, instanceId, databaseId string) error

InsertData shows how to insert data into Spanner in one round-trip when using gorm. Creating records in one call to Create reduces the number of round-trips to Spanner.

Execute the sample with the command `go run run_sample.go insert_data` from this directory.

func InterleavedTables

func InterleavedTables(projectId, instanceId, databaseId string) error

InterleavedTables shows how to map and use interleaved tables with gorm. Interleaved tables can be mapped as a 'has many' (https://gorm.io/docs/has_many.html) association in gorm.

Execute the sample with the command `go run run_sample.go interleaved_tables` from the samples directory.

func IsolationLevel

func IsolationLevel(projectId, instanceId, databaseId string) error

IsolationLevel shows how to use a custom transaction isolation level with Spanner and gorm.

Read/write transactions on Spanner take locks at the cell level (row/column). It is therefore recommended to only read the columns that you actually need.

Spanner can abort any read/write transaction due to lock conflicts or due to transient failures (e.g. network errors, machine restarts, etc.). Transactions that fail with an Aborted error should be retried. The Spanner gorm dialect provides the helper function `spannergorm.RunTransaction` for this.

Execute the sample with the command `go run run_sample.go isolation_level` from the samples directory.

func LastStatement

func LastStatement(projectId, instanceId, databaseId string) error

LastStatement shows how you can disable the use of default transactions in gorm to use the last_statement optimization in Spanner. This sample inserts a batch of data into Spanner in one round-trip using gorm while skipping a default transaction. This again allows the underlying database/sql driver to set the last_statement=true flag, which again allows Spanner to optimize the execution of that statement.

See https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#executesqlrequest for more information on the last_statement option.

Execute the sample with the command `go run run_sample.go last_statement` from this directory.

func Migrations

func Migrations(projectId, instanceId, databaseId string) error

Migrations shows how to (dry-)run gorm migrations with Spanner. Not all Spanner features can be created with gorm migrations. E.g. INTERLEAVED tables are not supported by gorm, and can not be created automatically using migrations.

It is therefore recommended to dry-run migrations first and inspect the DDL statements that are generated. Modify and execute these manually if you want to create interleaved tables, or if the generated data model for example contains more secondary indexes than you actually want in your database.

Execute the sample with the command `go run run_sample.go migrations` from the samples directory.

func ProtobufColumns

func ProtobufColumns(projectId, instanceId, databaseId string) error

ProtobufColumns shows how to map and use protobuf columns with gorm.

Execute the sample with the command `go run run_sample.go protobuf_columns` from the samples directory.

func ReadOnlyTransaction

func ReadOnlyTransaction(projectId, instanceId, databaseId string) error

ReadOnlyTransaction shows how to use read-only transactions with Spanner and gorm. Read-only transactions do not take locks, and are therefore more efficient than read/write transactions for workloads that only read data.

Execute the sample with the command `go run run_sample.go read_only_transaction` from the samples directory.

func ReadWriteTransaction

func ReadWriteTransaction(projectId, instanceId, databaseId string) error

ReadWriteTransaction shows how to use read/write transactions with Spanner and gorm. Read/write transactions guarantee the consistency and atomicity of multiple queries and updates on Spanner. Read/write transactions take locks on the rows that are read and updated, and should therefore be as short as possible, and only read/update the data that it actually needs.

Read/write transactions on Spanner take locks at the cell level (row/column). It is therefore recommended to only read the columns that you actually need.

Spanner can abort any read/write transaction due to lock conflicts or due to transient failures (e.g. network errors, machine restarts, etc.). Transactions that fail with an Aborted error should be retried. The Spanner gorm dialect provides the helper function `spannergorm.RunTransaction` for this.

Execute the sample with the command `go run run_sample.go read_write_transaction` from the samples directory.

func Upsert

func Upsert(projectId, instanceId, databaseId string) error

Upsert shows how to use INSERT-OR-UPDATE and INSERT-OR-IGNORE with Spanner and gorm.

Execute the sample with the command `go run run_sample.go upsert` from this directory.

func UuidPrimaryKey

func UuidPrimaryKey(projectId, instanceId, databaseId string) error

UuidPrimaryKey shows how to use a client-side generated UUID as the primary key in gorm. Using a primary key that is either generated client-side or otherwise set by the client, means that gorm does not need to use a THEN RETURN clause to return the primary key from the server.

Execute the sample with the command `go run run_sample.go uuid_primary_key` from the samples directory.

Types

type AllNullTypes

type AllNullTypes struct {
	ID           int64
	ColBool      spanner.NullBool
	ColDate      spanner.NullDate
	ColFloat32   spanner.NullFloat32
	ColFloat64   spanner.NullFloat64
	ColInt64     spanner.NullInt64
	ColJson      spanner.NullJSON
	ColNumeric   spanner.NullNumeric
	ColString    spanner.NullString
	ColTimestamp spanner.NullTime
}

AllNullTypes shows how the Spanner-specific Null* types can be used to map any data type in Spanner.

type AllTypes

type AllTypes struct {
	ID           int64
	ColBool      bool
	ColBytes     []byte
	ColDate      civil.Date `gorm:"type:date"`
	ColFloat32   float32
	ColFloat64   float64
	ColInt64     int64
	ColJson      spanner.NullJSON
	ColNumeric   big.Rat `gorm:"type:numeric"`
	ColString    string
	ColTimestamp time.Time
}

AllTypes shows the supported native Go type mappings to Spanner. Most of these types do not support NULL values.

type SqlNullTypes

type SqlNullTypes struct {
	ID           int64
	ColBool      sql.NullBool
	ColFloat64   sql.NullFloat64
	ColInt64     sql.NullInt64
	ColString    sql.NullString
	ColTimestamp sql.NullTime
}

SqlNullTypes shows the sql.Null* types that can be used with Spanner.

type TicketSaleWithNativeArray

type TicketSaleWithNativeArray struct {
	gorm.Model
	CustomerName string
	Seats        spannergorm.StringArray
	Concert      *sample_model.Concert
	ConcertId    int64
}

func (TicketSaleWithNativeArray) TableName

func (TicketSaleWithNativeArray) TableName() string

type TicketSaleWithNullArray

type TicketSaleWithNullArray struct {
	gorm.Model
	CustomerName string
	Seats        spannergorm.NullStringArray
	Concert      *sample_model.Concert
	ConcertId    int64
}

func (TicketSaleWithNullArray) TableName

func (TicketSaleWithNullArray) TableName() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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