Commit callback.

This commit is contained in:
Nuno Cruces
2024-01-27 10:05:31 +00:00
parent 99ad7ff766
commit c9cc893ed7
4 changed files with 53 additions and 10 deletions

View File

@@ -65,6 +65,6 @@ func (c *Conn) Limit(id LimitCategory, value int) int {
return int(int32(r))
}
func authorizerCallback(ctx context.Context, mod api.Module, pDB, action, zName3d, zName4th, zSchema, zInnerName uint32) uint32 {
func authorizerCallback(ctx context.Context, mod api.Module, pDB, action, zName3rd, zName4th, zSchema, zInnerName uint32) uint32 {
return 0
}

11
conn.go
View File

@@ -22,6 +22,8 @@ type Conn struct {
pending *Stmt
log func(code xErrorCode, msg string)
collation func(name string)
commit func() bool
rollback func()
arena arena
handle uint32
@@ -327,15 +329,6 @@ func progressCallback(ctx context.Context, mod api.Module, _ uint32) uint32 {
return 0
}
func commitCallback(ctx context.Context, mod api.Module, pDB uint32) uint32 {
return 0
}
func rollbackCallback(ctx context.Context, mod api.Module, pDB uint32) {}
func updateCallback(ctx context.Context, mod api.Module, pDB, action, zSchema, zTabName uint32, rowid uint64) {
}
// Deprecated: executes a PRAGMA statement and returns results.
func (c *Conn) Pragma(str string) ([]string, error) {
stmt, _, err := c.Prepare(`PRAGMA ` + str)

View File

@@ -18,6 +18,9 @@ func TestConn_Transaction_exec(t *testing.T) {
}
defer db.Close()
db.CommitHook(func() (ok bool) { return true })
db.RollbackHook(func() {})
err = db.Exec(`CREATE TABLE IF NOT EXISTS test (col)`)
if err != nil {
t.Fatal(err)

47
txn.go
View File

@@ -8,6 +8,8 @@ import (
"runtime"
"strconv"
"strings"
"github.com/tetratelabs/wazero/api"
)
// Txn is an in-progress database transaction.
@@ -229,3 +231,48 @@ func (c *Conn) TxnState(schema string) TxnState {
// Deprecated: renamed for consistency with [Conn.TxnState].
type Tx = Txn
// CommitHook registers a callback function to be invoked
// whenever a transaction is committed.
// Return true to allow the commit operation to continue normally.
//
// https://sqlite.org/c3ref/commit_hook.html
func (c *Conn) CommitHook(cb func() (ok bool)) {
var enable uint64
if cb != nil {
enable = 1
}
c.call("sqlite3_commit_hook_go", uint64(c.handle), enable)
c.commit = cb
}
// RollbackHook registers a callback function to be invoked
// whenever a transaction is rolled back.
//
// https://sqlite.org/c3ref/commit_hook.html
func (c *Conn) RollbackHook(cb func()) {
var enable uint64
if cb != nil {
enable = 1
}
c.call("sqlite3_rollback_hook_go", uint64(c.handle), enable)
c.rollback = cb
}
func commitCallback(ctx context.Context, mod api.Module, pDB uint32) uint32 {
if c, ok := ctx.Value(connKey{}).(*Conn); ok && c.handle == pDB && c.commit != nil {
if !c.commit() {
return 1
}
}
return 0
}
func rollbackCallback(ctx context.Context, mod api.Module, pDB uint32) {
if c, ok := ctx.Value(connKey{}).(*Conn); ok && c.handle == pDB && c.rollback != nil {
c.rollback()
}
}
func updateCallback(ctx context.Context, mod api.Module, pDB, action, zSchema, zTabName uint32, rowid uint64) {
}