github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/query/transaction.go (about) 1 package query 2 3 import ( 4 "context" 5 6 "github.com/ydb-platform/ydb-go-genproto/Ydb_Query_V1" 7 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb" 8 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Query" 9 10 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors" 11 "github.com/ydb-platform/ydb-go-sdk/v3/query" 12 ) 13 14 var _ query.Transaction = (*transaction)(nil) 15 16 type transaction struct { 17 id string 18 s *Session 19 } 20 21 func (tx transaction) ID() string { 22 return tx.id 23 } 24 25 func (tx transaction) Execute(ctx context.Context, q string, opts ...query.TxExecuteOption) ( 26 r query.Result, err error, 27 ) { 28 _, res, err := execute(ctx, tx.s, tx.s.queryClient, q, query.TxExecuteSettings(tx.id, opts...).ExecuteSettings) 29 if err != nil { 30 return nil, xerrors.WithStackTrace(err) 31 } 32 33 return res, nil 34 } 35 36 func commitTx(ctx context.Context, client Ydb_Query_V1.QueryServiceClient, sessionID, txID string) error { 37 response, err := client.CommitTransaction(ctx, &Ydb_Query.CommitTransactionRequest{ 38 SessionId: sessionID, 39 TxId: txID, 40 }) 41 if err != nil { 42 return xerrors.WithStackTrace(xerrors.Transport(err)) 43 } 44 if response.GetStatus() != Ydb.StatusIds_SUCCESS { 45 return xerrors.WithStackTrace(xerrors.FromOperation(response)) 46 } 47 48 return nil 49 } 50 51 func (tx transaction) CommitTx(ctx context.Context) (err error) { 52 return commitTx(ctx, tx.s.queryClient, tx.s.id, tx.id) 53 } 54 55 func rollback(ctx context.Context, client Ydb_Query_V1.QueryServiceClient, sessionID, txID string) error { 56 response, err := client.RollbackTransaction(ctx, &Ydb_Query.RollbackTransactionRequest{ 57 SessionId: sessionID, 58 TxId: txID, 59 }) 60 if err != nil { 61 return xerrors.WithStackTrace(xerrors.Transport(err)) 62 } 63 if response.GetStatus() != Ydb.StatusIds_SUCCESS { 64 return xerrors.WithStackTrace(xerrors.FromOperation(response)) 65 } 66 67 return nil 68 } 69 70 func (tx transaction) Rollback(ctx context.Context) (err error) { 71 return rollback(ctx, tx.s.queryClient, tx.s.id, tx.id) 72 }