MCPcopy Create free account
hub / github.com/bytebase/bytebase / executeInAutoCommitMode

Method executeInAutoCommitMode

backend/plugin/db/mssql/mssql.go:265–316  ·  view source on GitHub ↗

executeInAutoCommitMode executes statements sequentially in auto-commit mode

(ctx context.Context, statement string, opts db.ExecuteOptions)

Source from the content-addressed store, hash-verified

263
264// executeInAutoCommitMode executes statements sequentially in auto-commit mode
265func (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
319func (d *Driver) executeAutoCommit(ctx context.Context, statement string) (int64, error) {

Callers 1

ExecuteMethod · 0.95

Implementers 15

MockDriverbackend/plugin/advisor/utils_for_tests
Driverbackend/plugin/db/bigquery/bigquery.go
Driverbackend/plugin/db/mongodb/mongodb.go
Driverbackend/plugin/db/trino/trino.go
Driverbackend/plugin/db/redshift/redshift.go
Driverbackend/plugin/db/oracle/oracle.go
Driverbackend/plugin/db/dynamodb/dynamodb.go
Driverbackend/plugin/db/cosmosdb/cosmosdb.go
Driverbackend/plugin/db/spanner/spanner.go
Driverbackend/plugin/db/mssql/mssql.go
Driverbackend/plugin/db/mysql/mysql.go
Driverbackend/plugin/db/pg/pg.go

Calls 8

NextMethod · 0.95
BatchMethod · 0.95
executeAutoCommitMethod · 0.95
ResetMethod · 0.95
LogCommandExecuteMethod · 0.80
LogCommandResponseMethod · 0.80
ErrorfMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected