MCPcopy Index your code
hub / github.com/bytebase/bytebase / executeInTransactionMode

Method executeInTransactionMode

backend/plugin/db/snowflake/snowflake.go:253–301  ·  view source on GitHub ↗

executeInTransactionMode executes statements within a single transaction

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

Source from the content-addressed store, hash-verified

251
252// executeInTransactionMode executes statements within a single transaction
253func (d *Driver) executeInTransactionMode(ctx context.Context, statement string, opts db.ExecuteOptions) (int64, error) {
254 tx, err := d.db.BeginTx(ctx, nil)
255 if err != nil {
256 opts.LogTransactionControl(storepb.TaskRunLog_TransactionControl_BEGIN, err.Error())
257 return 0, err
258 }
259 opts.LogTransactionControl(storepb.TaskRunLog_TransactionControl_BEGIN, "")
260
261 committed := false
262 defer func() {
263 if !committed {
264 if err := tx.Rollback(); err != nil {
265 opts.LogTransactionControl(storepb.TaskRunLog_TransactionControl_ROLLBACK, err.Error())
266 } else {
267 opts.LogTransactionControl(storepb.TaskRunLog_TransactionControl_ROLLBACK, "")
268 }
269 }
270 }()
271
272 // To submit a variable number of SQL statements in the statement field, set MULTI_STATEMENT_COUNT to 0."
273 // https://docs.snowflake.com/en/developer-guide/sql-api/submitting-multiple-statements
274 mctx := snow.WithMultiStatement(ctx, 0 /* MULTI_STATEMENT_COUNT */)
275
276 // Log the entire multi-statement execution
277 opts.LogCommandExecute(&storepb.Range{Start: 0, End: int32(len(statement))}, statement)
278
279 result, err := tx.ExecContext(mctx, statement)
280 if err != nil {
281 opts.LogCommandResponse(0, nil, err.Error())
282 return 0, err
283 }
284
285 if err := tx.Commit(); err != nil {
286 opts.LogTransactionControl(storepb.TaskRunLog_TransactionControl_COMMIT, err.Error())
287 return 0, err
288 }
289 opts.LogTransactionControl(storepb.TaskRunLog_TransactionControl_COMMIT, "")
290 committed = true
291
292 rowsAffected, err := result.RowsAffected()
293 // Since we cannot differentiate DDL and DML yet, we have to ignore the error.
294 if err != nil {
295 slog.Debug("rowsAffected returns error", log.BBError(err))
296 opts.LogCommandResponse(0, nil, "")
297 return 0, nil
298 }
299 opts.LogCommandResponse(rowsAffected, nil, "")
300 return rowsAffected, nil
301}
302
303// executeInAutoCommitMode executes statements with autocommit enabled (no explicit transaction)
304func (d *Driver) executeInAutoCommitMode(ctx context.Context, statement string, opts db.ExecuteOptions) (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 6

BBErrorFunction · 0.92
LogTransactionControlMethod · 0.80
LogCommandExecuteMethod · 0.80
LogCommandResponseMethod · 0.80
DebugMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected