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