(ctx context.Context, statement string, opts db.ExecuteOptions)
| 192 | } |
| 193 | |
| 194 | func (d *Driver) executeInTransactionMode(ctx context.Context, statement string, opts db.ExecuteOptions) (int64, error) { |
| 195 | conn, err := d.db.Conn(ctx) |
| 196 | if err != nil { |
| 197 | return 0, err |
| 198 | } |
| 199 | defer conn.Close() |
| 200 | |
| 201 | connectionID, err := getConnectionID(ctx, conn) |
| 202 | if err != nil { |
| 203 | return 0, err |
| 204 | } |
| 205 | slog.Debug("connectionID", slog.String("connectionID", connectionID)) |
| 206 | |
| 207 | tx, err := conn.BeginTx(ctx, nil) |
| 208 | if err != nil { |
| 209 | opts.LogTransactionControl(storepb.TaskRunLog_TransactionControl_BEGIN, err.Error()) |
| 210 | return 0, errors.Wrapf(err, "failed to begin execute transaction") |
| 211 | } |
| 212 | opts.LogTransactionControl(storepb.TaskRunLog_TransactionControl_BEGIN, "") |
| 213 | |
| 214 | committed := false |
| 215 | defer func() { |
| 216 | if !committed { |
| 217 | err := tx.Rollback() |
| 218 | var rerr string |
| 219 | if err != nil && !errors.Is(err, sql.ErrTxDone) { |
| 220 | rerr = err.Error() |
| 221 | slog.Debug("failed to rollback transaction", log.BBError(err)) |
| 222 | } |
| 223 | opts.LogTransactionControl(storepb.TaskRunLog_TransactionControl_ROLLBACK, rerr) |
| 224 | } |
| 225 | }() |
| 226 | |
| 227 | sqlResult, err := tx.ExecContext(ctx, statement) |
| 228 | if err != nil { |
| 229 | if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { |
| 230 | slog.Info("cancel connection", slog.String("connectionID", connectionID)) |
| 231 | if err := d.StopConnectionByID(connectionID); err != nil { |
| 232 | slog.Error("failed to cancel connection", slog.String("connectionID", connectionID), log.BBError(err)) |
| 233 | } |
| 234 | } |
| 235 | return 0, err |
| 236 | } |
| 237 | |
| 238 | rowsAffected, err := sqlResult.RowsAffected() |
| 239 | if err != nil { |
| 240 | // Since we cannot differentiate DDL and DML yet, we have to ignore the error. |
| 241 | slog.Debug("rowsAffected returns error", log.BBError(err)) |
| 242 | } |
| 243 | |
| 244 | if err := tx.Commit(); err != nil { |
| 245 | opts.LogTransactionControl(storepb.TaskRunLog_TransactionControl_COMMIT, err.Error()) |
| 246 | return 0, errors.Wrapf(err, "failed to commit execute transaction") |
| 247 | } |
| 248 | opts.LogTransactionControl(storepb.TaskRunLog_TransactionControl_COMMIT, "") |
| 249 | committed = true |
| 250 | return rowsAffected, nil |
| 251 | } |
no test coverage detected