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

Method executeInAutoCommitMode

backend/plugin/db/mysql/mysql.go:464–508  ·  view source on GitHub ↗

executeInAutoCommitMode executes statements sequentially in auto-commit mode

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

Source from the content-addressed store, hash-verified

462
463// executeInAutoCommitMode executes statements sequentially in auto-commit mode
464func (d *Driver) executeInAutoCommitMode(ctx context.Context, conn *sql.Conn, commands []base.Statement, opts db.ExecuteOptions, connectionID string) (int64, error) {
465 var totalRowsAffected int64
466
467 if err := conn.Raw(func(driverConn any) error {
468 //nolint
469 exer := driverConn.(driver.ExecerContext)
470
471 for _, command := range commands {
472 opts.LogCommandExecute(command.Range, command.Text)
473
474 sqlWithBytebaseAppComment := util.MySQLPrependBytebaseAppComment(command.Text)
475 sqlResult, err := exer.ExecContext(ctx, sqlWithBytebaseAppComment, nil)
476 if err != nil {
477 if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
478 slog.Info("cancel connection", slog.String("connectionID", connectionID))
479 if err := d.StopConnectionByID(connectionID); err != nil {
480 slog.Error("failed to cancel connection", slog.String("connectionID", connectionID), log.BBError(err))
481 }
482 }
483
484 opts.LogCommandResponse(0, nil, err.Error())
485 // In auto-commit mode, we stop at the first error
486 // The database is left in a partially migrated state
487 return err
488 }
489
490 allRowsAffected := sqlResult.(mysql.Result).AllRowsAffected()
491 var rowsAffected int64
492 var allRowsAffectedInt64 []int64
493 for _, a := range allRowsAffected {
494 rowsAffected += a
495 allRowsAffectedInt64 = append(allRowsAffectedInt64, a)
496 }
497 totalRowsAffected += rowsAffected
498
499 opts.LogCommandResponse(rowsAffected, allRowsAffectedInt64, "")
500 }
501
502 return nil
503 }); err != nil {
504 return 0, err
505 }
506
507 return totalRowsAffected, nil
508}
509
510// QueryConn queries a SQL statement in a given connection.
511func (d *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string, queryContext db.QueryContext) ([]*v1pb.QueryResult, error) {

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 8

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

Tested by

no test coverage detected