Skip to content

Commit

Permalink
Driver savepoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
ncruces committed Nov 7, 2023
1 parent 6b28be6 commit c00927e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
6 changes: 6 additions & 0 deletions driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,12 @@ func (c *conn) ExecContext(ctx context.Context, query string, args []driver.Name
return nil, driver.ErrSkip
}

if savept, ok := ctx.(*saveptCtx); ok {
// Called from driver.Savepoint.
savept.Savepoint = c.Savepoint()
return resultRowsAffected(-1), nil
}

old := c.Conn.SetInterrupt(ctx)
defer c.Conn.SetInterrupt(old)

Expand Down
27 changes: 27 additions & 0 deletions driver/savepoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package driver

import (
"database/sql"
"time"

"github.com/ncruces/go-sqlite3"
)

// Savepoint establishes a new transaction savepoint.
//
// https://www.sqlite.org/lang_savepoint.html
func Savepoint(tx *sql.Tx) sqlite3.Savepoint {
var ctx saveptCtx
tx.ExecContext(&ctx, "")
return ctx.Savepoint
}

type saveptCtx struct{ sqlite3.Savepoint }

func (*saveptCtx) Deadline() (deadline time.Time, ok bool) { return }

func (*saveptCtx) Done() <-chan struct{} { return nil }

func (*saveptCtx) Err() error { return nil }

func (*saveptCtx) Value(key any) any { return nil }
1 change: 0 additions & 1 deletion ext/blob/blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func Example() {
if err != nil {
log.Fatal(err)
}
defer os.Remove("demo.db")
defer db.Close()

_, err = db.Exec(`CREATE TABLE IF NOT EXISTS test (col)`)
Expand Down
33 changes: 23 additions & 10 deletions tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/rand"
"runtime"
"strconv"
"strings"
)

// Tx is an in-progress database transaction.
Expand Down Expand Up @@ -119,17 +120,8 @@ type Savepoint struct {
//
// https://www.sqlite.org/lang_savepoint.html
func (c *Conn) Savepoint() Savepoint {
name := "sqlite3.Savepoint"
var pc [1]uintptr
if n := runtime.Callers(2, pc[:]); n > 0 {
frames := runtime.CallersFrames(pc[:n])
frame, _ := frames.Next()
if frame.Function != "" {
name = frame.Function
}
}
// Names can be reused; this makes catching bugs more likely.
name += "#" + strconv.Itoa(int(rand.Int31()))
name := saveptName() + "_" + strconv.Itoa(int(rand.Int31()))

err := c.txExecInterrupted(fmt.Sprintf("SAVEPOINT %q;", name))
if err != nil {
Expand All @@ -138,6 +130,27 @@ func (c *Conn) Savepoint() Savepoint {
return Savepoint{c: c, name: name}
}

func saveptName() (name string) {
defer func() {
if name == "" {
name = "sqlite3.Savepoint"
}
}()

var pc [8]uintptr
n := runtime.Callers(3, pc[:])
if n <= 0 {
return ""
}
frames := runtime.CallersFrames(pc[:n])
frame, more := frames.Next()
for more && (strings.HasPrefix(frame.Function, "database/sql.") ||
strings.HasPrefix(frame.Function, "github.com/ncruces/go-sqlite3/driver.")) {
frame, more = frames.Next()
}
return frame.Function
}

// Release releases the savepoint rolling back any changes
// if *error points to a non-nil error.
//
Expand Down

0 comments on commit c00927e

Please sign in to comment.