MCPcopy Create free account
hub / github.com/bytebase/bytebase / executeInAutoCommitMode

Method executeInAutoCommitMode

backend/plugin/db/tidb/tidb.go:315–359  ·  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

313
314// executeInAutoCommitMode executes statements sequentially in auto-commit mode
315func (d *Driver) executeInAutoCommitMode(ctx context.Context, conn *sql.Conn, commands []base.Statement, opts db.ExecuteOptions, connectionID string) (int64, error) {
316 var totalRowsAffected int64
317
318 if err := conn.Raw(func(driverConn any) error {
319 //nolint
320 exer := driverConn.(driver.ExecerContext)
321
322 for _, command := range commands {
323 opts.LogCommandExecute(command.Range, command.Text)
324
325 sqlWithBytebaseAppComment := util.MySQLPrependBytebaseAppComment(command.Text)
326 sqlResult, err := exer.ExecContext(ctx, sqlWithBytebaseAppComment, nil)
327 if err != nil {
328 if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
329 slog.Info("cancel connection", slog.String("connectionID", connectionID))
330 if err := d.StopConnectionByID(connectionID); err != nil {
331 slog.Error("failed to cancel connection", slog.String("connectionID", connectionID), log.BBError(err))
332 }
333 }
334
335 opts.LogCommandResponse(0, nil, err.Error())
336 // In auto-commit mode, we stop at the first error
337 // The database is left in a partially migrated state
338 return err
339 }
340
341 allRowsAffected := sqlResult.(mysql.Result).AllRowsAffected()
342 var rowsAffected int64
343 var allRowsAffectedInt64 []int64
344 for _, a := range allRowsAffected {
345 rowsAffected += a
346 allRowsAffectedInt64 = append(allRowsAffectedInt64, a)
347 }
348 totalRowsAffected += rowsAffected
349
350 opts.LogCommandResponse(rowsAffected, allRowsAffectedInt64, "")
351 }
352
353 return nil
354 }); err != nil {
355 return 0, err
356 }
357
358 return totalRowsAffected, nil
359}
360
361// QueryConn queries a SQL statement in a given connection.
362func (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