Now with a real open, but nothing else...

This commit is contained in:
Maas-Maarten Zeeman
2011-10-14 00:41:27 +02:00
parent 383d066f56
commit 71db5d00f7
3 changed files with 51 additions and 13 deletions

1
README
View File

@@ -0,0 +1 @@
Work in progress....

View File

@@ -9,42 +9,75 @@
#define MAX_PATHNAME 512 /* unfortunately not in sqlite.h. */ #define MAX_PATHNAME 512 /* unfortunately not in sqlite.h. */
static ErlNifResourceType *esqlite_sqlite3_type = NULL;
static ERL_NIF_TERM _atom_ok; static ERL_NIF_TERM _atom_ok;
static ERL_NIF_TERM _atom_error; static ERL_NIF_TERM _atom_error;
typedef struct {
sqlite3 *db;
} esqlite_sqlite3;
/*
*/
static void descruct_esqlite_sqlite3(ErlNifEnv *env, void *esqlite_db)
{
/* The destructor should only be called after all the prepared statements are finalized... */
/* TODO keep references, so this is the case */
int rc;
rc = sqlite3_close(((esqlite_sqlite3 *) esqlite_db)->db);
if(rc == SQLITE_BUSY) {
/* Can I raise exceptions here errors here? */
fprintf(stderr, "Close failed, still busy\n");
}
}
/* /*
* Open database. Expects utf-8 input * Open database. Expects utf-8 input
*/ */
static ERL_NIF_TERM esqlite_open_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) static ERL_NIF_TERM esqlite_open_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{ {
int rc;
char filename[MAX_PATHNAME]; char filename[MAX_PATHNAME];
sqlite3 *db; sqlite3 *db;
esqlite_sqlite3 *esqldb;
ERL_NIF_TERM esqlite_db;
/* TODO ignore the utf-8 stuff for now */ /* TODO ignores the utf-8 stuff for now. nifs only encoding is latin1 */
if(!enif_get_string(env, argv[0], filename, MAX_PATHNAME, ERL_NIF_LATIN1)) if(!enif_get_string(env, argv[0], filename, MAX_PATHNAME, ERL_NIF_LATIN1))
return enif_make_badarg(env); return enif_make_badarg(env);
/* open the database */ if(sqlite3_open(filename, &db)) {
rc = sqlite3_open(filename, &db);
if( rc ) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db); sqlite3_close(db);
/* maak een error tuple */ /* TODO maak een error tuple */
return enif_make_tuple2(env, _atom_error, _atom_error); /* todo add error message */ return enif_make_tuple2(env, _atom_error, _atom_error); /* todo add error message */
} }
/* and wrap it in an erlang resource */ esqldb = enif_alloc_resource(esqlite_sqlite3_type, sizeof(esqlite_sqlite3));
fprintf(stderr, "The database is open.\n"); esqldb->db = db;
sqlite3_close(db); esqlite_db = enif_make_resource(env, esqldb);
fprintf(stderr, "And closed\n"); enif_release_resource(esqldb);
return enif_make_tuple2(env, _atom_ok, _atom_ok); return enif_make_tuple2(env, _atom_ok, esqlite_db);
}
static ERL_NIF_TERM esqlite_prepare_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
return enif_make_tuple2(env, _atom_ok, _atom_ok);
} }
static int on_load(ErlNifEnv* env, void** priv, ERL_NIF_TERM info) static int on_load(ErlNifEnv* env, void** priv, ERL_NIF_TERM info)
{ {
ErlNifResourceType *rt = enif_open_resource_type(env, "esqlite", "esqlite_sqlite3_type",
descruct_esqlite_sqlite3, ERL_NIF_RT_CREATE, NULL);
if(!rt)
return -1;
esqlite_sqlite3_type = rt;
_atom_ok = enif_make_atom(env, "ok"); _atom_ok = enif_make_atom(env, "ok");
_atom_error = enif_make_atom(env, "error"); _atom_error = enif_make_atom(env, "error");
@@ -54,6 +87,7 @@ static int on_load(ErlNifEnv* env, void** priv, ERL_NIF_TERM info)
static ErlNifFunc nif_funcs[] = { static ErlNifFunc nif_funcs[] = {
{"open", 1, esqlite_open_nif}, {"open", 1, esqlite_open_nif},
{"prepare", 2, esqlite_prepare_nif}
}; };
ERL_NIF_INIT(esqlite, nif_funcs, on_load, NULL, NULL, NULL); ERL_NIF_INIT(esqlite, nif_funcs, on_load, NULL, NULL, NULL);

View File

@@ -5,7 +5,7 @@
-module(esqlite). -module(esqlite).
-author("Maas-Maarten Zeeman <mmzeeman@xs4all.nl>"). -author("Maas-Maarten Zeeman <mmzeeman@xs4all.nl>").
-export([open/1]). -export([open/1, prepare/2]).
-on_load(init/0). -on_load(init/0).
@@ -17,3 +17,6 @@ init() ->
open(_Filename) -> open(_Filename) ->
exit(nif_library_not_loaded). exit(nif_library_not_loaded).
prepare(_Db, _Sql) ->
exit(nif_library_not_loaded).