-
Notifications
You must be signed in to change notification settings - Fork 0
/
raptor.go
404 lines (317 loc) · 8.59 KB
/
raptor.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Package raptor provides a simple and easy-to-use interface for working with SQLite3 databases.
package raptor
import (
"context"
"database/sql"
"errors"
"fmt"
"io"
"sync"
"sync/atomic"
_ "modernc.org/sqlite"
)
const (
// DriverName is the name of the SQLite3 driver.
DriverName = "sqlite"
)
var (
connID atomic.Uint64
)
// New opens a new database connection
func New(source string) (*Conn, error) {
db, err := sql.Open(DriverName, source)
if err != nil {
return nil, err
}
c := &Conn{
db: db,
id: connID.Add(1),
queryLog: new(atomic.Pointer[logger]),
}
c.SetLogger(NewNoopQueryLogger())
return c, nil
}
// Conn represents a connection to a SQLite3 database.
type Conn struct {
id uint64 // Connection id
sp atomic.Uint64 // Savepoint id
db *sql.DB // Underlying database connection
queryLog *atomic.Pointer[logger] // Log query
}
// Close the database connection and perform any necessary cleanup
//
// Once close is called, new queries will be rejected.
// Close will block until all outstanding queries have completed.
func (c *Conn) Close() error {
return c.db.Close()
}
type logger struct {
QueryLogger
}
func (c *Conn) SetLogger(l QueryLogger) {
c.queryLog.Store(&logger{l})
}
// SetLogger assigns a logger instance to the connection.
func (c *Conn) SetQueryLogger(l QueryLogger) {
c.queryLog.Store(&logger{l})
}
func (c *Conn) queryLogger() QueryLogger {
return c.queryLog.Load().QueryLogger
}
// Ping verifies a connection to the database is still alive, establishing a connection if necessary.
func (c *Conn) Ping(ctx context.Context) error {
return c.db.PingContext(ctx)
}
// QueryLogger provides a standard interface for logging all SQL queries sent to Raptor
type QueryLogger interface {
LogQuery(context.Context, string, []any)
}
// NewQueryLogger creates a new QueryLogger that logs queries to an io.Writer.
func NewQueryLogger(w io.Writer) QueryLogger {
return &wQueryLogger{w}
}
type wQueryLogger struct {
w io.Writer
}
func (w *wQueryLogger) LogQuery(_ context.Context, q string, _ []any) {
fmt.Fprintln(w.w, q)
}
type noopQueryLogger struct{}
// NewNoopQueryLogger creates a new QueryLogger that doesn't log any queries.
func NewNoopQueryLogger() QueryLogger {
return &noopQueryLogger{}
}
func (w *noopQueryLogger) LogQuery(context.Context, string, []any) {}
// A Result summarizes an executed SQL command.
type Result interface {
sql.Result
}
// Rows is the result of a query. See sql.Rows for more information.
type Rows struct {
*sql.Rows
}
var (
ErrNoRows = sql.ErrNoRows
)
// Row is the result of calling QueryRow to select a single row.
type Row interface {
Scanner
Err() error
}
type connRow struct {
rows *sql.Rows
err error
}
func (r *connRow) Scan(dest ...any) error {
if r.err != nil {
return r.err
}
defer r.rows.Close()
for _, dp := range dest {
if _, ok := dp.(*sql.RawBytes); ok {
return errors.New("raptor: RawBytes isn't allowed on Row.Scan")
}
}
if !r.rows.Next() {
if err := r.rows.Err(); err != nil {
return err
}
return ErrNoRows
}
err := r.rows.Scan(dest...)
if err != nil {
return err
}
// Make sure the query can be processed to completion with no errors.
return r.rows.Close()
}
func (r *connRow) Err() error {
return r.err
}
func (r *connRow) Columns() ([]string, error) {
if r.err != nil {
return nil, r.err
}
return r.rows.Columns()
}
type Scanner interface {
Scan(...any) error
Columns() ([]string, error)
}
// Executor defines an interface for executing queries that don't return rows.
type Executor interface {
Exec(context.Context, string, ...any) (Result, error)
}
// Exec perform a query on the database. It will not return any rows. e.g. insert or delete
func (c *Conn) Exec(ctx context.Context, query string, args ...any) (Result, error) {
return c.exec(ctx, query, args...)
}
func (c *Conn) exec(ctx context.Context, query string, args ...any) (Result, error) {
c.queryLogger().LogQuery(ctx, query, args)
r, err := c.db.ExecContext(ctx, query, args...)
return Result(r), err
}
// Querier defines an interface for executing queries that return rows from the database.
type Querier interface {
Query(context.Context, string, ...any) (*Rows, error)
QueryRow(context.Context, string, ...any) Row
}
func (c *Conn) Query(ctx context.Context, query string, args ...any) (*Rows, error) {
return c.query(ctx, query, args)
}
func (c *Conn) query(ctx context.Context, query string, args []any) (*Rows, error) {
c.queryLogger().LogQuery(ctx, query, args)
r, err := c.db.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
return &Rows{r}, nil
}
func (c *Conn) QueryRow(ctx context.Context, query string, args ...any) Row {
return c.queryRow(ctx, query, args)
}
func (c *Conn) queryRow(ctx context.Context, query string, args []any) Row {
c.queryLogger().LogQuery(ctx, query, args)
r, err := c.db.QueryContext(ctx, query, args...)
return &connRow{rows: r, err: err}
}
func (c *Conn) newSavepointName() string {
return fmt.Sprintf("tx_%d_%d", c.id, c.sp.Add(1))
}
// TxRollbackError is returned when a transaction is rolled back and the rollback also returns an error.
type TxRollbackError struct {
Underlying error
Rollback error
}
func (e *TxRollbackError) Error() string {
return fmt.Sprintf("rollback error: %s; rollback error: %s", e.Underlying, e.Rollback)
}
// TxBroker defines an interface for performing a transaction.
type TxBroker interface {
Transact(context.Context, func(DB) error) error
}
// DB defines a standard set of interfaces that allow CRUD operations on a database.
type DB interface {
Executor
Querier
TxBroker
}
var _ DB = (*Conn)(nil)
var _ DB = (*txConn)(nil)
func (c *Conn) Transact(ctx context.Context, fn func(DB) error) error {
return c.transact(ctx, 0, fn)
}
func (c *Conn) transact(ctx context.Context, depth int, fn func(DB) error) error {
savepoint := c.newSavepointName()
txConn := &txConn{
conn: c,
depth: depth,
name: savepoint,
state: txStateInit,
}
if err := txConn.begin(ctx); err != nil {
return err
}
defer func() {
if p := recover(); p != nil {
_ = txConn.rollback(ctx)
panic(p)
}
}()
if err := fn(txConn); err != nil {
if rErr := txConn.rollback(ctx); rErr != nil {
return &TxRollbackError{Underlying: err, Rollback: rErr}
}
if errors.Is(err, ErrTxRollback) {
return nil
}
return err
}
return txConn.commit(ctx)
}
const (
txStateInit uint8 = iota
txStateRunning
txStateCommitted
txStateRollbacked
)
type txConn struct {
conn *Conn
depth int
mu sync.Mutex
name string
state uint8
}
var (
ErrTransactionAlreadyStarted = errors.New("transaction already started")
ErrTransactionNotRunning = errors.New("transaction not running")
ErrTxRollback = errors.New("transaction rollback") // Can be returned from a transaction to rollback the transaction. Will not be returned to the caller
)
func (t *txConn) begin(ctx context.Context) error {
t.mu.Lock()
defer t.mu.Unlock()
if t.state != txStateInit {
return ErrTransactionAlreadyStarted
}
_, err := t.conn.exec(ctx, "SAVEPOINT "+t.name+";")
if err == nil {
t.state = 1
}
return err
}
func (t *txConn) rollback(ctx context.Context) error {
t.mu.Lock()
defer t.mu.Unlock()
if t.state != txStateRunning {
return nil
}
_, err := t.conn.exec(ctx, "ROLLBACK TRANSACTION TO SAVEPOINT "+t.name+";")
if err == nil {
t.state = txStateRollbacked
}
return nil
}
func (t *txConn) commit(ctx context.Context) error {
t.mu.Lock()
defer t.mu.Unlock()
if t.state != txStateRunning {
return nil
}
_, err := t.conn.exec(ctx, "RELEASE SAVEPOINT "+t.name+";")
if err == nil {
t.state = txStateCommitted
}
return err
}
func (t *txConn) Exec(ctx context.Context, query string, args ...any) (Result, error) {
t.mu.Lock()
defer t.mu.Unlock()
if t.state != txStateRunning {
return nil, ErrTransactionNotRunning
}
return t.conn.exec(ctx, query, args...)
}
func (t *txConn) Transact(ctx context.Context, fn func(DB) error) error {
t.mu.Lock()
defer t.mu.Unlock()
if t.state != txStateRunning {
return ErrTransactionNotRunning
}
return t.conn.transact(ctx, t.depth+1, fn)
}
func (t *txConn) Query(ctx context.Context, query string, args ...any) (*Rows, error) {
t.mu.Lock()
defer t.mu.Unlock()
if t.state != txStateRunning {
return nil, ErrTransactionNotRunning
}
return t.conn.query(ctx, query, args)
}
func (t *txConn) QueryRow(ctx context.Context, query string, args ...any) Row {
t.mu.Lock()
defer t.mu.Unlock()
if t.state != txStateRunning {
return &connRow{rows: nil, err: ErrTransactionNotRunning}
}
return t.conn.queryRow(ctx, query, args)
}