Commit f857010b authored by gwenn's avatar gwenn

Wraps sqlite3_progress_handler.

parent 808b499d
...@@ -134,11 +134,12 @@ func (c *Conn) Error() os.Error { ...@@ -134,11 +134,12 @@ func (c *Conn) Error() os.Error {
// Connection // Connection
type Conn struct { type Conn struct {
db *C.sqlite3 db *C.sqlite3
authorizer *sqliteAuthorizer authorizer *sqliteAuthorizer
busyHandler *sqliteBusyHandler busyHandler *sqliteBusyHandler
profile *sqliteProfile profile *sqliteProfile
trace *sqliteTrace progressHandler *sqliteProgressHandler
trace *sqliteTrace
} }
// Calls http://sqlite.org/c3ref/libversion.html // Calls http://sqlite.org/c3ref/libversion.html
......
...@@ -20,6 +20,11 @@ func profile(d interface{}, sql string, nanoseconds uint64) { ...@@ -20,6 +20,11 @@ func profile(d interface{}, sql string, nanoseconds uint64) {
fmt.Printf("%s: %s = %d\n", d, sql, nanoseconds/1000) fmt.Printf("%s: %s = %d\n", d, sql, nanoseconds/1000)
} }
func progressHandler(d interface{}) int {
fmt.Print("+")
return 0
}
func open(t *testing.T) *Conn { func open(t *testing.T) *Conn {
db, err := Open("", OPEN_READWRITE, OPEN_CREATE, OPEN_FULLMUTEX, OPEN_URI) db, err := Open("", OPEN_READWRITE, OPEN_CREATE, OPEN_FULLMUTEX, OPEN_URI)
if err != nil { if err != nil {
...@@ -36,6 +41,7 @@ func open(t *testing.T) *Conn { ...@@ -36,6 +41,7 @@ func open(t *testing.T) *Conn {
} }
*/ */
//db.Profile(profile, "PROFILE") //db.Profile(profile, "PROFILE")
//db.ProgressHandler(progressHandler, 20, nil)
return db return db
} }
......
...@@ -32,6 +32,12 @@ extern int goXBusy(void *pArg, int n); ...@@ -32,6 +32,12 @@ extern int goXBusy(void *pArg, int n);
static int goSqlite3BusyHandler(sqlite3 *db, void *pArg) { static int goSqlite3BusyHandler(sqlite3 *db, void *pArg) {
return sqlite3_busy_handler(db, goXBusy, pArg); return sqlite3_busy_handler(db, goXBusy, pArg);
} }
extern int goXProgress(void *pArg);
static void goSqlite3ProgressHandler(sqlite3 *db, int freq, void *pArg) {
sqlite3_progress_handler(db, freq, goXProgress, pArg);
}
*/ */
import "C" import "C"
...@@ -186,3 +192,30 @@ func (c *Conn) BusyHandler(f BusyHandler, arg interface{}) os.Error { ...@@ -186,3 +192,30 @@ func (c *Conn) BusyHandler(f BusyHandler, arg interface{}) os.Error {
c.busyHandler = &sqliteBusyHandler{f, arg} c.busyHandler = &sqliteBusyHandler{f, arg}
return c.error(C.goSqlite3BusyHandler(c.db, unsafe.Pointer(c.busyHandler))) return c.error(C.goSqlite3BusyHandler(c.db, unsafe.Pointer(c.busyHandler)))
} }
// Returns non-zero to interrupt.
type ProgressHandler func(d interface{}) int
type sqliteProgressHandler struct {
f ProgressHandler
d interface{}
}
//export goXProgress
func goXProgress(pArg unsafe.Pointer) C.int {
arg := (*sqliteProgressHandler)(pArg)
result := arg.f(arg.d)
return C.int(result)
}
// Calls http://sqlite.org/c3ref/progress_handler.html
func (c *Conn) ProgressHandler(f ProgressHandler, freq int, arg interface{}) {
if f == nil {
c.progressHandler = nil
C.sqlite3_progress_handler(c.db, 0, nil, nil)
return
}
// To make sure it is not gced, keep a reference in the connection.
c.progressHandler = &sqliteProgressHandler{f, arg}
C.goSqlite3ProgressHandler(c.db, C.int(freq), unsafe.Pointer(c.progressHandler))
}
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