forked from snowflakedb/gosnowflake
/
transaction.go
55 lines (46 loc) · 1013 Bytes
/
transaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (c) 2017-2022 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"context"
"database/sql/driver"
"errors"
)
type snowflakeTx struct {
sc *snowflakeConn
ctx context.Context
}
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
}
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
}
if tx.sc == nil || tx.sc.rest == nil {
return driver.ErrBadConn
}
_, err = tx.sc.exec(tx.ctx, txStr, false /* noResult */, false /* isInternal */, false /* describeOnly */, nil)
if err != nil {
return
}
tx.sc = nil
return
}