executeInAutoCommitMode executes statements sequentially in auto-commit mode
(ctx context.Context, conn *sql.Conn, commands []base.Statement, opts db.ExecuteOptions, connectionID string)
| 462 | |
| 463 | // executeInAutoCommitMode executes statements sequentially in auto-commit mode |
| 464 | func (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. |
| 511 | func (d *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string, queryContext db.QueryContext) ([]*v1pb.QueryResult, error) { |
no test coverage detected