Documentation ΒΆ
Overview ΒΆ
Package spnr provides the orm for Cloud Spanner.
Index ΒΆ
- Variables
- func NewNullBool(b bool) spanner.NullBool
- func NewNullDate(d civil.Date) spanner.NullDate
- func NewNullInt64(val int64) spanner.NullInt64
- func NewNullNumeric(a, b int64) spanner.NullNumeric
- func NewNullString(str string) spanner.NullString
- func NewNullTime(t time.Time) spanner.NullTime
- func ToAllColumnNames(target interface{}) string
- func ToKeySets(target interface{}) spanner.KeySet
- type DML
- func (d *DML) Delete(ctx context.Context, tx *spanner.ReadWriteTransaction, target interface{}) (rowCount int64, err error)
- func (d *DML) GetTableName() string
- func (d *DML) Insert(ctx context.Context, tx *spanner.ReadWriteTransaction, target interface{}) (rowCount int64, err error)
- func (d *DML) Reader(ctx context.Context, tx Transaction) *Reader
- func (d *DML) Update(ctx context.Context, tx *spanner.ReadWriteTransaction, target interface{}) (rowCount int64, err error)
- func (d *DML) UpdateColumns(ctx context.Context, tx *spanner.ReadWriteTransaction, columns []string, ...) (rowCount int64, err error)
- type Mutation
- func (m *Mutation) ApplyDelete(ctx context.Context, client *spanner.Client, target interface{}) (time.Time, error)
- func (m *Mutation) ApplyInsertOrUpdate(ctx context.Context, client *spanner.Client, target interface{}) (time.Time, error)
- func (m *Mutation) ApplyInsertOrUpdateColumns(ctx context.Context, client *spanner.Client, columns []string, ...) (time.Time, error)
- func (m *Mutation) ApplyUpdate(ctx context.Context, client *spanner.Client, target interface{}) (time.Time, error)
- func (m *Mutation) ApplyUpdateColumns(ctx context.Context, client *spanner.Client, columns []string, ...) (time.Time, error)
- func (m *Mutation) Delete(tx *spanner.ReadWriteTransaction, target interface{}) error
- func (m *Mutation) GetTableName() string
- func (m *Mutation) InsertOrUpdate(tx *spanner.ReadWriteTransaction, target interface{}) error
- func (m *Mutation) InsertOrUpdateColumns(tx *spanner.ReadWriteTransaction, columns []string, target interface{}) error
- func (m *Mutation) Reader(ctx context.Context, tx Transaction) *Reader
- func (m *Mutation) Update(tx *spanner.ReadWriteTransaction, target interface{}) error
- func (m *Mutation) UpdateColumns(tx *spanner.ReadWriteTransaction, columns []string, target interface{}) error
- type Options
- type Reader
- func (r *Reader) FindAll(keys spanner.KeySet, target interface{}) error
- func (r *Reader) FindOne(key spanner.Key, target interface{}) error
- func (r *Reader) GetColumn(key spanner.Key, column string, target interface{}) error
- func (r *Reader) GetColumnAll(keys spanner.KeySet, column string, target interface{}) error
- func (r *Reader) Query(sql string, params map[string]interface{}, target interface{}) error
- func (r *Reader) QueryOne(sql string, params map[string]interface{}, target interface{}) error
- func (r *Reader) QueryValue(sql string, params map[string]interface{}, target interface{}) error
- func (r *Reader) QueryValues(sql string, params map[string]interface{}, target interface{}) error
- type Transaction
Constants ΒΆ
This section is empty.
Variables ΒΆ
var ( // ErrNotFound is returned when a read operation cannot find any records unexpectedly. ErrNotFound = errors.New("record not found") // ErrNotFound is returned when a read operation found multiple records unexpectedly. ErrMoreThanOneRecordFound = errors.New("more than one record found") )
Functions ΒΆ
func NewNullBool ΒΆ
NewNullBool initializes spanner.NullBool setting Valid as true
func NewNullDate ΒΆ
NewNullDate initializes spanner.NullDate setting Valid as true
func NewNullInt64 ΒΆ
NewNullInt64 initializes spanner.NullInt64 setting Valid as true
func NewNullNumeric ΒΆ
func NewNullNumeric(a, b int64) spanner.NullNumeric
NewNullNumeric initializes spanner.NullNumeric setting Valid as true
func NewNullString ΒΆ
func NewNullString(str string) spanner.NullString
NewNullString initializes spanner.NullString setting Valid as true
func NewNullTime ΒΆ
NewNullTime initializes spanner.NullTime setting Valid as true
func ToAllColumnNames ΒΆ
func ToAllColumnNames(target interface{}) string
ToAllColumnNames receives struct and returns the fields that the passed struct has. This method is useful when you build query to select all the fields. Instead of use *(wildcard), you can specify all of the columns using this method. Then you can avoid the risk that failing to map record to struct caused by the mismatch of an order of columns in spanner table and fields in struct.
Types ΒΆ
type DML ΒΆ
type DML struct {
// contains filtered or unexported fields
}
DML offers ORM with DML. It also contains read operations (call Reader method.)
func NewDML ΒΆ
NewDML initializes ORM with DML. It also contains read operations (call Reader method of DML.) If you want to use Mutation API, use New() or NewMutation() instead.
func NewDMLWithOptions ΒΆ
NewDMLWithOptions initializes DML with options. Check Options for the available options.
func (*DML) Delete ΒΆ
func (d *DML) Delete(ctx context.Context, tx *spanner.ReadWriteTransaction, target interface{}) (rowCount int64, err error)
Delete build and execute delete statement from the passed struct. You can pass either a struct or a slice of structs to target. If you pass a slice of structs, this method will build statement which deletes multiple records in one statement like the following.
DELETE FROM `T` WHERE (`COL1` = 'a' AND `COL2` = 'b') OR (`COL1` = 'c' AND `COL2` = 'd');
func (*DML) Insert ΒΆ
func (d *DML) Insert(ctx context.Context, tx *spanner.ReadWriteTransaction, target interface{}) (rowCount int64, err error)
Insert build and execute insert statement from the passed struct. You can pass either a struct or a slice of struct to target. If you pass a slice of struct, this method will build a statement which insert multiple records in one statement like the following
INSERT INTO `TableName` (`Column1`, `Column2`) VALUES ('a', 'b'), ('c', 'd'), ...;
func (*DML) Reader ΒΆ
func (d *DML) Reader(ctx context.Context, tx Transaction) *Reader
Reader returns Reader struct to call read operations.
func (*DML) Update ΒΆ
func (d *DML) Update(ctx context.Context, tx *spanner.ReadWriteTransaction, target interface{}) (rowCount int64, err error)
Update build and execute update statement from the passed struct. You can pass either a struct or slice of struct to target. If you pass a slice of struct, this method will call update statement in for loop.
func (*DML) UpdateColumns ΒΆ
func (d *DML) UpdateColumns(ctx context.Context, tx *spanner.ReadWriteTransaction, columns []string, target interface{}) (rowCount int64, err error)
UpdateColumns build and execute update statement from the passed column names and struct. You can specify the columns to update. Also, you can pass either a struct or slice of struct to target. If you pass a slice of struct, this method will call update statement in for loop.
type Mutation ΒΆ
type Mutation struct {
// contains filtered or unexported fields
}
DML offers ORM with Mutation API. It also contains read operations (call Reader method.)
func NewMutation ΒΆ
NewMutation initializes ORM with Mutation API. It also contains read operations (call Reader method of Mutation.) If you want to use DML, use NewDML() instead.
func NewMutationWithOptions ΒΆ
NewDMLWithOptions initializes Mutation with options. Check Options for the available options.
func (*Mutation) ApplyDelete ΒΆ
func (m *Mutation) ApplyDelete(ctx context.Context, client *spanner.Client, target interface{}) (time.Time, error)
ApplyDelete is basically same as Delete, but it doesn't require transaction. This method directly calls mutation API without transaction by calling spanner.Client.Apply method.
func (*Mutation) ApplyInsertOrUpdate ΒΆ
func (m *Mutation) ApplyInsertOrUpdate(ctx context.Context, client *spanner.Client, target interface{}) (time.Time, error)
ApplyInsertOrUpdate is basically same as InsertOrUpdate, but it doesn't require transaction. This method directly calls mutation API without transaction by calling spanner.Client.Apply method. If you want to insert or update only the specified columns, use ApplyInsertOrUpdateColumns instead.
func (*Mutation) ApplyInsertOrUpdateColumns ΒΆ
func (m *Mutation) ApplyInsertOrUpdateColumns(ctx context.Context, client *spanner.Client, columns []string, target interface{}) (time.Time, error)
ApplyInsertOrUpdateColumns is basically same as InsertOrUpdateColumns, but it doesn't require transaction. This method directly calls mutation API without transaction by calling spanner.Client.Apply method.
func (*Mutation) ApplyUpdate ΒΆ
func (m *Mutation) ApplyUpdate(ctx context.Context, client *spanner.Client, target interface{}) (time.Time, error)
ApplyUpdate is basically same as Update, but it doesn't require transaction. This method directly calls mutation API without transaction by calling spanner.Client.Apply method. If you want to update only the specified columns, use ApplyUpdateColumns instead.
func (*Mutation) ApplyUpdateColumns ΒΆ
func (m *Mutation) ApplyUpdateColumns(ctx context.Context, client *spanner.Client, columns []string, target interface{}) (time.Time, error)
ApplyUpdateColumns is basically same as UpdateColumns, but it doesn't require transaction. This method directly calls mutation API without transaction by calling spanner.Client.Apply method.
func (*Mutation) Delete ΒΆ
func (m *Mutation) Delete(tx *spanner.ReadWriteTransaction, target interface{}) error
Delete build and execute delete operation using mutation API. You can pass either a struct or a slice of structs. If you pass a slice of structs, this method will build a mutation for each struct. This method requires spanner.ReadWriteTransaction, and will call spanner.ReadWriteTransaction.BufferWrite to save the mutation to transaction.
func (*Mutation) GetTableName ΒΆ
GetTableName returns table name
func (*Mutation) InsertOrUpdate ΒΆ
func (m *Mutation) InsertOrUpdate(tx *spanner.ReadWriteTransaction, target interface{}) error
InsertOrUpdate build and execute insert_or_update operation using mutation API. You can pass either a struct or a slice of structs. If you pass a slice of structs, this method will call multiple mutations for each struct. This method requires spanner.ReadWriteTransaction, and will call spanner.ReadWriteTransaction.BufferWrite to save the mutation to transaction. If you want to insert or update only the specified columns, use InsertOrUpdateColumns instead.
func (*Mutation) InsertOrUpdateColumns ΒΆ
func (m *Mutation) InsertOrUpdateColumns(tx *spanner.ReadWriteTransaction, columns []string, target interface{}) error
InsertOrUpdateColumns build and execute insert_or_update operation for specified columns using mutation API. You can pass either a struct or a slice of structs to target. If you pass a slice of structs, this method will build a mutation for each struct. This method requires spanner.ReadWriteTransaction, and will call spanner.ReadWriteTransaction.BufferWrite to save the mutation to transaction.
func (*Mutation) Reader ΒΆ
func (m *Mutation) Reader(ctx context.Context, tx Transaction) *Reader
Reader returns Reader struct to call read operations.
func (*Mutation) Update ΒΆ
func (m *Mutation) Update(tx *spanner.ReadWriteTransaction, target interface{}) error
Update build and execute update operation using mutation API. You can pass either a struct or a slice of structs. If you pass a slice of structs, this method will call multiple mutations for each struct. This method requires spanner.ReadWriteTransaction, and will call spanner.ReadWriteTransaction.BufferWrite to save the mutation to transaction. If you want to update only the specified columns, use UpdateColumns instead.
func (*Mutation) UpdateColumns ΒΆ
func (m *Mutation) UpdateColumns(tx *spanner.ReadWriteTransaction, columns []string, target interface{}) error
UpdateColumns build and execute update operation for specified columns using mutation API. You can pass either a struct or a slice of structs to target. If you pass a slice of structs, this method will build a mutation for each struct. This method requires spanner.ReadWriteTransaction, and will call spanner.ReadWriteTransaction.BufferWrite to save the mutation to transaction.
type Options ΒΆ
type Options struct { Logger logger LogEnabled bool }
Options is for specifying the options for spnr.Mutation and spnr.DML.
type Reader ΒΆ
type Reader struct {
// contains filtered or unexported fields
}
Reader executes read operations.
func (*Reader) FindAll ΒΆ
FindAll fetches records by specified a set of primary keys, and map the records into the passed pointer of slice of structs.
func (*Reader) FindOne ΒΆ
FindOne fetches a record by specified primary key, and map the record into the passed pointer of struct.
func (*Reader) GetColumn ΒΆ
GetColumn fetches the specified column by specified primary key, and map the column into the passed pointer of value.
Caution:
It maps fetched column to the passed pointer by just calling spanner.Row.Columns method. So the type of passed value to map should be compatible to this method. For example if you fetch an INT64 column from spanner, you need to map this value to int64, not int.
func (*Reader) GetColumnAll ΒΆ
GetColumn fetches the specified column for the records that matches specified set of primary keys, and map the column into the passed pointer of a slice of values. Please see the caution commented in GetColumn to check type compatibility.
func (*Reader) Query ΒΆ
Query fetches records by calling specified query, and map the records into the passed pointer of a slice of struct.
func (*Reader) QueryOne ΒΆ
QueryOne fetches a record by calling specified query, and map the record into the passed pointer of struct.
Errors:
If no records are found, this method will return ErrNotFound. If multiple records are found, this method will return ErrMoreThanOneRecordFound.
If you don't need to fetch all columns but only needs one column, use QueryValue instead. If you don't need to fetch all columns but only needs some columns, please make a temporal struct to map the columns.
func (*Reader) QueryValue ΒΆ
QueryValue fetches one value by calling specified query, and map the value into the passed pointer of value.
Errors:
If no records are found, this method will return ErrNotFound. If multiple records are found, this method will return ErrMoreThanOneRecordFound.
Example:
var cnt int64 QueryValue("select count(*) as cnt from Singers", nil, &cnt)
func (*Reader) QueryValues ΒΆ
QueryValues fetches each value of multiple records by calling specified query, and map the values into the passed pointer of a slice of struct.
Example:
var names []string QueryValue("select Name from Singers", nil, &names)
type Transaction ΒΆ
type Transaction interface { Read(ctx context.Context, table string, keys spanner.KeySet, columns []string) *spanner.RowIterator ReadRow(ctx context.Context, table string, key spanner.Key, columns []string) (*spanner.Row, error) Query(ctx context.Context, statement spanner.Statement) *spanner.RowIterator }
Transaction is the interface for spanner.ReadOnlyTransaction and spanner.ReadWriteTransaction