-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutation_update.go
104 lines (96 loc) · 4 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
103
104
package spnr
import (
"context"
"strings"
"time"
"cloud.google.com/go/spanner"
"github.com/pkg/errors"
)
// 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 any) error {
isStruct, err := validateStructOrStructSliceType(target)
if err != nil {
return err
}
if isStruct {
return errors.WithStack(tx.BufferWrite(m.buildUpdate([]any{target})))
}
return errors.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 any) (time.Time, error) {
isStruct, err := validateStructOrStructSliceType(target)
if err != nil {
return time.Time{}, err
}
if isStruct {
t, err := client.Apply(ctx, m.buildUpdate([]any{target}))
return t, errors.WithStack(err)
}
t, err := client.Apply(ctx, m.buildUpdate(toStructSlice(target)))
return t, errors.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 any) error {
isStruct, err := validateStructOrStructSliceType(target)
if err != nil {
return err
}
if isStruct {
return errors.WithStack(tx.BufferWrite(m.buildUpdateWithColumns([]any{target}, columns)))
}
return errors.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 any) (time.Time, error) {
isStruct, err := validateStructOrStructSliceType(target)
if err != nil {
return time.Time{}, err
}
if isStruct {
t, err := client.Apply(ctx, m.buildUpdateWithColumns([]any{target}, columns))
return t, errors.WithStack(err)
}
t, err := client.Apply(ctx, m.buildUpdateWithColumns(toStructSlice(target), columns))
return t, errors.WithStack(err)
}
func (m *Mutation) buildUpdate(targets []any) []*spanner.Mutation {
var ms []*spanner.Mutation
for _, target := range targets {
var columns []string
var values []any
for _, field := range toFields(target) {
columns = append(columns, field.name)
values = append(values, field.value)
}
m.logf("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 []any, columns []string) []*spanner.Mutation {
var ms []*spanner.Mutation
for _, target := range targets {
fieldNameToField := map[string]field{}
for _, f := range toFields(target) {
fieldNameToField[strings.ToLower(f.name)] = f
}
var values []any
for _, c := range columns {
values = append(values, fieldNameToField[strings.ToLower(c)].value)
}
m.logf("Update %s, columns=%+v, values=%+v", m.table, columns, values)
ms = append(ms, spanner.Update(m.table, columns, values))
}
return ms
}