|
| 1 | +package isolation |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "database/sql/driver" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Table" |
| 10 | + |
| 11 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors" |
| 12 | + "github.com/ydb-platform/ydb-go-sdk/v3/table" |
| 13 | +) |
| 14 | + |
| 15 | +var errUnsupportedReadOnlyMode = errors.New("unsupported read-only mode") |
| 16 | + |
| 17 | +// ToYDB maps driver transaction options to ydb transaction Option or query transaction control. |
| 18 | +// This caused by ydb logic that prevents start actual transaction with OnlineReadOnly mode and ReadCommitted |
| 19 | +// and ReadUncommitted isolation levels should use tx_control in every query request. |
| 20 | +// It returns error on unsupported options. |
| 21 | +func ToYDB(opts driver.TxOptions) (txSettings *table.TransactionSettings, err error) { |
| 22 | + level := sql.IsolationLevel(opts.Isolation) |
| 23 | + if !opts.ReadOnly { |
| 24 | + switch level { |
| 25 | + case sql.LevelDefault, |
| 26 | + sql.LevelSerializable: |
| 27 | + return table.TxSettings(table.WithSerializableReadWrite()), nil |
| 28 | + default: |
| 29 | + return nil, xerrors.WithStackTrace(fmt.Errorf( |
| 30 | + "ydb: unsupported transaction options: isolation=%s read_only=%t", |
| 31 | + nameIsolationLevel(level), opts.ReadOnly, |
| 32 | + )) |
| 33 | + } |
| 34 | + } |
| 35 | + return nil, xerrors.WithStackTrace(errUnsupportedReadOnlyMode) |
| 36 | +} |
| 37 | + |
| 38 | +func nameIsolationLevel(x sql.IsolationLevel) string { |
| 39 | + if int(x) < len(isolationLevelName) { |
| 40 | + return isolationLevelName[x] |
| 41 | + } |
| 42 | + return "unknown_isolation" |
| 43 | +} |
| 44 | + |
| 45 | +var isolationLevelName = [...]string{ |
| 46 | + sql.LevelDefault: "default", |
| 47 | + sql.LevelReadUncommitted: "read_uncommitted", |
| 48 | + sql.LevelReadCommitted: "read_committed", |
| 49 | + sql.LevelWriteCommitted: "write_committed", |
| 50 | + sql.LevelRepeatableRead: "repeatable_read", |
| 51 | + sql.LevelSnapshot: "snapshot", |
| 52 | + sql.LevelSerializable: "serializable", |
| 53 | + sql.LevelLinearizable: "linearizable", |
| 54 | +} |
| 55 | + |
| 56 | +// FromYDB maps table transaction settings to driver transaction options |
| 57 | +func FromYDB(txSettings *table.TransactionSettings) (txOptions *sql.TxOptions, err error) { |
| 58 | + switch txMode := txSettings.Settings().TxMode.(type) { |
| 59 | + case *Ydb_Table.TransactionSettings_SerializableReadWrite: |
| 60 | + return &sql.TxOptions{ |
| 61 | + Isolation: sql.LevelSerializable, |
| 62 | + ReadOnly: false, |
| 63 | + }, nil |
| 64 | + case *Ydb_Table.TransactionSettings_OnlineReadOnly: |
| 65 | + txOptions = &sql.TxOptions{ |
| 66 | + Isolation: sql.LevelReadCommitted, |
| 67 | + ReadOnly: true, |
| 68 | + } |
| 69 | + if txMode.OnlineReadOnly.AllowInconsistentReads { |
| 70 | + txOptions.Isolation = sql.LevelReadUncommitted |
| 71 | + } |
| 72 | + return txOptions, nil |
| 73 | + case *Ydb_Table.TransactionSettings_StaleReadOnly: |
| 74 | + return &sql.TxOptions{ |
| 75 | + Isolation: sql.LevelReadCommitted, |
| 76 | + ReadOnly: true, |
| 77 | + }, nil |
| 78 | + default: |
| 79 | + return nil, xerrors.WithStackTrace(fmt.Errorf( |
| 80 | + "ydb: unsupported transaction options: txMode=%T(%v)", |
| 81 | + txMode, txMode, |
| 82 | + )) |
| 83 | + } |
| 84 | +} |
0 commit comments