github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/xsql/isolation/isolation.go (about)

     1  package isolation
     2  
     3  import (
     4  	"database/sql"
     5  	"database/sql/driver"
     6  	"fmt"
     7  
     8  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
     9  	"github.com/ydb-platform/ydb-go-sdk/v3/table"
    10  )
    11  
    12  // ToYDB maps driver transaction options to ydb transaction Option or query transaction control.
    13  // This caused by ydb logic that prevents start actual transaction with OnlineReadOnly mode and ReadCommitted
    14  // and ReadUncommitted isolation levels should use tx_control in every query request.
    15  // It returns error on unsupported options.
    16  func ToYDB(opts driver.TxOptions) (txcControl table.TxOption, err error) {
    17  	level := sql.IsolationLevel(opts.Isolation)
    18  	switch level {
    19  	case sql.LevelDefault, sql.LevelSerializable:
    20  		if !opts.ReadOnly {
    21  			return table.WithSerializableReadWrite(), nil
    22  		}
    23  	case sql.LevelSnapshot:
    24  		if opts.ReadOnly {
    25  			return table.WithSnapshotReadOnly(), nil
    26  		}
    27  	}
    28  
    29  	return nil, xerrors.WithStackTrace(fmt.Errorf(
    30  		"unsupported transaction options: %+v", opts,
    31  	))
    32  }