-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
63 lines (53 loc) · 1.59 KB
/
reader.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
package spnr
import (
"context"
"log"
"reflect"
"cloud.google.com/go/spanner"
"github.com/googleapis/gax-go/v2/apierror"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
)
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")
)
const readLogTemplate = "executing read... %s, %+v"
// Transaction is the interface for spanner.ReadOnlyTransaction and spanner.ReadWriteTransaction
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
}
// Reader executes read operations.
type Reader struct {
table string
ctx context.Context
tx Transaction
logger logger
logEnabled bool
}
func (r *Reader) logf(format string, v ...any) {
if !r.logEnabled {
return
}
if r.logger != nil {
r.logger.Printf(format, v...)
} else {
log.Printf(format, v...)
}
}
func toColumnNames(val reflect.Type) []string {
var columns []string
for i := 0; i < val.NumField(); i++ {
columns = append(columns, val.Field(i).Name)
}
return columns
}
func isNotFound(err error) bool {
var apiErr *apierror.APIError
return errors.As(err, &apiErr) &&
apiErr.GRPCStatus().Code() == codes.NotFound
}