Commit 8f71d095 authored by gwenn's avatar gwenn

Add UpdateHook method.

parent cedd3642
...@@ -11,7 +11,8 @@ CGOFILES=\ ...@@ -11,7 +11,8 @@ CGOFILES=\
backup.go\ backup.go\
meta.go\ meta.go\
trace.go\ trace.go\
blob.go blob.go\
hook.go
GOFILES=\ GOFILES=\
date.go date.go
......
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package sqlite provides access to the SQLite library, version 3.
package sqlite
/*
#include <sqlite3.h>
#include <stdlib.h>
extern void goXUpdateHook(void *pArg, int action, char const *db, char const *table, sqlite3_int64 rowId);
static void goSqlite3UpdateHook(sqlite3 *db, void *pArg) {
sqlite3_update_hook(db, goXUpdateHook, pArg);
}
*/
import "C"
import (
"unsafe"
)
type UpdateHook func(d interface{}, a Action, db, table string, rowId int64)
type sqliteUpdateHook struct {
f UpdateHook
d interface{}
}
//export goXUpdateHook
func goXUpdateHook(pArg unsafe.Pointer, action C.int, db, table *C.char, rowId C.sqlite3_int64) {
arg := (*sqliteUpdateHook)(pArg)
arg.f(arg.d, Action(action), C.GoString(db), C.GoString(table), int64(rowId))
}
// Calls http://sqlite.org/c3ref/update_hook.html
func (c *Conn) UpdateHook(f UpdateHook, arg interface{}) {
if f == nil {
c.updateHook = nil
C.sqlite3_update_hook(c.db, nil, nil)
}
// To make sure it is not gced, keep a reference in the connection.
c.updateHook = &sqliteUpdateHook{f, arg}
C.goSqlite3UpdateHook(c.db, unsafe.Pointer(c.updateHook))
}
...@@ -140,6 +140,7 @@ type Conn struct { ...@@ -140,6 +140,7 @@ type Conn struct {
profile *sqliteProfile profile *sqliteProfile
progressHandler *sqliteProgressHandler progressHandler *sqliteProgressHandler
trace *sqliteTrace trace *sqliteTrace
updateHook *sqliteUpdateHook
} }
// Calls http://sqlite.org/c3ref/libversion.html // Calls http://sqlite.org/c3ref/libversion.html
......
package sqlite_test package sqlite_test
import ( import (
//"fmt" "fmt"
. "github.com/gwenn/gosqlite" . "github.com/gwenn/gosqlite"
"testing" "testing"
) )
...@@ -24,6 +24,10 @@ func progressHandler(d interface{}) int { ...@@ -24,6 +24,10 @@ func progressHandler(d interface{}) int {
return 0 return 0
} }
func update_hook(d interface{}, a Action, db, table string, rowId int64) {
fmt.Printf("%s: %d, %s.%s.%d\n", d, a, db, table, rowId)
}
func TestNoTrace(t *testing.T) { func TestNoTrace(t *testing.T) {
db, err := Open("") db, err := Open("")
if err != nil { if err != nil {
...@@ -34,6 +38,7 @@ func TestNoTrace(t *testing.T) { ...@@ -34,6 +38,7 @@ func TestNoTrace(t *testing.T) {
db.Profile(nil, nil) db.Profile(nil, nil)
db.ProgressHandler(nil, 0, nil) db.ProgressHandler(nil, 0, nil)
db.BusyHandler(nil, nil) db.BusyHandler(nil, nil)
db.UpdateHook(nil, nil)
db.Close() db.Close()
} }
...@@ -46,5 +51,6 @@ func TestTrace(t *testing.T) { ...@@ -46,5 +51,6 @@ func TestTrace(t *testing.T) {
} }
db.Profile(profile, "PROFILE") db.Profile(profile, "PROFILE")
db.ProgressHandler(progressHandler, 1, /*20*/ nil) db.ProgressHandler(progressHandler, 1, /*20*/ nil)
db.UpdateHook(update_hook, "TEST")
db.Exists("SELECT 1 WHERE 1 = ?", 1) db.Exists("SELECT 1 WHERE 1 = ?", 1)
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment