Commit 1ee314b6 authored by gwenn's avatar gwenn

Wraps sqlite3_profile.

parent a40e43f9
......@@ -136,6 +136,7 @@ func (c *Conn) Error() os.Error {
type Conn struct {
db *C.sqlite3
authorizer *sqliteAuthorizer
profile *sqliteProfile
trace *sqliteTrace
}
......
......@@ -16,6 +16,10 @@ func authorizer(d interface{}, action Action, arg1, arg2, arg3, arg4 string) Aut
return AUTH_OK
}
func profile(d interface{}, sql string, nanoseconds uint64) {
fmt.Printf("%s: %s = %d\n", d, sql, nanoseconds/1000)
}
func open(t *testing.T) *Conn {
db, err := Open("", OPEN_READWRITE, OPEN_CREATE, OPEN_FULLMUTEX, OPEN_URI)
if err != nil {
......@@ -31,6 +35,7 @@ func open(t *testing.T) *Conn {
t.Fatal("couldn't set an authorizer", err)
}
*/
//db.Profile(profile, "PROFILE")
return db
}
......
......@@ -15,6 +15,11 @@ extern void goXTrace(void *pArg, const char *t);
static void goSqlite3Trace(sqlite3 *db, void *pArg) {
sqlite3_trace(db, goXTrace, pArg);
}
extern void goXProfile(void *pArg, const char *sql, sqlite3_uint64 nanoseconds);
static void goSqlite3Profile(sqlite3 *db, void *pArg) {
sqlite3_profile(db, goXProfile, pArg);
}
extern int goXAuth(void *pUserData, int action, const char *arg1, const char *arg2, const char *arg3, const char *arg4);
......@@ -54,6 +59,31 @@ func (c *Conn) Trace(f Tracer, arg interface{}) {
C.goSqlite3Trace(c.db, unsafe.Pointer(c.trace))
}
type Profiler func(d interface{}, sql string, nanoseconds uint64)
type sqliteProfile struct {
f Profiler
d interface{}
}
//export goXProfile
func goXProfile(pArg unsafe.Pointer, sql *C.char, nanoseconds C.sqlite3_uint64) {
arg := (*sqliteProfile)(pArg)
arg.f(arg.d, C.GoString(sql), uint64(nanoseconds))
}
// Calls sqlite3_profile, http://sqlite.org/c3ref/profile.html
func (c *Conn) Profile(f Profiler, arg interface{}) {
if f == nil {
c.profile = nil
C.sqlite3_profile(c.db, nil, nil)
return
}
// To make sure it is not gced, keep a reference in the connection.
c.profile = &sqliteProfile{f, arg}
C.goSqlite3Profile(c.db, unsafe.Pointer(c.profile))
}
type Auth int
const (
......
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