I'm working on writing a ffi for SQLite3. The goal is to replace John Graham-Cumming's sqlite network wrapper. The first step will be to implement open exec and close. That will be enough to get a functional SQLite3 database. This is my first time implementing any ffi. Also I haven't had a chance to clean up or refactor the code. So I'll apologize for that now. As of right now sqlite3-open works. sqlite3-exec seg faults with 0x3c. I'm currently deving on mac os x. But I could jump over to my linux box if needed. In the end I'm hoping to get the code to work on linux, mac os x and windows. I'm looking for some help to get it functional. If anyone can help it is much appreciated! The Code: (require "ffi.arc")
;; begin callback
;; this list will hold all defined callbacks
;; without this, they would be garbage collected, because Scheme's runtime
;; doesn't know if the foreign runtime references or not the callbacks
;; this way, I'm sure that they'll never be garbage collected
(= callbacks* nil)
(def sqlite3-callback args ;(db3 int3 argin3 errormessage3)
t);(prn db3 int3 argin3 errormessage3)
;; load the callback function into callbacks*
(let cb (ffi-callback sqlite3-callback (list
cptr
cint
cstring
cstring)
cint)
(push cb callbacks*) cb) ;Callback function
(def topcb () (let cb (pop callbacks*) (push cb callbacks*) cb))
;dependency - mac os x dylib / linux .so / windows .dll
(w/ffi "/opt/local/lib/libsqlite3" ;in mac os x "sudo port install sqlite3"
;int sqlite3_open(
; const char *filename, /* Database filename (UTF-8) */
; sqlite3 **ppDb /* OUT: SQLite db handle */
;);
;sqlite3_open
(cdef sqlite3-open "sqlite3_open" cint (cstring cptr))
;int sqlite3_exec(
; sqlite3*, /* An open database */
; const char *sql, /* SQL to be evaluted */
; int (*callback)(void*,int,char**,char**), /* Callback function */
; void *, /* 1st argument to callback */
; char **errmsg /* Error msg written here */
;);
;sqlite3_exec
(cdef sqlite3-exec "sqlite3_exec" cint (cptr ;sqlite3* - An open database
cstring ;SQL to be evaluated
cfptr;function pointer
cptr ;1st argument to callback
cstring)) ;Error msg - need to GC this.
;int sqlite3_close(sqlite3 *);
;sqlite3_close
(cdef sqlite3-close "sqlite3_close" cint (cptr)))
Load code: (= sqlite3 (cmalloc 4))
(= sqltxt "create table bar (id integer primary key, text varchar(255));")
(= sqlerrormsg "")
(= sqltxtptr (cmalloc 4))
(cpset sqltxtptr cstring sqltxt)
(sqlite3-open "test" sqlite3)
(sqlite3-exec sqlite3 sqltxt (topcb) sqltxtptr sqlerrormsg)
Don't forget to change w/ffi if you are going to try to run the code.links ffi: http://arclanguage.org/item?id=6053 http://arclanguage.org/item?id=5193 http://arclanguage.org/item?id=4706 links sqlite: http://www.sqlite.org/c3ref/intro.html http://www.jgc.org/blog/labels/arc.html |