Skip to content

Commit

Permalink
SNOW-828186 Remove code duplication in transaction.go (#807)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-dheyman authored May 29, 2023
1 parent cd0f451 commit f8a152c
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,48 @@ package gosnowflake
import (
"context"
"database/sql/driver"
"errors"
)

type snowflakeTx struct {
sc *snowflakeConn
ctx context.Context
}

func (tx *snowflakeTx) Commit() (err error) {
if tx.sc == nil || tx.sc.rest == nil {
return driver.ErrBadConn
type txCommand int

const (
commit txCommand = iota
rollback
)

func (cmd txCommand) string() (string, error) {
switch cmd {
case commit:
return "COMMIT", nil
case rollback:
return "ROLLBACK", nil
}
_, err = tx.sc.exec(tx.ctx, "COMMIT", false /* noResult */, false /* isInternal */, false /* describeOnly */, nil)
return "", errors.New("unsupported transaction command")
}

func (tx *snowflakeTx) Commit() error {
return tx.execTxCommand(commit)
}

func (tx *snowflakeTx) Rollback() error {
return tx.execTxCommand(rollback)
}

func (tx *snowflakeTx) execTxCommand(command txCommand) (err error) {
txStr, err := command.string()
if err != nil {
return
}
tx.sc = nil
return
}

func (tx *snowflakeTx) Rollback() (err error) {
if tx.sc == nil || tx.sc.rest == nil {
return driver.ErrBadConn
}
_, err = tx.sc.exec(tx.ctx, "ROLLBACK", false /* noResult */, false /* isInternal */, false /* describeOnly */, nil)
_, err = tx.sc.exec(tx.ctx, txStr, false /* noResult */, false /* isInternal */, false /* describeOnly */, nil)
if err != nil {
return
}
Expand Down

0 comments on commit f8a152c

Please sign in to comment.