github.com/snowflakedb/gosnowflake@v1.9.0/transaction.go (about) 1 // Copyright (c) 2017-2022 Snowflake Computing Inc. All rights reserved. 2 3 package gosnowflake 4 5 import ( 6 "context" 7 "database/sql/driver" 8 "errors" 9 ) 10 11 type snowflakeTx struct { 12 sc *snowflakeConn 13 ctx context.Context 14 } 15 16 type txCommand int 17 18 const ( 19 commit txCommand = iota 20 rollback 21 ) 22 23 func (cmd txCommand) string() (string, error) { 24 switch cmd { 25 case commit: 26 return "COMMIT", nil 27 case rollback: 28 return "ROLLBACK", nil 29 } 30 return "", errors.New("unsupported transaction command") 31 } 32 33 func (tx *snowflakeTx) Commit() error { 34 return tx.execTxCommand(commit) 35 } 36 37 func (tx *snowflakeTx) Rollback() error { 38 return tx.execTxCommand(rollback) 39 } 40 41 func (tx *snowflakeTx) execTxCommand(command txCommand) (err error) { 42 txStr, err := command.string() 43 if err != nil { 44 return 45 } 46 if tx.sc == nil || tx.sc.rest == nil { 47 return driver.ErrBadConn 48 } 49 _, err = tx.sc.exec(tx.ctx, txStr, false /* noResult */, false /* isInternal */, false /* describeOnly */, nil) 50 if err != nil { 51 return 52 } 53 tx.sc = nil 54 return 55 }