-
Notifications
You must be signed in to change notification settings - Fork 85
/
transaction.go
101 lines (81 loc) · 2.52 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
package query
import (
"context"
"github.com/genjidb/genji/internal/database"
"github.com/genjidb/genji/internal/errors"
"github.com/genjidb/genji/internal/query/statement"
)
// BeginStmt is a statement that creates a new transaction.
type BeginStmt struct {
Writable bool
}
// Prepare implements the Preparer interface.
func (stmt BeginStmt) Prepare(*statement.Context) (statement.Statement, error) {
return stmt, nil
}
func (stmt BeginStmt) alterQuery(ctx context.Context, db *database.Database, q *Query) error {
if q.tx != nil {
return errors.New("cannot begin a transaction within a transaction")
}
var err error
q.tx, err = db.BeginTx(ctx, &database.TxOptions{
ReadOnly: !stmt.Writable,
Attached: true,
})
q.autoCommit = false
return err
}
func (stmt BeginStmt) IsReadOnly() bool {
return !stmt.Writable
}
func (stmt BeginStmt) Run(ctx *statement.Context) (statement.Result, error) {
return statement.Result{}, errors.New("cannot begin a transaction within a transaction")
}
// RollbackStmt is a statement that rollbacks the current active transaction.
type RollbackStmt struct{}
// Prepare implements the Preparer interface.
func (stmt RollbackStmt) Prepare(*statement.Context) (statement.Statement, error) {
return stmt, nil
}
func (stmt RollbackStmt) alterQuery(ctx context.Context, db *database.Database, q *Query) error {
if q.tx == nil || q.autoCommit {
return errors.New("cannot rollback with no active transaction")
}
err := q.tx.Rollback()
if err != nil {
return err
}
q.tx = nil
q.autoCommit = true
return nil
}
func (stmt RollbackStmt) IsReadOnly() bool {
return false
}
func (stmt RollbackStmt) Run(ctx *statement.Context) (statement.Result, error) {
return statement.Result{}, errors.New("cannot rollback with no active transaction")
}
// CommitStmt is a statement that commits the current active transaction.
type CommitStmt struct{}
// Prepare implements the Preparer interface.
func (stmt CommitStmt) Prepare(*statement.Context) (statement.Statement, error) {
return stmt, nil
}
func (stmt CommitStmt) alterQuery(ctx context.Context, db *database.Database, q *Query) error {
if q.tx == nil || q.autoCommit {
return errors.New("cannot commit with no active transaction")
}
err := q.tx.Commit()
if err != nil {
return err
}
q.tx = nil
q.autoCommit = true
return nil
}
func (stmt CommitStmt) IsReadOnly() bool {
return false
}
func (stmt CommitStmt) Run(ctx *statement.Context) (statement.Result, error) {
return statement.Result{}, errors.New("cannot commit with no active transaction")
}