-
Notifications
You must be signed in to change notification settings - Fork 95
/
db.go
547 lines (456 loc) · 11 KB
/
db.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
package chai implements an embedded SQL database.
*/
package chai
import (
"bufio"
"bytes"
"context"
"database/sql"
"database/sql/driver"
"io"
"github.com/chaisql/chai/internal/database"
"github.com/chaisql/chai/internal/database/catalogstore"
"github.com/chaisql/chai/internal/environment"
errs "github.com/chaisql/chai/internal/errors"
"github.com/chaisql/chai/internal/query"
"github.com/chaisql/chai/internal/query/statement"
"github.com/chaisql/chai/internal/row"
"github.com/chaisql/chai/internal/sql/parser"
"github.com/chaisql/chai/internal/stream"
"github.com/chaisql/chai/internal/types"
"github.com/cockroachdb/errors"
)
// DB represents a collection of tables.
type DB struct {
DB *database.Database
ctx context.Context
}
// Open creates a Chai database at the given path.
// If path is equal to ":memory:" it will open an in-memory database,
// otherwise it will create an on-disk database.
func Open(path string) (*DB, error) {
db, err := database.Open(path, &database.Options{
CatalogLoader: catalogstore.LoadCatalog,
})
if err != nil {
return nil, err
}
return &DB{
DB: db,
}, nil
}
func (db *DB) Connect() (*Connection, error) {
conn, err := db.DB.Connect()
if err != nil {
return nil, err
}
return &Connection{
db: db,
Conn: conn,
}, nil
}
// WithContext creates a new database handle using the given context for every operation.
func (db DB) WithContext(ctx context.Context) *DB {
db.ctx = ctx
return &db
}
func (db *DB) withConn(fn func(*Connection) error) error {
conn, err := db.Connect()
if err != nil {
return err
}
defer conn.Close()
return fn(conn)
}
// QueryRow runs the query and returns the first row.
func (db *DB) QueryRow(q string, args ...any) (r *Row, err error) {
err = db.withConn(func(c *Connection) error {
r, err = c.QueryRow(q, args...)
return err
})
return
}
// Exec a query against the database without returning the result.
func (db *DB) Exec(q string, args ...any) error {
return db.withConn(func(c *Connection) error {
return c.Exec(q, args...)
})
}
// Close the database.
func (db *DB) Close() error {
return db.DB.Close()
}
type Connection struct {
db *DB
Conn *database.Connection
}
// Begin starts a new transaction.
// The returned transaction must be closed either by calling Rollback or Commit.
func (c *Connection) Begin(writable bool) (*Tx, error) {
_, err := c.Conn.BeginTx(&database.TxOptions{
ReadOnly: !writable,
})
if err != nil {
return nil, err
}
return &Tx{
conn: c,
}, nil
}
// View starts a read only transaction, runs fn and automatically rolls it back.
func (c *Connection) View(fn func(tx *Tx) error) error {
tx, err := c.Begin(false)
if err != nil {
return err
}
defer tx.Rollback()
return fn(tx)
}
// Update starts a read-write transaction, runs fn and automatically commits it.
func (c *Connection) Update(fn func(tx *Tx) error) error {
tx, err := c.Begin(true)
if err != nil {
return err
}
defer tx.Rollback()
err = fn(tx)
if err != nil {
return err
}
return tx.Commit()
}
// Query the database and return the result.
// The returned result must always be closed after usage.
func (c *Connection) Query(q string, args ...any) (*Result, error) {
stmt, err := c.Prepare(q)
if err != nil {
return nil, err
}
res, err := stmt.Query(args...)
if err != nil {
return nil, err
}
res.conn = c
return res, nil
}
// QueryRow runs the query and returns the first row.
func (c *Connection) QueryRow(q string, args ...any) (*Row, error) {
stmt, err := c.Prepare(q)
if err != nil {
return nil, err
}
return stmt.QueryRow(args...)
}
// Exec a query against the database without returning the result.
func (c *Connection) Exec(q string, args ...any) error {
stmt, err := c.Prepare(q)
if err != nil {
return err
}
return stmt.Exec(args...)
}
// Prepare parses the query and returns a prepared statement.
func (c *Connection) Prepare(q string) (*Statement, error) {
pq, err := parser.ParseQuery(q)
if err != nil {
return nil, err
}
err = pq.Prepare(newQueryContext(c, nil))
if err != nil {
return nil, err
}
return &Statement{
pq: pq,
conn: c,
}, nil
}
func (c *Connection) Close() error {
return c.Conn.Close()
}
// Tx represents a database transaction. It provides methods for managing the
// collection of tables and the transaction itself.
// Tx is either read-only or read/write. Read-only can be used to read tables
// and read/write can be used to read, create, delete and modify tables.
type Tx struct {
conn *Connection
}
// Rollback the transaction. Can be used safely after commit.
func (tx *Tx) Rollback() error {
t := tx.conn.Conn.GetTx()
if t == nil {
return errors.New("transaction has already been committed or rolled back")
}
return t.Rollback()
}
// Commit the transaction. Calling this method on read-only transactions
// will return an error.
func (tx *Tx) Commit() error {
t := tx.conn.Conn.GetTx()
if t == nil {
return errors.New("transaction has already been committed or rolled back")
}
return t.Commit()
}
// Query the database withing the transaction and returns the result.
// Closing the returned result after usage is not mandatory.
func (tx *Tx) Query(q string, args ...any) (*Result, error) {
stmt, err := tx.Prepare(q)
if err != nil {
return nil, err
}
return stmt.Query(args...)
}
// QueryRow runs the query and returns the first row.
func (tx *Tx) QueryRow(q string, args ...any) (*Row, error) {
stmt, err := tx.Prepare(q)
if err != nil {
return nil, err
}
return stmt.QueryRow(args...)
}
// Exec a query against the database within tx and without returning the result.
func (tx *Tx) Exec(q string, args ...any) (err error) {
stmt, err := tx.Prepare(q)
if err != nil {
return err
}
return stmt.Exec(args...)
}
// Prepare parses the query and returns a prepared statement.
func (tx *Tx) Prepare(q string) (*Statement, error) {
pq, err := parser.ParseQuery(q)
if err != nil {
return nil, err
}
err = pq.Prepare(newQueryContext(tx.conn, nil))
if err != nil {
return nil, err
}
return &Statement{
pq: pq,
conn: tx.conn,
tx: tx,
}, nil
}
// Statement is a prepared statement. If Statement has been created on a Tx,
// it will only be valid until Tx closes. If it has been created on a DB, it
// is valid until the DB closes.
// It's safe for concurrent use by multiple goroutines.
type Statement struct {
pq query.Query
conn *Connection
tx *Tx
}
// Query the database and return the result.
// The returned result must always be closed after usage.
func (s *Statement) Query(args ...any) (*Result, error) {
var r *statement.Result
var err error
r, err = s.pq.Run(newQueryContext(s.conn, argsToParams(args)))
if err != nil {
return nil, err
}
return &Result{result: r, ctx: s.conn.db.ctx}, nil
}
func argsToParams(args []interface{}) []environment.Param {
nv := make([]environment.Param, len(args))
for i := range args {
switch t := args[i].(type) {
case sql.NamedArg:
nv[i].Name = t.Name
nv[i].Value = t.Value
case *sql.NamedArg:
nv[i].Name = t.Name
nv[i].Value = t.Value
case driver.NamedValue:
nv[i].Name = t.Name
nv[i].Value = t.Value
case *driver.NamedValue:
nv[i].Name = t.Name
nv[i].Value = t.Value
case *environment.Param:
nv[i] = *t
case environment.Param:
nv[i] = t
default:
nv[i].Value = args[i]
}
}
return nv
}
// QueryRow runs the query and returns the first row.
func (s *Statement) QueryRow(args ...any) (r *Row, err error) {
res, err := s.Query(args...)
if err != nil {
return nil, err
}
defer func() {
er := res.Close()
if err == nil {
err = er
}
}()
return res.GetFirst()
}
// Exec a query against the database without returning the result.
func (s *Statement) Exec(args ...any) (err error) {
res, err := s.Query(args...)
if err != nil {
return err
}
defer func() {
er := res.Close()
if err == nil {
err = er
}
}()
return res.Iterate(func(*Row) error {
return nil
})
}
// Result of a query.
type Result struct {
result *statement.Result
ctx context.Context
conn *Connection
}
func (r *Result) Iterate(fn func(r *Row) error) error {
var row Row
if r.ctx == nil {
return r.result.Iterate(func(dr database.Row) error {
row.Row = dr
return fn(&row)
})
}
return r.result.Iterate(func(dr database.Row) error {
if err := r.ctx.Err(); err != nil {
return err
}
row.Row = dr
return fn(&row)
})
}
func (r *Result) GetFirst() (*Row, error) {
var rr *Row
err := r.Iterate(func(row *Row) error {
rr = row.Clone()
return stream.ErrStreamClosed
})
if err != nil {
return nil, err
}
if rr == nil {
return nil, errors.WithStack(errs.NewRowNotFoundError())
}
return rr, nil
}
func (r *Result) Columns() ([]string, error) {
if r.result.Iterator == nil {
return nil, nil
}
stmt, ok := r.result.Iterator.(*statement.StreamStmtIterator)
if !ok || stmt.Stream.Op == nil {
return nil, nil
}
var env environment.Environment
env.DB = stmt.Context.DB
env.Tx = stmt.Context.Tx
env.SetParams(stmt.Context.Params)
return stmt.Stream.Columns(&env)
}
// Close the result stream.
func (r *Result) Close() (err error) {
if r == nil {
return nil
}
err = r.result.Close()
return err
}
func (r *Result) MarshalJSON() ([]byte, error) {
var buf bytes.Buffer
err := r.MarshalJSONTo(&buf)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (r *Result) MarshalJSONTo(w io.Writer) error {
buf := bufio.NewWriter(w)
buf.WriteByte('[')
first := true
err := r.result.Iterate(func(r database.Row) error {
if !first {
buf.WriteString(", ")
} else {
first = false
}
data, err := r.MarshalJSON()
if err != nil {
return err
}
_, err = buf.Write(data)
return err
})
if err != nil {
return err
}
buf.WriteByte(']')
return buf.Flush()
}
func newQueryContext(conn *Connection, params []environment.Param) *query.Context {
return &query.Context{
Ctx: conn.db.ctx,
DB: conn.db.DB,
Conn: conn.Conn,
Params: params,
}
}
type Row struct {
Row database.Row
}
func (r *Row) Clone() *Row {
var rr Row
cb := row.NewColumnBuffer()
err := cb.Copy(r.Row)
if err != nil {
panic(err)
}
var br database.BasicRow
br.ResetWith(r.Row.TableName(), r.Row.Key(), cb)
rr.Row = &br
return &rr
}
func (r *Row) Columns() ([]string, error) {
var cols []string
err := r.Row.Iterate(func(column string, value types.Value) error {
cols = append(cols, column)
return nil
})
if err != nil {
return nil, err
}
return cols, nil
}
func (r *Row) GetColumnType(column string) (string, error) {
v, err := r.Row.Get(column)
if errors.Is(err, types.ErrColumnNotFound) {
return "", err
}
return v.Type().String(), err
}
func (r *Row) ScanColumn(column string, dest any) error {
return row.ScanColumn(r.Row, column, dest)
}
func (r *Row) Scan(dest ...any) error {
return row.Scan(r.Row, dest...)
}
func (r *Row) StructScan(dest any) error {
return row.StructScan(r.Row, dest)
}
func (r *Row) MapScan(dest map[string]any) error {
return row.MapScan(r.Row, dest)
}
func (r *Row) MarshalJSON() ([]byte, error) {
return r.Row.MarshalJSON()
}