github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/internal/datasource/transaction/single_transaction.go (about)

     1  // Copyright 2021 ecodeclub
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package transaction
    16  
    17  import (
    18  	"context"
    19  	"database/sql"
    20  	"sync"
    21  
    22  	"github.com/ecodeclub/eorm/internal/errs"
    23  
    24  	"github.com/ecodeclub/eorm/internal/datasource"
    25  )
    26  
    27  type SingleTxFactory struct{}
    28  
    29  func (SingleTxFactory) TxOf(ctx Context, finder datasource.Finder) (datasource.Tx, error) {
    30  	return NewSingleTx(ctx, finder), nil
    31  }
    32  
    33  type SingleTx struct {
    34  	DB     string
    35  	ctx    Context
    36  	lock   sync.RWMutex
    37  	tx     datasource.Tx
    38  	finder datasource.Finder
    39  }
    40  
    41  func (t *SingleTx) findTgt(ctx context.Context, query datasource.Query) (datasource.TxBeginner, error) {
    42  	return t.finder.FindTgt(ctx, query)
    43  }
    44  
    45  func (t *SingleTx) findOrBeginTx(ctx context.Context, query datasource.Query) (datasource.Tx, error) {
    46  	t.lock.RLock()
    47  	if t.DB != "" && t.tx != nil {
    48  		if t.DB != query.DB {
    49  			t.lock.RUnlock()
    50  			return nil, errs.NewErrDBNotEqual(t.DB, query.DB)
    51  		}
    52  		t.lock.RUnlock()
    53  		return t.tx, nil
    54  	}
    55  	t.lock.RUnlock()
    56  	t.lock.Lock()
    57  	defer t.lock.Unlock()
    58  	if t.DB != "" && t.tx != nil {
    59  		if t.DB != query.DB {
    60  			return nil, errs.NewErrDBNotEqual(t.DB, query.DB)
    61  		}
    62  		return t.tx, nil
    63  	}
    64  	db, err := t.findTgt(ctx, query)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	tx, err := db.BeginTx(t.ctx.TxCtx, t.ctx.Opts)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  	t.tx = tx
    73  	t.DB = query.DB
    74  	return tx, nil
    75  }
    76  
    77  func (t *SingleTx) Query(ctx context.Context, query datasource.Query) (*sql.Rows, error) {
    78  	// 防止 GetMulti 的查询重复创建多个事务
    79  	tx, err := t.findOrBeginTx(ctx, query)
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  	return tx.Query(ctx, query)
    84  }
    85  
    86  func (t *SingleTx) Exec(ctx context.Context, query datasource.Query) (sql.Result, error) {
    87  	tx, err := t.findOrBeginTx(ctx, query)
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  	return tx.Exec(ctx, query)
    92  }
    93  
    94  func (t *SingleTx) Commit() error {
    95  	if t.tx != nil {
    96  		return t.tx.Commit()
    97  	}
    98  	return nil
    99  }
   100  
   101  func (t *SingleTx) Rollback() error {
   102  	if t.tx != nil {
   103  		return t.tx.Rollback()
   104  	}
   105  	return nil
   106  }
   107  
   108  func NewSingleTx(ctx Context, finder datasource.Finder) *SingleTx {
   109  	return &SingleTx{
   110  		ctx:    ctx,
   111  		finder: finder,
   112  	}
   113  }