( ctx context.Context, owner string, statement string, commands []base.Statement, nonTransactionAndSetRoleStmts []base.Statement, opts db.ExecuteOptions, isPlsql bool, )
| 498 | } |
| 499 | |
| 500 | func (d *Driver) executeInTransactionMode( |
| 501 | ctx context.Context, |
| 502 | owner string, |
| 503 | statement string, |
| 504 | commands []base.Statement, |
| 505 | nonTransactionAndSetRoleStmts []base.Statement, |
| 506 | opts db.ExecuteOptions, |
| 507 | isPlsql bool, |
| 508 | ) (int64, error) { |
| 509 | conn, err := d.db.Conn(ctx) |
| 510 | if err != nil { |
| 511 | return 0, errors.Wrapf(err, "failed to get connection") |
| 512 | } |
| 513 | defer conn.Close() |
| 514 | |
| 515 | if isPlsql { |
| 516 | if d.connectionCtx.TenantMode { |
| 517 | // USE SET SESSION ROLE to set the role for the current session. |
| 518 | if _, err := conn.ExecContext(ctx, fmt.Sprintf("SET SESSION ROLE '%s'", owner)); err != nil { // NOSONAR(go:S2077) owner is from pg_roles system catalog, not user input |
| 519 | return 0, errors.Wrapf(err, "failed to set role to database owner %q", owner) |
| 520 | } |
| 521 | } |
| 522 | opts.LogCommandExecute(&storepb.Range{Start: 0, End: int32(len(statement))}, statement) |
| 523 | if _, err := conn.ExecContext(ctx, statement); err != nil { |
| 524 | opts.LogCommandResponse(0, []int64{0}, err.Error()) |
| 525 | return 0, err |
| 526 | } |
| 527 | opts.LogCommandResponse(0, []int64{0}, "") |
| 528 | |
| 529 | return 0, nil |
| 530 | } |
| 531 | |
| 532 | totalRowsAffected := int64(0) |
| 533 | |
| 534 | totalCommands := len(commands) |
| 535 | if totalCommands > 0 { |
| 536 | err = conn.Raw(func(driverConn any) error { |
| 537 | conn := driverConn.(*stdlib.Conn).Conn() |
| 538 | |
| 539 | tx, err := conn.Begin(ctx) |
| 540 | if err != nil { |
| 541 | opts.LogTransactionControl(storepb.TaskRunLog_TransactionControl_BEGIN, err.Error()) |
| 542 | return errors.Wrapf(err, "failed to begin transaction") |
| 543 | } |
| 544 | opts.LogTransactionControl(storepb.TaskRunLog_TransactionControl_BEGIN, "") |
| 545 | |
| 546 | committed := false |
| 547 | defer func() { |
| 548 | rollbackErr := tx.Rollback(ctx) |
| 549 | if committed { |
| 550 | return |
| 551 | } |
| 552 | var rerr string |
| 553 | if rollbackErr != nil { |
| 554 | rerr = rollbackErr.Error() |
| 555 | } |
| 556 | opts.LogTransactionControl(storepb.TaskRunLog_TransactionControl_ROLLBACK, rerr) |
| 557 | }() |
no test coverage detected