2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-12-05 09:35:50 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2020-01-15 02:29:51 +00:00
|
|
|
package dbx
|
2018-12-05 09:35:50 +00:00
|
|
|
|
2018-12-07 14:46:42 +00:00
|
|
|
import (
|
2019-03-12 13:08:23 +00:00
|
|
|
"context"
|
2019-04-23 12:13:57 +01:00
|
|
|
"fmt"
|
2019-03-12 13:08:23 +00:00
|
|
|
|
2019-11-08 20:40:39 +00:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2018-12-07 14:46:42 +00:00
|
|
|
"github.com/zeebo/errs"
|
2020-01-08 13:40:19 +00:00
|
|
|
|
2021-04-23 10:52:40 +01:00
|
|
|
"storj.io/private/dbutil/cockroachutil"
|
|
|
|
"storj.io/private/dbutil/txutil"
|
|
|
|
"storj.io/private/tagsql"
|
2018-12-07 14:46:42 +00:00
|
|
|
)
|
|
|
|
|
2020-01-17 20:07:00 +00:00
|
|
|
//go:generate sh gen.sh
|
2018-12-17 20:14:16 +00:00
|
|
|
|
2019-12-17 20:07:54 +00:00
|
|
|
var mon = monkit.Package()
|
|
|
|
|
2018-12-07 14:46:42 +00:00
|
|
|
func init() {
|
|
|
|
// catch dbx errors
|
2019-04-23 12:13:57 +01:00
|
|
|
class := errs.Class("satellitedb")
|
2018-12-17 20:14:16 +00:00
|
|
|
WrapErr = func(e *Error) error {
|
2019-04-01 21:14:58 +01:00
|
|
|
switch e.Code {
|
|
|
|
case ErrorCode_NoRows:
|
2018-12-17 20:14:16 +00:00
|
|
|
return e.Err
|
2019-04-01 21:14:58 +01:00
|
|
|
case ErrorCode_ConstraintViolation:
|
2019-04-23 12:13:57 +01:00
|
|
|
return class.Wrap(&constraintError{e.Constraint, e.Err})
|
2018-12-17 20:14:16 +00:00
|
|
|
}
|
2019-04-23 12:13:57 +01:00
|
|
|
return class.Wrap(e)
|
2018-12-17 20:14:16 +00:00
|
|
|
}
|
2020-11-29 23:03:26 +00:00
|
|
|
ShouldRetry = func(driver string, err error) bool {
|
|
|
|
if driver == "pgxcockroach" || driver == "cockroach" {
|
|
|
|
return cockroachutil.NeedsRetry(err)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2018-12-07 14:46:42 +00:00
|
|
|
}
|
2019-03-12 13:08:23 +00:00
|
|
|
|
2019-04-23 12:13:57 +01:00
|
|
|
// Unwrap returns the underlying error.
|
2019-04-23 20:48:57 +01:00
|
|
|
func (e *Error) Unwrap() error { return e.Err }
|
|
|
|
|
|
|
|
// Cause returns the underlying error.
|
|
|
|
func (e *Error) Cause() error { return e.Err }
|
2019-04-23 12:13:57 +01:00
|
|
|
|
|
|
|
type constraintError struct {
|
|
|
|
constraint string
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unwrap returns the underlying error.
|
2019-04-23 20:48:57 +01:00
|
|
|
func (err *constraintError) Unwrap() error { return err.err }
|
|
|
|
|
|
|
|
// Cause returns the underlying error.
|
|
|
|
func (err *constraintError) Cause() error { return err.err }
|
2019-04-23 12:13:57 +01:00
|
|
|
|
2020-07-01 16:25:20 +01:00
|
|
|
// IsConstraintError returns true if the error is a constraint error.
|
|
|
|
func IsConstraintError(err error) bool {
|
|
|
|
_, ok := err.(*constraintError)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2019-04-23 12:13:57 +01:00
|
|
|
// Error implements the error interface.
|
|
|
|
func (err *constraintError) Error() string {
|
|
|
|
return fmt.Sprintf("violates constraint %q: %v", err.constraint, err.err)
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// WithTx wraps DB code in a transaction.
|
2019-03-12 13:08:23 +00:00
|
|
|
func (db *DB) WithTx(ctx context.Context, fn func(context.Context, *Tx) error) (err error) {
|
2020-01-30 19:38:25 +00:00
|
|
|
return txutil.WithTx(ctx, db, nil, func(ctx context.Context, tx tagsql.Tx) error {
|
|
|
|
return fn(ctx, &Tx{
|
|
|
|
Tx: tx,
|
|
|
|
txMethods: db.wrapTx(tx),
|
|
|
|
})
|
2020-01-07 22:20:20 +00:00
|
|
|
})
|
2019-03-12 13:08:23 +00:00
|
|
|
}
|