MCPcopy Index your code
hub / github.com/bytebase/bytebase / executeInTransactionMode

Method executeInTransactionMode

backend/plugin/db/tidb/tidb.go:240–312  ·  view source on GitHub ↗

executeInTransactionMode executes statements within a single transaction

(ctx context.Context, conn *sql.Conn, commands []base.Statement, opts db.ExecuteOptions, connectionID string)

Source from the content-addressed store, hash-verified

238
239// executeInTransactionMode executes statements within a single transaction
240func (d *Driver) executeInTransactionMode(ctx context.Context, conn *sql.Conn, commands []base.Statement, opts db.ExecuteOptions, connectionID string) (int64, error) {
241 var totalRowsAffected int64
242
243 if err := conn.Raw(func(driverConn any) error {
244 //nolint
245 exer := driverConn.(driver.ExecerContext)
246 //nolint
247 txer := driverConn.(driver.ConnBeginTx)
248 tx, err := txer.BeginTx(ctx, driver.TxOptions{})
249 if err != nil {
250 opts.LogTransactionControl(storepb.TaskRunLog_TransactionControl_BEGIN, err.Error())
251 return err
252 } else {
253 opts.LogTransactionControl(storepb.TaskRunLog_TransactionControl_BEGIN, "")
254 }
255
256 committed := false
257 defer func() {
258 err := tx.Rollback()
259 if committed {
260 return
261 }
262 var rerr string
263 if err != nil {
264 rerr = err.Error()
265 }
266 opts.LogTransactionControl(storepb.TaskRunLog_TransactionControl_ROLLBACK, rerr)
267 }()
268
269 for _, command := range commands {
270 opts.LogCommandExecute(command.Range, command.Text)
271
272 sqlWithBytebaseAppComment := util.MySQLPrependBytebaseAppComment(command.Text)
273 sqlResult, err := exer.ExecContext(ctx, sqlWithBytebaseAppComment, nil)
274 if err != nil {
275 if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
276 slog.Info("cancel connection", slog.String("connectionID", connectionID))
277 if err := d.StopConnectionByID(connectionID); err != nil {
278 slog.Error("failed to cancel connection", slog.String("connectionID", connectionID), log.BBError(err))
279 }
280 }
281
282 opts.LogCommandResponse(0, nil, err.Error())
283
284 return err
285 }
286
287 allRowsAffected := sqlResult.(mysql.Result).AllRowsAffected()
288 var rowsAffected int64
289 var allRowsAffectedInt64 []int64
290 for _, a := range allRowsAffected {
291 rowsAffected += a
292 allRowsAffectedInt64 = append(allRowsAffectedInt64, a)
293 }
294 totalRowsAffected += rowsAffected
295
296 opts.LogCommandResponse(rowsAffected, allRowsAffectedInt64, "")
297 }

Callers 1

ExecuteMethod · 0.95

Implementers 15

MockDriverbackend/plugin/advisor/utils_for_tests
Driverbackend/plugin/db/bigquery/bigquery.go
Driverbackend/plugin/db/mongodb/mongodb.go
Driverbackend/plugin/db/trino/trino.go
Driverbackend/plugin/db/redshift/redshift.go
Driverbackend/plugin/db/oracle/oracle.go
Driverbackend/plugin/db/dynamodb/dynamodb.go
Driverbackend/plugin/db/cosmosdb/cosmosdb.go
Driverbackend/plugin/db/spanner/spanner.go
Driverbackend/plugin/db/mssql/mssql.go
Driverbackend/plugin/db/mysql/mysql.go
Driverbackend/plugin/db/pg/pg.go

Calls 9

StopConnectionByIDMethod · 0.95
BBErrorFunction · 0.92
LogTransactionControlMethod · 0.80
LogCommandExecuteMethod · 0.80
InfoMethod · 0.80
LogCommandResponseMethod · 0.80
StringMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected