Enable passing context when beginning a transaction
This commit is contained in:
parent
788d19c498
commit
dc6a357e26
6 changed files with 17 additions and 11 deletions
2
db/db.go
2
db/db.go
|
@ -83,7 +83,7 @@ func Export(w io.Writer) (err error) {
|
|||
// with the underlying database, and returns
|
||||
// the transaction, or any error which occured.
|
||||
func Begin(rw bool) (txn kvs.TX, err error) {
|
||||
return db.Begin(rw)
|
||||
return db.Begin(context.Background(), rw)
|
||||
}
|
||||
|
||||
// Socket registers a websocket for live queries
|
||||
|
|
|
@ -145,7 +145,7 @@ func (e *executor) execute(ctx context.Context, ast *sql.Query) {
|
|||
|
||||
switch stm.(type) {
|
||||
case *sql.BeginStatement:
|
||||
err = e.begin(true)
|
||||
err = e.begin(ctx, true)
|
||||
trc.Finish()
|
||||
continue
|
||||
case *sql.CancelStatement:
|
||||
|
@ -243,7 +243,7 @@ func (e *executor) operate(ctx context.Context, stm sql.Statement) (res []interf
|
|||
trw = false
|
||||
}
|
||||
|
||||
err = e.begin(trw)
|
||||
err = e.begin(ctx, trw)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -420,9 +420,9 @@ func (e *executor) operate(ctx context.Context, stm sql.Statement) (res []interf
|
|||
|
||||
}
|
||||
|
||||
func (e *executor) begin(rw bool) (err error) {
|
||||
func (e *executor) begin(ctx context.Context, rw bool) (err error) {
|
||||
if e.dbo.TX == nil {
|
||||
e.dbo.TX, err = db.Begin(rw)
|
||||
e.dbo.TX, err = db.Begin(ctx, rw)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -244,7 +244,7 @@ func (s *socket) deregister(id string) {
|
|||
|
||||
delete(sockets, id)
|
||||
|
||||
txn, _ := db.Begin(true)
|
||||
txn, _ := db.Begin(context.Background(), true)
|
||||
|
||||
defer txn.Commit()
|
||||
|
||||
|
|
|
@ -14,11 +14,14 @@
|
|||
|
||||
package kvs
|
||||
|
||||
import "io"
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
)
|
||||
|
||||
// DB represents a database implementation
|
||||
type DB interface {
|
||||
Begin(bool) (TX, error)
|
||||
Begin(context.Context, bool) (TX, error)
|
||||
Import(io.Reader) error
|
||||
Export(io.Writer) error
|
||||
Close() error
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
package kvs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
|
@ -63,8 +64,8 @@ func New(opts *cnf.Options) (ds *DS, err error) {
|
|||
// Begin begins a new read / write transaction
|
||||
// with the underlying database, and returns
|
||||
// the transaction, or any error which occured.
|
||||
func (ds *DS) Begin(writable bool) (txn TX, err error) {
|
||||
return ds.db.Begin(writable)
|
||||
func (ds *DS) Begin(ctx context.Context, writable bool) (txn TX, err error) {
|
||||
return ds.db.Begin(ctx, writable)
|
||||
}
|
||||
|
||||
// Import loads database operations from a reader.
|
||||
|
|
|
@ -17,6 +17,8 @@ package rixxdb
|
|||
import (
|
||||
"io"
|
||||
|
||||
"context"
|
||||
|
||||
"github.com/abcum/rixxdb"
|
||||
"github.com/abcum/surreal/kvs"
|
||||
)
|
||||
|
@ -25,7 +27,7 @@ type DB struct {
|
|||
pntr *rixxdb.DB
|
||||
}
|
||||
|
||||
func (db *DB) Begin(writable bool) (txn kvs.TX, err error) {
|
||||
func (db *DB) Begin(ctx context.Context, writable bool) (txn kvs.TX, err error) {
|
||||
var pntr *rixxdb.TX
|
||||
if pntr, err = db.pntr.Begin(writable); err != nil {
|
||||
err = &kvs.DBError{Err: err}
|
||||
|
|
Loading…
Reference in a new issue