/
mutation_update.go
102 lines (95 loc) · 3.98 KB
/
mutation_update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package spnr
import (
"cloud.google.com/go/spanner"
"context"
"strings"
"time"
)
// 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 (m *Mutation) Update(tx *spanner.ReadWriteTransaction, target interface{}) error {
isStruct, err := validateStructOrStructSliceType(target)
if err != nil {
return err
}
if isStruct {
return withStack(tx.BufferWrite(m.buildUpdate([]interface{}{target})))
}
return withStack(tx.BufferWrite(m.buildUpdate(toStructSlice(target))))
}
// 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 (m *Mutation) ApplyUpdate(ctx context.Context, client *spanner.Client, target interface{}) (time.Time, error) {
isStruct, err := validateStructOrStructSliceType(target)
if err != nil {
return time.Time{}, err
}
if isStruct {
t, err := client.Apply(ctx, m.buildUpdate([]interface{}{target}))
return t, withStack(err)
}
t, err := client.Apply(ctx, m.buildUpdate(toStructSlice(target)))
return t, withStack(err)
}
// 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.
func (m *Mutation) UpdateColumns(tx *spanner.ReadWriteTransaction, columns []string, target interface{}) error {
isStruct, err := validateStructOrStructSliceType(target)
if err != nil {
return err
}
if isStruct {
return withStack(tx.BufferWrite(m.buildUpdateWithColumns([]interface{}{target}, columns)))
}
return withStack(tx.BufferWrite(m.buildUpdateWithColumns(toStructSlice(target), columns)))
}
// 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 (m *Mutation) ApplyUpdateColumns(ctx context.Context, client *spanner.Client, columns []string, target interface{}) (time.Time, error) {
isStruct, err := validateStructOrStructSliceType(target)
if err != nil {
return time.Time{}, err
}
if isStruct {
t, err := client.Apply(ctx, m.buildUpdateWithColumns([]interface{}{target}, columns))
return t, withStack(err)
}
t, err := client.Apply(ctx, m.buildUpdateWithColumns(toStructSlice(target), columns))
return t, withStack(err)
}
func (m *Mutation) buildUpdate(targets []interface{}) []*spanner.Mutation {
var ms []*spanner.Mutation
for _, target := range targets {
var columns []string
var values []interface{}
for _, field := range toFields(target) {
columns = append(columns, field.name)
values = append(values, field.value)
}
m.log("Update %s, columns=%+v, values=%+v", m.table, columns, values)
ms = append(ms, spanner.Update(m.table, columns, values))
}
return ms
}
func (m *Mutation) buildUpdateWithColumns(targets []interface{}, columns []string) []*spanner.Mutation {
var ms []*spanner.Mutation
for _, target := range targets {
fieldNameField := map[string]field{}
for _, f := range toFields(target) {
fieldNameField[strings.ToLower(f.name)] = f
}
var values []interface{}
for _, c := range columns {
values = append(values, fieldNameField[c])
}
m.log("Update %s, columns=%+v, values=%+v", m.table, columns, values)
ms = append(ms, spanner.Update(m.table, columns, values))
}
return ms
}