-
Notifications
You must be signed in to change notification settings - Fork 52
/
yo_db.go.tpl
64 lines (53 loc) · 1.92 KB
/
yo_db.go.tpl
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
// YODB is the common interface for database operations.
type YODB interface {
YORODB
}
// YORODB is the common interface for database operations.
type YORODB interface {
ReadRow(ctx context.Context, table string, key spanner.Key, columns []string) (*spanner.Row, error)
Read(ctx context.Context, table string, keys spanner.KeySet, columns []string) *spanner.RowIterator
ReadUsingIndex(ctx context.Context, table, index string, keys spanner.KeySet, columns []string) (ri *spanner.RowIterator)
Query(ctx context.Context, statement spanner.Statement) *spanner.RowIterator
}
// YOLog provides the log func used by generated queries.
var YOLog = func(context.Context, string, ...interface{}) { }
func newError(method, table string, err error) error {
code := spanner.ErrCode(err)
return newErrorWithCode(code, method, table, err)
}
func newErrorWithCode(code codes.Code, method, table string, err error) error {
return &yoError{
method: method,
table: table,
err: err,
code: code,
}
}
type yoError struct {
err error
method string
table string
code codes.Code
}
func (e yoError) Error() string {
return fmt.Sprintf("yo error in %s(%s): %v", e.method, e.table, e.err)
}
func (e yoError) Unwrap() error {
return e.err
}
func (e yoError) DBTableName() string {
return e.table
}
// GRPCStatus implements a conversion to a gRPC status using `status.Convert(error)`.
// If the error is originated from the Spanner library, this returns a gRPC status of
// the original error. It may contain details of the status such as RetryInfo.
func (e yoError) GRPCStatus() *status.Status {
var ae *apierror.APIError
if errors.As(e.err, &ae) {
return status.Convert(ae)
}
return status.New(e.code, e.Error())
}
func (e yoError) Timeout() bool { return e.code == codes.DeadlineExceeded }
func (e yoError) Temporary() bool { return e.code == codes.DeadlineExceeded }
func (e yoError) NotFound() bool { return e.code == codes.NotFound }