github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/query/transaction_control.go (about) 1 package query 2 3 import ( 4 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Query" 5 6 "github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator" 7 ) 8 9 var ( 10 _ interface { 11 ToYDB(a *allocator.Allocator) *Ydb_Query.TransactionControl 12 } = (*TransactionControl)(nil) 13 _ txSelector = (*TransactionSettings)(nil) 14 ) 15 16 type ( 17 txSelector interface { 18 applyTxSelector(a *allocator.Allocator, txControl *Ydb_Query.TransactionControl) 19 } 20 txControlOption interface { 21 applyTxControlOption(txControl *TransactionControl) 22 } 23 TransactionControl struct { 24 selector txSelector 25 commit bool 26 } 27 transactionControlOption struct { 28 txControl *TransactionControl 29 } 30 ) 31 32 func (opt *transactionControlOption) applyExecuteOption(s *executeSettings) { 33 s.txControl = opt.txControl 34 } 35 36 func (ctrl *TransactionControl) ToYDB(a *allocator.Allocator) *Ydb_Query.TransactionControl { 37 if ctrl == nil { 38 return nil 39 } 40 41 txControl := a.QueryTransactionControl() 42 ctrl.selector.applyTxSelector(a, txControl) 43 txControl.CommitTx = ctrl.commit 44 45 return txControl 46 } 47 48 var ( 49 _ txControlOption = beginTxOptions{} 50 _ txSelector = beginTxOptions{} 51 ) 52 53 type beginTxOptions []txSettingsOption 54 55 func (opts beginTxOptions) applyTxControlOption(txControl *TransactionControl) { 56 txControl.selector = opts 57 } 58 59 func (opts beginTxOptions) applyTxSelector(a *allocator.Allocator, txControl *Ydb_Query.TransactionControl) { 60 selector := a.QueryTransactionControlBeginTx() 61 selector.BeginTx = a.QueryTransactionSettings() 62 for _, opt := range opts { 63 opt.applyTxSettingsOption(a, selector.BeginTx) 64 } 65 txControl.TxSelector = selector 66 } 67 68 // BeginTx returns selector transaction control option 69 func BeginTx(opts ...txSettingsOption) beginTxOptions { 70 return opts 71 } 72 73 var ( 74 _ txControlOption = txIDTxControlOption("") 75 _ txSelector = txIDTxControlOption("") 76 ) 77 78 type txIDTxControlOption string 79 80 func (id txIDTxControlOption) applyTxControlOption(txControl *TransactionControl) { 81 txControl.selector = id 82 } 83 84 func (id txIDTxControlOption) applyTxSelector(a *allocator.Allocator, txControl *Ydb_Query.TransactionControl) { 85 selector := a.QueryTransactionControlTxID() 86 selector.TxId = string(id) 87 txControl.TxSelector = selector 88 } 89 90 func WithTx(t TxIdentifier) txIDTxControlOption { 91 return txIDTxControlOption(t.ID()) 92 } 93 94 func WithTxID(txID string) txIDTxControlOption { 95 return txIDTxControlOption(txID) 96 } 97 98 type commitTxOption struct{} 99 100 func (c commitTxOption) applyTxControlOption(txControl *TransactionControl) { 101 txControl.commit = true 102 } 103 104 // CommitTx returns commit transaction control option 105 func CommitTx() txControlOption { 106 return commitTxOption{} 107 } 108 109 // TxControl makes transaction control from given options 110 func TxControl(opts ...txControlOption) *TransactionControl { 111 txControl := &TransactionControl{ 112 selector: BeginTx(WithSerializableReadWrite()), 113 commit: false, 114 } 115 for _, opt := range opts { 116 if opt != nil { 117 opt.applyTxControlOption(txControl) 118 } 119 } 120 121 return txControl 122 } 123 124 func NoTx() *TransactionControl { 125 return nil 126 } 127 128 // DefaultTxControl returns default transaction control with serializable read-write isolation mode and auto-commit 129 func DefaultTxControl() *TransactionControl { 130 return TxControl( 131 BeginTx(WithSerializableReadWrite()), 132 CommitTx(), 133 ) 134 } 135 136 // SerializableReadWriteTxControl returns transaction control with serializable read-write isolation mode 137 func SerializableReadWriteTxControl(opts ...txControlOption) *TransactionControl { 138 return TxControl( 139 append([]txControlOption{ 140 BeginTx(WithSerializableReadWrite()), 141 }, opts...)..., 142 ) 143 } 144 145 // OnlineReadOnlyTxControl returns online read-only transaction control 146 func OnlineReadOnlyTxControl(opts ...TxOnlineReadOnlyOption) *TransactionControl { 147 return TxControl( 148 BeginTx(WithOnlineReadOnly(opts...)), 149 CommitTx(), // open transactions not supported for OnlineReadOnly 150 ) 151 } 152 153 // StaleReadOnlyTxControl returns stale read-only transaction control 154 func StaleReadOnlyTxControl() *TransactionControl { 155 return TxControl( 156 BeginTx(WithStaleReadOnly()), 157 CommitTx(), // open transactions not supported for StaleReadOnly 158 ) 159 } 160 161 // SnapshotReadOnlyTxControl returns snapshot read-only transaction control 162 func SnapshotReadOnlyTxControl() *TransactionControl { 163 return TxControl( 164 BeginTx(WithSnapshotReadOnly()), 165 CommitTx(), // open transactions not supported for StaleReadOnly 166 ) 167 }