Browse Source

Merge remote-tracking branch 'origin/master'

Dmitriy Gnatenko 3 months ago
parent
commit
c78f9df181
2 changed files with 7 additions and 23 deletions
  1. 3 17
      db/db.go
  2. 4 6
      db/tx.go

+ 3 - 17
db/db.go

@@ -94,12 +94,7 @@ func (s *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sqlx.Tx, error)
 	return s.db.BeginTxx(ctx, opts)
 }
 
-func (s *DB) SelectContext(
-	ctx context.Context,
-	dest interface{},
-	query string,
-	args ...interface{},
-) error {
+func (s *DB) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
 	tx, ok := ctx.Value(TxKey{}).(*sqlx.Tx)
 	if ok {
 		return tx.SelectContext(ctx, dest, query, args...)
@@ -108,12 +103,7 @@ func (s *DB) SelectContext(
 	return s.db.SelectContext(ctx, dest, query, args...)
 }
 
-func (s *DB) GetContext(
-	ctx context.Context,
-	dest interface{},
-	query string,
-	args ...interface{},
-) error {
+func (s *DB) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error {
 	tx, ok := ctx.Value(TxKey{}).(*sqlx.Tx)
 	if ok {
 		return tx.GetContext(ctx, dest, query, args...)
@@ -122,11 +112,7 @@ func (s *DB) GetContext(
 	return s.db.GetContext(ctx, dest, query, args...)
 }
 
-func (s *DB) ExecContext(
-	ctx context.Context,
-	query string,
-	args ...interface{},
-) (sql.Result, error) {
+func (s *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
 	tx, ok := ctx.Value(TxKey{}).(*sqlx.Tx)
 	if ok {
 		return tx.ExecContext(ctx, query, args...)

+ 4 - 6
db/tx.go

@@ -12,8 +12,6 @@ type TxDB interface {
 	BeginTx(ctx context.Context, opts *sql.TxOptions) (*sqlx.Tx, error)
 }
 
-type Handler func(ctx context.Context) error
-
 type TxManager struct {
 	db TxDB
 }
@@ -24,7 +22,7 @@ func NewTransactionManager(db TxDB) *TxManager {
 	}
 }
 
-func (s *TxManager) transaction(ctx context.Context, opts sql.TxOptions, fn Handler) (err error) {
+func (s *TxManager) transaction(ctx context.Context, opts sql.TxOptions, fn func(ctx context.Context) error) (err error) {
 	tx, ok := ctx.Value(TxKey{}).(*sqlx.Tx)
 	if ok {
 		return fn(ctx)
@@ -65,17 +63,17 @@ func (s *TxManager) transaction(ctx context.Context, opts sql.TxOptions, fn Hand
 	return err
 }
 
-func (s *TxManager) ReadCommitted(ctx context.Context, f Handler) error {
+func (s *TxManager) ReadCommitted(ctx context.Context, f func(ctx context.Context) error) error {
 	txOpts := sql.TxOptions{Isolation: sql.LevelReadCommitted}
 	return s.transaction(ctx, txOpts, f)
 }
 
-func (s *TxManager) RepeatableRead(ctx context.Context, f Handler) error {
+func (s *TxManager) RepeatableRead(ctx context.Context, f func(ctx context.Context) error) error {
 	txOpts := sql.TxOptions{Isolation: sql.LevelRepeatableRead}
 	return s.transaction(ctx, txOpts, f)
 }
 
-func (s *TxManager) Serializable(ctx context.Context, numAttempts int, f Handler) error {
+func (s *TxManager) Serializable(ctx context.Context, numAttempts int, f func(ctx context.Context) error) error {
 	txOpts := sql.TxOptions{Isolation: sql.LevelSerializable}
 
 	for i := 0; i < numAttempts; i++ {