executeInTransactionMode executes statements within a single transaction
(ctx context.Context, statement string, opts db.ExecuteOptions)
| 251 | |
| 252 | // executeInTransactionMode executes statements within a single transaction |
| 253 | func (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) |
| 304 | func (d *Driver) executeInAutoCommitMode(ctx context.Context, statement string, opts db.ExecuteOptions) (int64, error) { |
no test coverage detected