github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/xsql/tx_fake.go (about) 1 package xsql 2 3 import ( 4 "context" 5 "database/sql/driver" 6 7 "github.com/ydb-platform/ydb-go-sdk/v3/internal/stack" 8 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext" 9 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors" 10 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql/badconn" 11 "github.com/ydb-platform/ydb-go-sdk/v3/table" 12 "github.com/ydb-platform/ydb-go-sdk/v3/trace" 13 ) 14 15 type txFake struct { 16 beginCtx context.Context 17 conn *conn 18 ctx context.Context 19 } 20 21 func (tx *txFake) PrepareContext(ctx context.Context, query string) (_ driver.Stmt, finalErr error) { 22 onDone := trace.DatabaseSQLOnTxPrepare(tx.conn.trace, &ctx, 23 stack.FunctionID(""), 24 &tx.beginCtx, tx, query, 25 ) 26 defer func() { 27 onDone(finalErr) 28 }() 29 if !tx.conn.isReady() { 30 return nil, badconn.Map(xerrors.WithStackTrace(errNotReadyConn)) 31 } 32 33 return &stmt{ 34 conn: tx.conn, 35 processor: tx, 36 stmtCtx: ctx, 37 query: query, 38 trace: tx.conn.trace, 39 }, nil 40 } 41 42 var ( 43 _ driver.Tx = &txFake{} 44 _ driver.ExecerContext = &txFake{} 45 _ driver.QueryerContext = &txFake{} 46 _ table.TransactionIdentifier = &txFake{} 47 ) 48 49 func (c *conn) beginTxFake(ctx context.Context, txOptions driver.TxOptions) (currentTx, error) { 50 return &txFake{ 51 conn: c, 52 ctx: ctx, 53 }, nil 54 } 55 56 func (tx *txFake) ID() string { 57 return "FAKE" 58 } 59 60 func (tx *txFake) Commit() (err error) { 61 onDone := trace.DatabaseSQLOnTxCommit(tx.conn.trace, &tx.ctx, 62 stack.FunctionID(""), 63 tx, 64 ) 65 defer func() { 66 onDone(err) 67 }() 68 defer func() { 69 tx.conn.currentTx = nil 70 }() 71 if !tx.conn.isReady() { 72 return badconn.Map(xerrors.WithStackTrace(errNotReadyConn)) 73 } 74 75 return nil 76 } 77 78 func (tx *txFake) Rollback() (err error) { 79 onDone := trace.DatabaseSQLOnTxRollback(tx.conn.trace, &tx.ctx, 80 stack.FunctionID(""), 81 tx, 82 ) 83 defer func() { 84 onDone(err) 85 }() 86 defer func() { 87 tx.conn.currentTx = nil 88 }() 89 if !tx.conn.isReady() { 90 return badconn.Map(xerrors.WithStackTrace(errNotReadyConn)) 91 } 92 93 return err 94 } 95 96 func (tx *txFake) QueryContext(ctx context.Context, query string, args []driver.NamedValue) ( 97 rows driver.Rows, err error, 98 ) { 99 onDone := trace.DatabaseSQLOnTxQuery( 100 tx.conn.trace, &ctx, 101 stack.FunctionID(""), 102 tx.ctx, tx, query, xcontext.IsIdempotent(ctx), 103 ) 104 defer func() { 105 onDone(err) 106 }() 107 rows, err = tx.conn.QueryContext(ctx, query, args) 108 if err != nil { 109 return nil, xerrors.WithStackTrace(err) 110 } 111 112 return rows, nil 113 } 114 115 func (tx *txFake) ExecContext(ctx context.Context, query string, args []driver.NamedValue) ( 116 result driver.Result, err error, 117 ) { 118 onDone := trace.DatabaseSQLOnTxExec( 119 tx.conn.trace, &ctx, 120 stack.FunctionID(""), 121 tx.ctx, tx, query, xcontext.IsIdempotent(ctx), 122 ) 123 defer func() { 124 onDone(err) 125 }() 126 result, err = tx.conn.ExecContext(ctx, query, args) 127 if err != nil { 128 return nil, xerrors.WithStackTrace(err) 129 } 130 131 return result, nil 132 }