Commit 27311031 authored by gwenn's avatar gwenn

Fix SetAuthorizer.

parent 3ecfccaf
...@@ -134,7 +134,8 @@ func (c *Conn) Error() os.Error { ...@@ -134,7 +134,8 @@ func (c *Conn) Error() os.Error {
// Connection // Connection
type Conn struct { type Conn struct {
db *C.sqlite3 db *C.sqlite3
authorizer *sqliteAuthorizer
} }
// Calls http://sqlite.org/c3ref/libversion.html // Calls http://sqlite.org/c3ref/libversion.html
......
...@@ -11,7 +11,7 @@ func trace(d interface{}, t string) { ...@@ -11,7 +11,7 @@ func trace(d interface{}, t string) {
fmt.Printf("%s: %s\n", d, t) fmt.Printf("%s: %s\n", d, t)
} }
func authorizer(d interface{}, action int, arg1, arg2, arg3, arg4 string) Auth { func authorizer(d interface{}, action Action, arg1, arg2, arg3, arg4 string) Auth {
fmt.Printf("%s: %d, %s, %s, %s, %s\n", d, action, arg1, arg2, arg3, arg4) fmt.Printf("%s: %d, %s, %s, %s, %s\n", d, action, arg1, arg2, arg3, arg4)
return AUTH_OK return AUTH_OK
} }
......
...@@ -25,7 +25,6 @@ static int goSqlite3SetAuthorizer(sqlite3 *db, void *pUserData) { ...@@ -25,7 +25,6 @@ static int goSqlite3SetAuthorizer(sqlite3 *db, void *pUserData) {
import "C" import "C"
import ( import (
"fmt"
"os" "os"
"unsafe" "unsafe"
) )
...@@ -54,13 +53,52 @@ func (c *Conn) Trace(f SqliteTrace, arg interface{}) { ...@@ -54,13 +53,52 @@ func (c *Conn) Trace(f SqliteTrace, arg interface{}) {
} }
type Auth int type Auth int
const ( const (
AUTH_OK Auth = C.SQLITE_OK AUTH_OK Auth = C.SQLITE_OK
AUTH_DENY Auth = C.SQLITE_DENY AUTH_DENY Auth = C.SQLITE_DENY
AUTH_IGNORE Auth = C.SQLITE_IGNORE AUTH_IGNORE Auth = C.SQLITE_IGNORE
) )
type SqliteAuthorizer func(d interface{}, action int, arg1, arg2, arg3, arg4 string) Auth type Action int
const (
CREATE_INDEX Action = C.SQLITE_CREATE_INDEX
CREATE_TABLE Action = C.SQLITE_CREATE_TABLE
CREATE_TEMP_INDEX Action = C.SQLITE_CREATE_TEMP_INDEX
CREATE_TEMP_TABLE Action = C.SQLITE_CREATE_TEMP_TABLE
CREATE_TEMP_TRIGGER Action = C.SQLITE_CREATE_TEMP_TRIGGER
CREATE_TEMP_VIEW Action = C.SQLITE_CREATE_TEMP_VIEW
CREATE_TRIGGER Action = C.SQLITE_CREATE_TRIGGER
CREATE_VIEW Action = C.SQLITE_CREATE_VIEW
DELETE Action = C.SQLITE_DELETE
DROP_INDEX Action = C.SQLITE_DROP_INDEX
DROP_TABLE Action = C.SQLITE_DROP_TABLE
DROP_TEMP_INDEX Action = C.SQLITE_DROP_TEMP_INDEX
DROP_TEMP_TABLE Action = C.SQLITE_DROP_TEMP_TABLE
DROP_TEMP_TRIGGER Action = C.SQLITE_DROP_TEMP_TRIGGER
DROP_TEMP_VIEW Action = C.SQLITE_DROP_TEMP_VIEW
DROP_TRIGGER Action = C.SQLITE_DROP_TRIGGER
DROP_VIEW Action = C.SQLITE_DROP_VIEW
INSERT Action = C.SQLITE_INSERT
PRAGMA Action = C.SQLITE_PRAGMA
READ Action = C.SQLITE_READ
SELECT Action = C.SQLITE_SELECT
TRANSACTION Action = C.SQLITE_TRANSACTION
UPDATE Action = C.SQLITE_UPDATE
ATTACH Action = C.SQLITE_ATTACH
DETACH Action = C.SQLITE_DETACH
ALTER_TABLE Action = C.SQLITE_ALTER_TABLE
REINDEX Action = C.SQLITE_REINDEX
ANALYZE Action = C.SQLITE_ANALYZE
CREATE_VTABLE Action = C.SQLITE_CREATE_VTABLE
DROP_VTABLE Action = C.SQLITE_DROP_VTABLE
FUNCTION Action = C.SQLITE_FUNCTION
SAVEPOINT Action = C.SQLITE_SAVEPOINT
COPY Action = C.SQLITE_COPY
)
type SqliteAuthorizer func(d interface{}, action Action, arg1, arg2, arg3, arg4 string) Auth
type sqliteAuthorizer struct { type sqliteAuthorizer struct {
f SqliteAuthorizer f SqliteAuthorizer
...@@ -69,22 +107,18 @@ type sqliteAuthorizer struct { ...@@ -69,22 +107,18 @@ type sqliteAuthorizer struct {
//export goXAuth //export goXAuth
func goXAuth(pUserData unsafe.Pointer, action C.int, arg1, arg2, arg3, arg4 *C.char) C.int { func goXAuth(pUserData unsafe.Pointer, action C.int, arg1, arg2, arg3, arg4 *C.char) C.int {
var result Auth arg := (*sqliteAuthorizer)(pUserData)
if pUserData != nil { result := arg.f(arg.d, Action(action), C.GoString(arg1), C.GoString(arg2), C.GoString(arg3), C.GoString(arg4))
arg := (*sqliteAuthorizer)(pUserData)
result = arg.f(arg.d, int(action), C.GoString(arg1), C.GoString(arg2), C.GoString(arg3), C.GoString(arg4))
} else {
fmt.Printf("ERROR - %v\n", pUserData)
result = AUTH_OK
}
return C.int(result) return C.int(result)
} }
// Calls http://sqlite.org/c3ref/set_authorizer.html // Calls http://sqlite.org/c3ref/set_authorizer.html
func (c *Conn) SetAuthorizer(f SqliteAuthorizer, arg interface{}) os.Error { func (c *Conn) SetAuthorizer(f SqliteAuthorizer, arg interface{}) os.Error {
if f == nil { if f == nil {
c.authorizer = nil
return c.error(C.sqlite3_set_authorizer(c.db, nil, nil)) return c.error(C.sqlite3_set_authorizer(c.db, nil, nil))
} }
pArg := unsafe.Pointer(&sqliteAuthorizer{f, arg}) // To make sure it is not gced, keep a reference in the connection.
return c.error(C.goSqlite3SetAuthorizer(c.db, pArg)) c.authorizer = &sqliteAuthorizer{f, arg}
return c.error(C.goSqlite3SetAuthorizer(c.db, unsafe.Pointer(c.authorizer)))
} }
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