executeInAutoCommitMode executes statements sequentially in auto-commit mode
(ctx context.Context, statement string, opts db.ExecuteOptions)
| 263 | |
| 264 | // executeInAutoCommitMode executes statements sequentially in auto-commit mode |
| 265 | func (d *Driver) executeInAutoCommitMode(ctx context.Context, statement string, opts db.ExecuteOptions) (int64, error) { |
| 266 | totalAffectRows := int64(0) |
| 267 | |
| 268 | batch := tsqlbatch.NewBatcher(statement) |
| 269 | |
| 270 | for idx := 0; ; { |
| 271 | command, err := batch.Next() |
| 272 | if err != nil { |
| 273 | if err == io.EOF { |
| 274 | // Try send the last batch to server. |
| 275 | v := batch.Batch() |
| 276 | if v != nil && len(v.Text) > 0 { |
| 277 | opts.LogCommandExecute(&storepb.Range{Start: int32(v.Start), End: int32(v.End)}, v.Text) |
| 278 | rowsAffected, err := d.executeAutoCommit(ctx, v.Text) |
| 279 | if err != nil { |
| 280 | opts.LogCommandResponse(0, nil, err.Error()) |
| 281 | return totalAffectRows, err |
| 282 | } |
| 283 | opts.LogCommandResponse(rowsAffected, []int64{rowsAffected}, "") |
| 284 | totalAffectRows += rowsAffected |
| 285 | } |
| 286 | break |
| 287 | } |
| 288 | return 0, errors.Wrapf(err, "failed to get next batch for statement: %s", batch.Batch().Text) |
| 289 | } |
| 290 | if command == nil { |
| 291 | continue |
| 292 | } |
| 293 | switch v := command.(type) { |
| 294 | case *tsqlbatch.GoCommand: |
| 295 | b := batch.Batch() |
| 296 | // Execute the batch in auto-commit mode |
| 297 | idx++ |
| 298 | for i := uint(0); i < v.Count; i++ { |
| 299 | opts.LogCommandExecute(&storepb.Range{Start: int32(b.Start), End: int32(b.End)}, b.Text) |
| 300 | rowsAffected, err := d.executeAutoCommit(ctx, b.Text) |
| 301 | if err != nil { |
| 302 | opts.LogCommandResponse(0, nil, err.Error()) |
| 303 | // In auto-commit mode, we stop at the first error |
| 304 | return totalAffectRows, err |
| 305 | } |
| 306 | opts.LogCommandResponse(rowsAffected, []int64{rowsAffected}, "") |
| 307 | totalAffectRows += rowsAffected |
| 308 | } |
| 309 | default: |
| 310 | return 0, errors.Errorf("unsupported command type: %T", v) |
| 311 | } |
| 312 | batch.Reset(nil) |
| 313 | } |
| 314 | |
| 315 | return totalAffectRows, nil |
| 316 | } |
| 317 | |
| 318 | // executeAutoCommit executes a single statement in auto-commit mode |
| 319 | func (d *Driver) executeAutoCommit(ctx context.Context, statement string) (int64, error) { |
no test coverage detected