gorm

package module
v1.9.1 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2026 License: Apache-2.0 Imports: 22 Imported by: 7

README

go-gorm-spanner

go.dev reference

Google Cloud Spanner ORM for Go's GORM implementation.

import (
    "gorm.io/gorm"
    _ "github.com/googleapis/go-sql-spanner"

    spannergorm "github.com/googleapis/go-gorm-spanner"
)

db, err := gorm.Open(spannergorm.New(spannergorm.Config{
    DriverName: "spanner",
    DSN:        "projects/PROJECT/instances/INSTANCE/databases/DATABASE",
}), &gorm.Config{PrepareStmt: true})
if err != nil {
    log.Fatal(err)
}

// Print singers with more than 500 likes.
type Singer struct {
    gorm.Model
    Text         string
    Likes        int
}
var singers []Singer
if err := db.Where("likes > ?", 500).Find(&singers).Error; err != nil {
    log.Fatal(err)
}
for s := range singers {
    fmt.Println(s.ID, s.Text)
}
Connection URL Properties

The Cloud Spanner GORM supports the following connection URL properties

Commonly Used Properties
  • credentials (String): File name for the credentials to use. The connection will use the default credentials of the environment if no credentials file is specified in the connection string. Example: projects/my-project/instances/my-instance/databases/my-db;credentials=/path/to/credentials.json
  • optimizerVersion (String): Sets the default query optimizer version to use for this connection. See also https://cloud.google.com/spanner/docs/query-optimizer/query-optimizer-versions.
  • isolationLevel (String): Sets the default isolation level for read/write transaction. The default is sql.LevelSerializable. Other supported values are sql.LevelRepeatableRead. Example: fmt.Sprintf("projects/my-project/instances/my-instance/databases/my-db;isolationLevel=%s", sql.LevelRepeatableRead)

Note that Spanner gorm uses a single multiplexed session for all operations from version 1.9.0 and higher. Multiplexed sessions can handle any number of concurrent queries and transactions. This means that there is no need to configure the minimum and maximum number of sessions for gorm.

See https://cloud.google.com/spanner/docs/sessions#multiplexed_sessions for more information about multiplexed sessions.

Advanced Properties
  • numChannels (int): Sets the number of gRPC channels to use. Defaults to 4.
  • retryAbortsInternally (boolean): Boolean that indicates whether the connection should automatically retry aborted errors. The default is true.
  • disableRouteToLeader (boolean): Boolean that indicates if all the requests of type read-write and PDML need to be routed to the leader region. The default is false.
  • usePlainText (boolean): : Boolean that indicates whether the connection should use plain text communication or not. Set this to true to connect to local mock servers that do not use SSL. Example: projects/test-project/instances/test-instance/databases/test-db;usePlainText=true

Example: projects/my-project/instances/my-instance/databases/my-db;numChannels=4;retryAbortsInternally=true;disableRouteToLeader=false;usePlainText=false

Additional Spanner Configuration

You can also connect gorm to Spanner using a driver.Connector. This allows you to supply additional configuration for the Spanner client that should be used for gorm:

// Create a function that sets the Spanner client configuration for the database connection.
configureFunction := func(config *spanner.ClientConfig, opts *[]option.ClientOption) {
    // Set a default query optimizer version that the client should use.
    config.QueryOptions = spanner.QueryOptions{Options: &spannerpb.ExecuteSqlRequest_QueryOptions{OptimizerVersion: "1"}}
}
// Create a ConnectorConfig to use with the Spanner database/sql driver.
config := spannerdriver.ConnectorConfig{
    Project:      projectId,
    Instance:     instanceId,
    Database:     databaseId,
    Configurator: configureFunction,
}
// Create a Connector for Spanner. This Connector instance should be re-used for all gorm connections.
c, err := spannerdriver.CreateConnector(config)
db, err := gorm.Open(
    spannergorm.New(spannergorm.Config{Connector: c}),
    &gorm.Config{PrepareStmt: true})

See custom_spanner_config.go for a working sample application.

Emulator

See the Google Cloud Spanner Emulator support to learn how to start the emulator. When the emulator has been started and the environment variable has been set, gorm will automatically connect to the emulator instead of Cloud Spanner.

$ gcloud emulators spanner start
$ export SPANNER_EMULATOR_HOST=localhost:9010

Go Versions Supported

The Spanner gorm dialect follows the Go release policy and supports the two latest major Go versions.

Data Types

Spanner supports the following data types in combination with gorm.

Cloud Spanner Type gorm / go type
bool bool, sql.NullBool, spanner.NullBool
int64 uint, int64, sql.NullInt64, spanner.NullInt64
string string, sql.NullString, spanner.NullString
json spanner.NullJSON
float64 float64, sql.NullFloat64, spanner.NullFloat64
float32 float32, spanner.NullFloat32, spanner.NullFloat32
numeric big.Rat, spanner.NullNumeric
timestamp with time zone time.Time, sql.NullTime, spanner.NullTime
date civil.Date, spanner.NullDate
bytes []byte

See data_types.go for a working sample for each data type.

You can also use arrays and protobuf columns with gorm. See the following samples for how to map and use those types:

Auto-increment Primary Keys

Columns that are marked as auto-increment in gorm use IDENTITY columns in Spanner by default. IDENTITY columns use a backing bit-reversed sequence for value generation. These values are guaranteed to be unique and safe to use as generated primary key values with Spanner. See https://cloud.google.com/spanner/docs/primary-key-default-value#identity-columns for more information on IDENTITY columns.

The following model uses an IDENTITY column with a backing bit-reversed sequence for primary key values:

type singer struct {
	gorm.Model
	Name string
}
Sequence Kind

IDENTITY columns by default use a bit-reversed sequence to generate values. You can set the type of sequence to use in the gorm dialector configuration. The default and currently only supported value is BIT_REVERSED_POSITIVE.

In addition, the configuration option supports two special values:

  • DISABLED: Disable the use of IDENTITY columns and use bit-reversed sequences instead. See below for more information.
  • AUTO_INCREMENT: Generate AUTO_INCREMENT columns instead of IDENTITY columns. AUTO_INCREMENT columns use the default sequence kind that has been configured in the database. See https://cloud.google.com/spanner/docs/primary-key-default-value#serial-auto-increment for more information on AUTO_INCREMENT columns.

Example:

// This dialector uses AUTO_INCREMENT columns instead of IDENTITY columns.
dialector := New(Config{
    DriverName:          "spanner",
    DSN:                 fmt.Sprintf("projects/my-project/instances/my-instance/databases/my-database"),
    DefaultSequenceKind: "AUTO_INCREMENT",
})
Sequences

You can also manually assign a sequence to be used for primary key generation for a model. Use the gorm_sequence_name tag to specify a sequence name.

This model uses a sequence named singer_sequence for primary key generation.

type singer struct {
	ID       uint `gorm:"primarykey" gorm_sequence_name:"singer_sequence"`
	Name     string
}

You can also configure Spanner gorm to use sequences for to generate primary key values for all models. Set the DefaultSequenceKind in the dialector configuration for this:

dialector := New(Config{
    DriverName:          "spanner",
    DSN:                 fmt.Sprintf("projects/my-project/instances/my-instance/databases/my-database"),
    DefaultSequenceKind: "DISABLED",
})

// This model uses a bit-reversed sequence for primary key generation,
// as DefaultSequenceKind has been set to DISABLED in the configuration.
type singer struct {
    gorm.Model
    Name string
}

AutoMigrate Dry Run

The Spanner gorm dialect supports dry-runs for auto-migration. Use this to get the DDL statements that would be generated and executed by auto-migration. You can manually verify and modify these statements to optimize your data model.

Example:

tables := []interface{}{&singer{}, &album{}}

// Unwrap the underlying SpannerMigrator interface. This interface supports
// the `AutoMigrateDryRun` method, which does not actually execute the
// generated statements, and instead just returns these as an array.
m := db.Migrator()
migrator, ok := m.(spannergorm.SpannerMigrator)
if !ok {
    return fmt.Errorf("unexpected migrator type: %v", m)
}
statements, err := migrator.AutoMigrateDryRun(tables...)

Limitations

The Spanner gorm dialect has the following known limitations:

Limitation Workaround
Nested transactions Nested transactions and savepoints are not supported. It is therefore recommended to set the configuration option DisableNestedTransaction: true,
gorm.Automigrate with interleaved tables Interleaved tables are supported by the Spanner gorm dialect, but Auto-Migration does not support interleaved tables. It is therefore recommended to create interleaved tables manually.
Spanner stale reads Stale reads are not supported by gorm.

For the complete list of the limitations, see the Spanner GORM limitations.

Nested Transactions

gorm uses savepoints for nested transactions. Savepoints are currently not supported by Cloud Spanner. Nested transactions can therefore not be used with GORM.

Authorization

By default, each API will use Google Application Default Credentials for authorization credentials used in calling the API endpoints. This will allow your application to run in many environments without requiring explicit configuration.

Contributing

Contributions are welcome. Please, see the CONTRIBUTING document for details.

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See Contributor Code of Conduct for more information.

Documentation

Index

Constants

View Source
const (
	DefaultSequenceKind          = "BIT_REVERSED_POSITIVE"
	AutoIncrementIdentityColumns = "AUTO_INCREMENT"
	DisableIdentityColumns       = "DISABLED"
)

Variables

This section is empty.

Functions

func BeforeUpdate

func BeforeUpdate(db *gorm.DB)

func New

func New(config Config) gorm.Dialector

func Open

func Open(dsn string) gorm.Dialector

func RunTransaction added in v1.5.0

func RunTransaction(ctx context.Context, db *gorm.DB, fc func(tx *gorm.DB) error, opts ...*sql.TxOptions) error

RunTransaction executes a transaction on Spanner using the given gorm database, and retries the transaction if it is aborted by Spanner.

Types

type BoolArray added in v1.6.0

type BoolArray []bool

BoolArray is a named type for storing bool arrays in Spanner. This type cannot contain any NULL elements. We must use a named type for this to implement the driver.Valuer interface. This is required, because gorm otherwise translates arrays/slices to literals in the form `(item1, item2, ..., itemN)`.

func (BoolArray) GormDBDataType added in v1.6.0

func (a BoolArray) GormDBDataType(_ *gorm.DB, _ *schema.Field) string

func (BoolArray) GormDataType added in v1.6.0

func (a BoolArray) GormDataType() string

func (*BoolArray) Scan added in v1.6.0

func (a *BoolArray) Scan(v any) error

func (BoolArray) Value added in v1.6.0

func (a BoolArray) Value() (driver.Value, error)

type BytesArray added in v1.6.0

type BytesArray [][]byte

BytesArray is a named type for storing bytes arrays in Spanner. We must use a named type for this to implement the driver.Valuer interface. This is required, because gorm otherwise translates arrays/slices to literals in the form `(item1, item2, ..., itemN)`.

func (BytesArray) GormDBDataType added in v1.6.0

func (a BytesArray) GormDBDataType(_ *gorm.DB, _ *schema.Field) string

func (BytesArray) GormDataType added in v1.6.0

func (a BytesArray) GormDataType() string

func (*BytesArray) Scan added in v1.6.0

func (a *BytesArray) Scan(v any) error

func (BytesArray) Value added in v1.6.0

func (a BytesArray) Value() (driver.Value, error)

type Column

type Column struct {
	// contains filtered or unexported fields
}

func (Column) DatabaseTypeName

func (c Column) DatabaseTypeName() string

func (Column) DecimalSize

func (c Column) DecimalSize() (int64, int64, bool)

DecimalSize return precision int64, scale int64, ok bool

func (Column) Length

func (c Column) Length() (int64, bool)

func (Column) Name

func (c Column) Name() string

func (Column) Nullable

func (c Column) Nullable() (bool, bool)

type CommitTimestamp

type CommitTimestamp struct {
	Timestamp sql.NullTime
}

CommitTimestamp can be used for columns that should write the PENDING_COMMIT_TIMESTAMP(). Use it as the type for a field in a model. The corresponding database column must be of type TIMESTAMP, and the option `allow_commit_timestamp=true` must have been set. The Spanner gorm migrator will automatically create a TIMESTAMP column with the `allow_commit_timestamp=true` option enabled for any field that has type CommitTimestamp.

Note that the commit timestamp is not returned directly after inserting/updating a row. Instead, the value can only be read after the transaction has been committed.

Example:

type Singer struct {
  ID          int64
  Name        string
  LastUpdated CommitTimestamp
}

func (CommitTimestamp) GormDataType

func (ct CommitTimestamp) GormDataType() string

GormDataType implements gorm.GormDataTypeInterface.

func (CommitTimestamp) GormValue

func (ct CommitTimestamp) GormValue(ctx context.Context, db *gorm.DB) clause.Expr

GormValue implements the gorm.Valuer interface.

func (*CommitTimestamp) Scan

func (ct *CommitTimestamp) Scan(v interface{}) error

Scan implements the sql.Scanner interface

type Config

type Config struct {
	DriverName string

	// DSN is the Data Source Name that should be used to open a database connection.
	// Only set one of DSN, Connector, and Conn.
	DSN string

	// Connector is the driver.Connector that should be used to open a database connection.
	// Create a driver.Connector for Spanner by calling spannerdriver.CreateConnector.
	// A connector should be created only once and used to create all database connections.
	// Only set one of DSN, Connector, and Conn.
	Connector driver.Connector

	// Conn is a pre-created gorm connection pool.
	// Only set one of DSN, Connector, and Conn.
	Conn gorm.ConnPool

	// DisableAutoMigrateBatching turns off DDL batching for AutoMigrate calls.
	// Cloud Spanner by default uses DDL batching when AutoMigrate is called, as
	// executing multiple DDL statements in a single batch is a lot more efficient
	// than executing each statement separately. You should only use this option
	// if you are experiencing problems with the automatic batching of DDL
	// statements when calling AutoMigrate.
	DisableAutoMigrateBatching bool

	// DefaultSequenceKind is the value that will be used for auto-generated
	// primary keys. This configuration option defaults to 'bit_reversed_positive'
	// if no value has been set.
	//
	// Set this configuration option to AUTO_INCREMENT to use the AUTO_INCREMENT
	// keyword. The auto-generated value will then use the default_sequence_kind
	// that is configured in the database. This returns an error if the database
	// does not have a default_sequence_kind.
	//
	// Set this configuration option to DISABLED to fall back to using sequences
	// for auto-increment primary keys.
	DefaultSequenceKind string
}

type DateArray added in v1.6.0

type DateArray []civil.Date

DateArray is a named type for storing date arrays in Spanner. This type cannot contain any NULL elements. We must use a named type for this to implement the driver.Valuer interface. This is required, because gorm otherwise translates arrays/slices to literals in the form `(item1, item2, ..., itemN)`.

func (DateArray) GormDBDataType added in v1.6.0

func (a DateArray) GormDBDataType(_ *gorm.DB, _ *schema.Field) string

func (DateArray) GormDataType added in v1.6.0

func (a DateArray) GormDataType() string

func (*DateArray) Scan added in v1.6.0

func (a *DateArray) Scan(v any) error

func (DateArray) Value added in v1.6.0

func (a DateArray) Value() (driver.Value, error)

type Dialector

type Dialector struct {
	*Config
}

func (Dialector) BindVarTo

func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{})

func (Dialector) DataTypeOf

func (dialector Dialector) DataTypeOf(field *schema.Field) string

func (Dialector) DefaultValueOf

func (dialector Dialector) DefaultValueOf(field *schema.Field) clause.Expression

func (Dialector) Explain

func (dialector Dialector) Explain(sql string, vars ...interface{}) string

func (Dialector) Initialize

func (dialector Dialector) Initialize(db *gorm.DB) (err error)

func (Dialector) Migrator

func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator

func (Dialector) Name

func (dialector Dialector) Name() string

func (Dialector) QuoteTo

func (dialector Dialector) QuoteTo(writer clause.Writer, str string)

type Exprs

type Exprs []clause.Expression

func (Exprs) Build

func (exprs Exprs) Build(builder clause.Builder)

type Float32Array added in v1.6.0

type Float32Array []float32

Float32Array is a named type for storing float32 arrays in Spanner. This type cannot contain any NULL elements. We must use a named type for this to implement the driver.Valuer interface. This is required, because gorm otherwise translates arrays/slices to literals in the form `(item1, item2, ..., itemN)`.

func (Float32Array) GormDBDataType added in v1.6.0

func (a Float32Array) GormDBDataType(_ *gorm.DB, _ *schema.Field) string

func (Float32Array) GormDataType added in v1.6.0

func (a Float32Array) GormDataType() string

func (*Float32Array) Scan added in v1.6.0

func (a *Float32Array) Scan(v any) error

func (Float32Array) Value added in v1.6.0

func (a Float32Array) Value() (driver.Value, error)

type Float64Array added in v1.6.0

type Float64Array []float64

Float64Array is a named type for storing float64 arrays in Spanner. This type cannot contain any NULL elements. We must use a named type for this to implement the driver.Valuer interface. This is required, because gorm otherwise translates arrays/slices to literals in the form `(item1, item2, ..., itemN)`.

func (Float64Array) GormDBDataType added in v1.6.0

func (a Float64Array) GormDBDataType(_ *gorm.DB, _ *schema.Field) string

func (Float64Array) GormDataType added in v1.6.0

func (a Float64Array) GormDataType() string

func (*Float64Array) Scan added in v1.6.0

func (a *Float64Array) Scan(v any) error

func (Float64Array) Value added in v1.6.0

func (a Float64Array) Value() (driver.Value, error)

type Index added in v1.3.0

type Index struct {
	TableName    string
	ColumnName   string
	IndexName    string
	IsUnique     sql.NullBool
	IsPrimaryKey sql.NullBool
}

type IndexHint

type IndexHint struct {
	Type string
	Key  string
}

func ForceIndex

func ForceIndex(name string) IndexHint

func (IndexHint) Build

func (indexHint IndexHint) Build(builder clause.Builder)

func (IndexHint) ModifyStatement

func (indexHint IndexHint) ModifyStatement(stmt *gorm.Statement)

type Int64Array added in v1.6.0

type Int64Array []int64

Int64Array is a named type for storing int64 arrays in Spanner. This type cannot contain any NULL elements. We must use a named type for this to implement the driver.Valuer interface. This is required, because gorm otherwise translates arrays/slices to literals in the form `(item1, item2, ..., itemN)`.

func (Int64Array) GormDBDataType added in v1.6.0

func (a Int64Array) GormDBDataType(_ *gorm.DB, _ *schema.Field) string

func (Int64Array) GormDataType added in v1.6.0

func (a Int64Array) GormDataType() string

func (*Int64Array) Scan added in v1.6.0

func (a *Int64Array) Scan(v any) error

func (Int64Array) Value added in v1.6.0

func (a Int64Array) Value() (driver.Value, error)

type NullBoolArray added in v1.6.0

type NullBoolArray []spanner.NullBool

NullBoolArray is a named type for storing bool arrays in Spanner. We must use a named type for this to implement the driver.Valuer interface. This is required, because gorm otherwise translates arrays/slices to literals in the form `(item1, item2, ..., itemN)`. ARRAY<BOOL> is by default mapped to []spanner.NullBool in the Spanner database/sql driver. This is because Spanner always allows arrays to contain null elements, even if the column itself is defined as NOT NULL.

func (NullBoolArray) GormDBDataType added in v1.6.0

func (a NullBoolArray) GormDBDataType(_ *gorm.DB, _ *schema.Field) string

func (NullBoolArray) GormDataType added in v1.6.0

func (a NullBoolArray) GormDataType() string

func (NullBoolArray) Value added in v1.6.0

func (a NullBoolArray) Value() (driver.Value, error)

type NullBytesArray added in v1.6.0

type NullBytesArray BytesArray

NullBytesArray is a synonym for BytesArray. It is only defined for consistency with the other array data types.

func (NullBytesArray) GormDBDataType added in v1.6.0

func (a NullBytesArray) GormDBDataType(db *gorm.DB, field *schema.Field) string

func (NullBytesArray) GormDataType added in v1.6.0

func (a NullBytesArray) GormDataType() string

func (*NullBytesArray) Scan added in v1.6.0

func (a *NullBytesArray) Scan(v any) error

func (NullBytesArray) Value added in v1.6.0

func (a NullBytesArray) Value() (driver.Value, error)

type NullDateArray added in v1.6.0

type NullDateArray []spanner.NullDate

NullDateArray is a named type for storing date arrays in Spanner. We must use a named type for this to implement the driver.Valuer interface. This is required, because gorm otherwise translates arrays/slices to literals in the form `(item1, item2, ..., itemN)`. ARRAY<DATE> is by default mapped to []spanner.NullDate in the Spanner database/sql driver. This is because Spanner always allows arrays to contain null elements, even if the column itself is defined as NOT NULL.

func (NullDateArray) GormDBDataType added in v1.6.0

func (a NullDateArray) GormDBDataType(_ *gorm.DB, _ *schema.Field) string

func (NullDateArray) GormDataType added in v1.6.0

func (a NullDateArray) GormDataType() string

func (NullDateArray) Value added in v1.6.0

func (a NullDateArray) Value() (driver.Value, error)

type NullFloat32Array added in v1.6.0

type NullFloat32Array []spanner.NullFloat32

NullFloat32Array is a named type for storing float32 arrays in Spanner. We must use a named type for this to implement the driver.Valuer interface. This is required, because gorm otherwise translates arrays/slices to literals in the form `(item1, item2, ..., itemN)`. ARRAY<FLOAT32> is by default mapped to []spanner.NullFloat32 in the Spanner database/sql driver. This is because Spanner always allows arrays to contain null elements, even if the column itself is defined as NOT NULL.

func (NullFloat32Array) GormDBDataType added in v1.6.0

func (a NullFloat32Array) GormDBDataType(_ *gorm.DB, _ *schema.Field) string

func (NullFloat32Array) GormDataType added in v1.6.0

func (a NullFloat32Array) GormDataType() string

func (NullFloat32Array) Value added in v1.6.0

func (a NullFloat32Array) Value() (driver.Value, error)

type NullFloat64Array added in v1.6.0

type NullFloat64Array []spanner.NullFloat64

NullFloat64Array is a named type for storing float64 arrays in Spanner. We must use a named type for this to implement the driver.Valuer interface. This is required, because gorm otherwise translates arrays/slices to literals in the form `(item1, item2, ..., itemN)`. ARRAY<FLOAT64> is by default mapped to []spanner.NullFloat64 in the Spanner database/sql driver. This is because Spanner always allows arrays to contain null elements, even if the column itself is defined as NOT NULL.

func (NullFloat64Array) GormDBDataType added in v1.6.0

func (a NullFloat64Array) GormDBDataType(_ *gorm.DB, _ *schema.Field) string

func (NullFloat64Array) GormDataType added in v1.6.0

func (a NullFloat64Array) GormDataType() string

func (NullFloat64Array) Value added in v1.6.0

func (a NullFloat64Array) Value() (driver.Value, error)

type NullInt64Array added in v1.6.0

type NullInt64Array []spanner.NullInt64

NullInt64Array is a named type for storing int64 arrays in Spanner. We must use a named type for this to implement the driver.Valuer interface. This is required, because gorm otherwise translates arrays/slices to literals in the form `(item1, item2, ..., itemN)`. ARRAY<INT64> is by default mapped to []spanner.NullInt64 in the Spanner database/sql driver. This is because Spanner always allows arrays to contain null elements, even if the column itself is defined as NOT NULL.

func (NullInt64Array) GormDBDataType added in v1.6.0

func (a NullInt64Array) GormDBDataType(_ *gorm.DB, _ *schema.Field) string

func (NullInt64Array) GormDataType added in v1.6.0

func (a NullInt64Array) GormDataType() string

func (NullInt64Array) Value added in v1.6.0

func (a NullInt64Array) Value() (driver.Value, error)

type NullJSONArray added in v1.6.0

type NullJSONArray []spanner.NullJSON

NullJSONArray is a named type for storing JSON arrays in Spanner. We must use a named type for this to implement the driver.Valuer interface. This is required, because gorm otherwise translates arrays/slices to literals in the form `(item1, item2, ..., itemN)`. ARRAY<JSON> is by default mapped to []spanner.NullJSON in the Spanner database/sql driver. This is because Spanner always allows arrays to contain null elements, even if the column itself is defined as NOT NULL.

func (NullJSONArray) GormDBDataType added in v1.6.0

func (a NullJSONArray) GormDBDataType(_ *gorm.DB, _ *schema.Field) string

func (NullJSONArray) GormDataType added in v1.6.0

func (a NullJSONArray) GormDataType() string

func (NullJSONArray) Value added in v1.6.0

func (a NullJSONArray) Value() (driver.Value, error)

type NullStringArray added in v1.6.0

type NullStringArray []spanner.NullString

NullStringArray is a named type for storing string arrays in Spanner. We must use a named type for this to implement the driver.Valuer interface. This is required, because gorm otherwise translates arrays/slices to literals in the form `(item1, item2, ..., itemN)`. ARRAY<STRING> is by default mapped to []spanner.NullString in the Spanner database/sql driver. This is because Spanner always allows arrays to contain null elements, even if the column itself is defined as NOT NULL.

func (NullStringArray) GormDBDataType added in v1.6.0

func (a NullStringArray) GormDBDataType(_ *gorm.DB, field *schema.Field) string

func (NullStringArray) GormDataType added in v1.6.0

func (a NullStringArray) GormDataType() string

func (NullStringArray) Value added in v1.6.0

func (a NullStringArray) Value() (driver.Value, error)

type NullTimeArray added in v1.6.0

type NullTimeArray []spanner.NullTime

NullTimeArray is a named type for storing timestamp arrays in Spanner. We must use a named type for this to implement the driver.Valuer interface. This is required, because gorm otherwise translates arrays/slices to literals in the form `(item1, item2, ..., itemN)`. ARRAY<TIMESTAMP> is by default mapped to []spanner.NullTime in the Spanner database/sql driver. This is because Spanner always allows arrays to contain null elements, even if the column itself is defined as NOT NULL.

func (NullTimeArray) GormDBDataType added in v1.6.0

func (a NullTimeArray) GormDBDataType(_ *gorm.DB, _ *schema.Field) string

func (NullTimeArray) GormDataType added in v1.6.0

func (a NullTimeArray) GormDataType() string

func (NullTimeArray) Value added in v1.6.0

func (a NullTimeArray) Value() (driver.Value, error)

type SpannerMigrator

type SpannerMigrator interface {
	gorm.Migrator

	AutoMigrateDryRun(values ...interface{}) ([]spanner.Statement, error)
	StartBatchDDL() error
	RunBatch() error
	AbortBatch() error
}

type StringArray added in v1.6.0

type StringArray []string

StringArray is a named type for storing string arrays in Spanner. This type cannot contain any NULL elements. We must use a named type for this to implement the driver.Valuer interface. This is required, because gorm otherwise translates arrays/slices to literals in the form `(item1, item2, ..., itemN)`.

func (StringArray) GormDBDataType added in v1.6.0

func (a StringArray) GormDBDataType(_ *gorm.DB, field *schema.Field) string

func (StringArray) GormDataType added in v1.6.0

func (a StringArray) GormDataType() string

func (*StringArray) Scan added in v1.6.0

func (a *StringArray) Scan(v any) error

func (StringArray) Value added in v1.6.0

func (a StringArray) Value() (driver.Value, error)

type TimeArray added in v1.6.0

type TimeArray []time.Time

TimeArray is a named type for storing date arrays in Spanner. This type cannot contain any NULL elements. We must use a named type for this to implement the driver.Valuer interface. This is required, because gorm otherwise translates arrays/slices to literals in the form `(item1, item2, ..., itemN)`.

func (TimeArray) GormDBDataType added in v1.6.0

func (a TimeArray) GormDBDataType(_ *gorm.DB, _ *schema.Field) string

func (TimeArray) GormDataType added in v1.6.0

func (a TimeArray) GormDataType() string

func (*TimeArray) Scan added in v1.6.0

func (a *TimeArray) Scan(v any) error

func (TimeArray) Value added in v1.6.0

func (a TimeArray) Value() (driver.Value, error)

Directories

Path Synopsis
samples module

Jump to

Keyboard shortcuts

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