( ctx context.Context, owner string, statement string, commands []base.Statement, nonTransactionAndSetRoleStmts []base.Statement, opts db.ExecuteOptions, isPlsql bool, )
| 624 | } |
| 625 | |
| 626 | func (d *Driver) executeInAutoCommitMode( |
| 627 | ctx context.Context, |
| 628 | owner string, |
| 629 | statement string, |
| 630 | commands []base.Statement, |
| 631 | nonTransactionAndSetRoleStmts []base.Statement, |
| 632 | opts db.ExecuteOptions, |
| 633 | isPlsql bool, |
| 634 | ) (int64, error) { |
| 635 | // For auto-commit mode, treat all statements as non-transactional |
| 636 | nonTransactionAndSetRoleStmts = append(nonTransactionAndSetRoleStmts, commands...) |
| 637 | |
| 638 | conn, err := d.db.Conn(ctx) |
| 639 | if err != nil { |
| 640 | return 0, errors.Wrapf(err, "failed to get connection") |
| 641 | } |
| 642 | defer conn.Close() |
| 643 | |
| 644 | if d.connectionCtx.TenantMode { |
| 645 | // USE SET SESSION ROLE to set the role for the current session. |
| 646 | 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 |
| 647 | return 0, errors.Wrapf(err, "failed to set role to database owner %q", owner) |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | if isPlsql { |
| 652 | opts.LogCommandExecute(&storepb.Range{Start: 0, End: int32(len(statement))}, statement) |
| 653 | if _, err := conn.ExecContext(ctx, statement); err != nil { |
| 654 | opts.LogCommandResponse(0, []int64{0}, err.Error()) |
| 655 | return 0, err |
| 656 | } |
| 657 | opts.LogCommandResponse(0, []int64{0}, "") |
| 658 | return 0, nil |
| 659 | } |
| 660 | |
| 661 | totalRowsAffected := int64(0) |
| 662 | // Execute all statements individually in auto-commit mode |
| 663 | for _, stmt := range nonTransactionAndSetRoleStmts { |
| 664 | opts.LogCommandExecute(stmt.Range, stmt.Text) |
| 665 | |
| 666 | sqlResult, err := conn.ExecContext(ctx, stmt.Text) |
| 667 | if err != nil { |
| 668 | opts.LogCommandResponse(0, []int64{0}, err.Error()) |
| 669 | if isLockTimeoutError(err.Error()) { |
| 670 | return totalRowsAffected, &LockTimeoutError{ |
| 671 | Message: err.Error(), |
| 672 | } |
| 673 | } |
| 674 | return totalRowsAffected, err |
| 675 | } |
| 676 | |
| 677 | rowsAffected, err := sqlResult.RowsAffected() |
| 678 | if err != nil { |
| 679 | // PostgreSQL returns error for statements that don't support RowsAffected |
| 680 | rowsAffected = 0 |
| 681 | } |
| 682 | |
| 683 | opts.LogCommandResponse(rowsAffected, []int64{rowsAffected}, "") |
no test coverage detected