-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtransaction.go
119 lines (98 loc) · 3.21 KB
/
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// SPDX-FileCopyrightText: 2020 SAP SE
// SPDX-FileCopyrightText: 2021 SAP SE
// SPDX-FileCopyrightText: 2022 SAP SE
// SPDX-FileCopyrightText: 2023 SAP SE
//
// SPDX-License-Identifier: Apache-2.0
package ase
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"github.com/SAP/go-dblib"
"github.com/SAP/go-dblib/tds"
)
// Interface satisfaction checks.
var (
_ driver.ConnBeginTx = (*Conn)(nil)
_ driver.Tx = (*Transaction)(nil)
)
// DefaultTxOptions returns default driver.TxOptions.
func DefaultTxOptions() driver.TxOptions {
return driver.TxOptions{
Isolation: driver.IsolationLevel(sql.LevelDefault),
ReadOnly: false,
}
}
// Transaction implements the driver.Tx interface.
type Transaction struct {
conn *Conn
name string
}
// Name returns the name of the transaction.
func (tx Transaction) Name() string {
return tx.name
}
// Begin implements the driver.Conn interface.
func (c *Conn) Begin() (driver.Tx, error) {
return c.BeginTx(context.Background(), DefaultTxOptions())
}
// BeginTx implements the driver.ConnBeginTx interface.
func (c *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
return c.NewTransaction(ctx, opts, "")
}
// NewTransaction creates a new transaction.
func (c *Conn) NewTransaction(ctx context.Context, opts driver.TxOptions, name string) (*Transaction, error) {
tx := &Transaction{
conn: c,
name: name,
}
return tx, tx.begin(ctx, opts)
}
func (tx Transaction) begin(ctx context.Context, opts driver.TxOptions) error {
if opts.ReadOnly {
return errors.New("go-ase: ASE does not support read-only transactions")
}
isolationLvl, err := dblib.ASEIsolationLevelFromGo(sql.IsolationLevel(opts.Isolation))
if err != nil {
return fmt.Errorf("go-ase: error mapping sql.IsolationLevel to ASE isolation level: %w", err)
}
if isolationLvl == dblib.ASELevelInvalid {
return fmt.Errorf("go-ase: sql.IsolationLevel %s has no equivalent ASE isolation level", sql.IsolationLevel(opts.Isolation))
}
if _, _, err := tx.conn.GenericExec(ctx, "begin transaction "+tx.name, nil); err != nil {
return fmt.Errorf("go-ase: error initializing transaction: %w", err)
}
optIsolationPkg := &tds.OptionCmdPackage{
Cmd: tds.TDS_OPT_SET,
Option: tds.TDS_OPT_ISOLATION,
OptionArg: []byte{byte(isolationLvl)},
}
if err := tx.conn.Channel.QueuePackage(ctx, optIsolationPkg); err != nil {
return fmt.Errorf("go-ase: error queueing package: %w", err)
}
return nil
}
// NewTransaction creates a new transaction.
func (tx Transaction) NewTransaction(ctx context.Context, opts driver.TxOptions) (*Transaction, error) {
newTx := &Transaction{
conn: tx.conn,
}
return newTx, newTx.begin(ctx, opts)
}
// Commit implements the driver.Tx interface.
func (tx Transaction) Commit() error {
if _, _, err := tx.conn.GenericExec(context.Background(), "commit "+tx.name, nil); err != nil {
return fmt.Errorf("go-ase: error committing transaction: %w", err)
}
return nil
}
// Rollback implements the driver.Tx interface.
func (tx Transaction) Rollback() error {
if _, _, err := tx.conn.GenericExec(context.Background(), "rollback "+tx.name, nil); err != nil {
return fmt.Errorf("go-ase: error rolling back transaction: %w", err)
}
return nil
}