-
Notifications
You must be signed in to change notification settings - Fork 25
/
sqalx.go
232 lines (194 loc) · 5.48 KB
/
sqalx.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package sqalx
import (
"context"
"database/sql"
"errors"
"strings"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
)
var (
// ErrNotInTransaction is returned when using Commit
// outside of a transaction.
ErrNotInTransaction = errors.New("not in transaction")
// ErrIncompatibleOption is returned when using an option incompatible
// with the selected driver.
ErrIncompatibleOption = errors.New("incompatible option")
)
// A Node is a database driver that can manage nested transactions.
type Node interface {
Driver
// Close the underlying sqlx connection.
Close() error
// Begin a new transaction.
Beginx() (Node, error)
// Begin a new transaction using the provided context and options.
// Note that the provided parameters are only used when opening a new transaction,
// not on nested ones.
BeginTxx(ctx context.Context, opts *sql.TxOptions) (Node, error)
// Rollback the associated transaction.
Rollback() error
// Commit the assiociated transaction.
Commit() error
// Tx returns the underlying transaction.
Tx() *sqlx.Tx
}
// A Driver can query the database. It can either be a *sqlx.DB or a *sqlx.Tx
// and therefore is limited to the methods they have in common.
type Driver interface {
sqlx.Execer
sqlx.ExecerContext
sqlx.Queryer
sqlx.QueryerContext
sqlx.Preparer
sqlx.PreparerContext
BindNamed(query string, arg interface{}) (string, []interface{}, error)
DriverName() string
Get(dest interface{}, query string, args ...interface{}) error
GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
MustExec(query string, args ...interface{}) sql.Result
MustExecContext(ctx context.Context, query string, args ...interface{}) sql.Result
NamedExec(query string, arg interface{}) (sql.Result, error)
NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error)
NamedQuery(query string, arg interface{}) (*sqlx.Rows, error)
PrepareNamed(query string) (*sqlx.NamedStmt, error)
PrepareNamedContext(ctx context.Context, query string) (*sqlx.NamedStmt, error)
Preparex(query string) (*sqlx.Stmt, error)
PreparexContext(ctx context.Context, query string) (*sqlx.Stmt, error)
QueryRow(string, ...interface{}) *sql.Row
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
Rebind(query string) string
Select(dest interface{}, query string, args ...interface{}) error
SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
}
// New creates a new Node with the given DB.
func New(db *sqlx.DB, options ...Option) (Node, error) {
n := node{
db: db,
Driver: db,
}
for _, opt := range options {
err := opt(&n)
if err != nil {
return nil, err
}
}
return &n, nil
}
// NewFromTransaction creates a new Node from the given transaction.
func NewFromTransaction(tx *sqlx.Tx, options ...Option) (Node, error) {
n := node{
tx: tx,
Driver: tx,
}
for _, opt := range options {
err := opt(&n)
if err != nil {
return nil, err
}
}
return &n, nil
}
// Connect to a database.
func Connect(driverName, dataSourceName string, options ...Option) (Node, error) {
db, err := sqlx.Connect(driverName, dataSourceName)
if err != nil {
return nil, err
}
node, err := New(db, options...)
if err != nil {
// the connection has been opened within this function, we must close it
// on error.
db.Close()
return nil, err
}
return node, nil
}
type node struct {
Driver
db *sqlx.DB
tx *sqlx.Tx
savePointID string
savePointEnabled bool
nested bool
}
func (n *node) Close() error {
return n.db.Close()
}
func (n node) Beginx() (Node, error) {
return n.BeginTxx(context.Background(), nil)
}
func (n node) BeginTxx(ctx context.Context, opts *sql.TxOptions) (Node, error) {
var err error
switch {
case n.tx == nil:
// new actual transaction
n.tx, err = n.db.BeginTxx(ctx, opts)
n.Driver = n.tx
case n.savePointEnabled:
// already in a transaction: using savepoints
n.nested = true
// savepoints name must start with a char and cannot contain dashes (-)
n.savePointID = "sp_" + strings.Replace(uuid.NewString(), "-", "_", -1)
_, err = n.tx.Exec("SAVEPOINT " + n.savePointID)
default:
// already in a transaction: reusing current transaction
n.nested = true
}
if err != nil {
return nil, err
}
return &n, nil
}
func (n *node) Rollback() error {
if n.tx == nil {
return nil
}
var err error
if n.savePointEnabled && n.savePointID != "" {
_, err = n.tx.Exec("ROLLBACK TO SAVEPOINT " + n.savePointID)
} else if !n.nested {
err = n.tx.Rollback()
}
if err != nil {
return err
}
n.tx = nil
n.Driver = nil
return nil
}
func (n *node) Commit() error {
if n.tx == nil {
return ErrNotInTransaction
}
var err error
if n.savePointID != "" {
_, err = n.tx.Exec("RELEASE SAVEPOINT " + n.savePointID)
} else if !n.nested {
err = n.tx.Commit()
}
if err != nil {
return err
}
n.tx = nil
n.Driver = nil
return nil
}
// Tx returns the underlying transaction.
func (n *node) Tx() *sqlx.Tx {
return n.tx
}
// Option to configure sqalx
type Option func(*node) error
// SavePoint option enables PostgreSQL and SQLite Savepoints for nested
// transactions.
func SavePoint(enabled bool) Option {
return func(n *node) error {
driverName := n.Driver.DriverName()
if enabled && driverName != "postgres" && driverName != "pgx" && driverName != "pgx/v5" && driverName != "sqlite3" && driverName != "mysql" {
return ErrIncompatibleOption
}
n.savePointEnabled = enabled
return nil
}
}