Yeah processing queries....

This commit is contained in:
Maas-Maarten Zeeman
2011-10-22 00:07:23 +02:00
parent 31a23dc46c
commit 5a56f14bb7
3 changed files with 234 additions and 180 deletions

View File

@@ -16,87 +16,87 @@ static ErlNifResourceType *esqlite_db_type = NULL;
/* database connection context */ /* database connection context */
typedef struct { typedef struct {
ErlNifTid tid; ErlNifTid tid;
ErlNifThreadOpts* opts; ErlNifThreadOpts* opts;
sqlite3 *db; sqlite3 *db;
queue *commands; queue *commands;
int alive; int alive;
} esqlite_db; } esqlite_db;
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 enum { typedef enum {
cmd_unknown, cmd_unknown,
cmd_open, cmd_open,
cmd_exec, cmd_exec,
cmd_close, cmd_close,
cmd_stop cmd_stop
} command_type; } command_type;
typedef struct { typedef struct {
command_type type; command_type type;
ErlNifEnv *env; ErlNifEnv *env;
ERL_NIF_TERM ref; ERL_NIF_TERM ref;
ErlNifPid pid; ErlNifPid pid;
ERL_NIF_TERM arg;
/* Args */
} esqlite_command; } esqlite_command;
static ERL_NIF_TERM static ERL_NIF_TERM
make_atom(ErlNifEnv *env, const char *atom_name) make_atom(ErlNifEnv *env, const char *atom_name)
{ {
ERL_NIF_TERM atom; ERL_NIF_TERM atom;
if(enif_make_existing_atom(env, atom_name, &atom, ERL_NIF_LATIN1)) if(enif_make_existing_atom(env, atom_name, &atom, ERL_NIF_LATIN1))
return atom; return atom;
return enif_make_atom(env, atom_name); return enif_make_atom(env, atom_name);
} }
static ERL_NIF_TERM static ERL_NIF_TERM
make_ok_tuple(ErlNifEnv *env, ERL_NIF_TERM value) make_ok_tuple(ErlNifEnv *env, ERL_NIF_TERM value)
{ {
return enif_make_tuple2(env, _atom_ok, value); return enif_make_tuple2(env, _atom_ok, value);
} }
static ERL_NIF_TERM static ERL_NIF_TERM
make_error_tuple(ErlNifEnv *env, const char *reason) make_error_tuple(ErlNifEnv *env, const char *reason)
{ {
return enif_make_tuple2(env, _atom_error, make_atom(env, reason)); return enif_make_tuple2(env, _atom_error, make_atom(env, reason));
} }
static void static void
command_destroy(void *obj) command_destroy(void *obj)
{ {
esqlite_command *cmd = (esqlite_command *) obj; esqlite_command *cmd = (esqlite_command *) obj;
if(cmd->env != NULL) if(cmd->env != NULL)
enif_free_env(cmd->env); enif_free_env(cmd->env);
enif_free(cmd);
enif_free(cmd);
} }
static esqlite_command * static esqlite_command *
command_create() command_create()
{ {
esqlite_command *cmd = (esqlite_command *) enif_alloc(sizeof(esqlite_command)); esqlite_command *cmd = (esqlite_command *) enif_alloc(sizeof(esqlite_command));
if(cmd == NULL) if(cmd == NULL)
return NULL; return NULL;
cmd->env = enif_alloc_env(); cmd->env = enif_alloc_env();
if(cmd->env == NULL) { if(cmd->env == NULL) {
command_destroy(cmd); command_destroy(cmd);
return NULL; return NULL;
} }
cmd->type = cmd_unknown; cmd->type = cmd_unknown;
cmd->ref = 0; cmd->ref = 0;
cmd->arg = 0;
return cmd; return cmd;
} }
/* /*
@@ -105,80 +105,129 @@ command_create()
static void static void
descruct_esqlite_db(ErlNifEnv *env, void *arg) descruct_esqlite_db(ErlNifEnv *env, void *arg)
{ {
esqlite_db *db = (esqlite_db *) arg; esqlite_db *db = (esqlite_db *) arg;
esqlite_command *cmd = command_create(); esqlite_command *cmd = command_create();
/* Send the stop command */ /* Send the stop command */
cmd->type = cmd_stop; cmd->type = cmd_stop;
queue_push(db->commands, cmd); queue_push(db->commands, cmd);
queue_send(db->commands, cmd); queue_send(db->commands, cmd);
/* wait for the thread to finish */
enif_thread_join(db->tid, NULL);
enif_thread_opts_destroy(db->opts);
/* the thread is finished... remove the command queue, and close the datbase. */
queue_destroy(db->commands);
/* wait for the thread to finish */ if(db->db)
enif_thread_join(db->tid, NULL); sqlite3_close(db->db);
enif_thread_opts_destroy(db->opts);
} }
static ERL_NIF_TERM static ERL_NIF_TERM
do_open(ErlNifEnv *env, esqlite_db *db, const ERL_NIF_TERM argv[]) do_open(ErlNifEnv *env, esqlite_db *db, const ERL_NIF_TERM arg)
{ {
fprintf(stderr, "do_open\n"); char filename[MAX_PATHNAME];
return _atom_ok; unsigned int size;
int rc;
ERL_NIF_TERM error;
if(db->db)
return make_error_tuple(env, "database_already_open");
size = enif_get_string(env, arg, filename, MAX_PATHNAME, ERL_NIF_LATIN1);
if(size <= 0)
return make_error_tuple(env, "invalid_filename");
rc = sqlite3_open(filename, &db->db);
if(rc == SQLITE_OK) {
fprintf(stderr, "opened %s\n", filename);
return _atom_ok;
}
error = make_error_tuple(env, sqlite3_errmsg(db->db));
sqlite3_close(db->db);
db->db = NULL;
return error;
}
static int
the_callback(void *a_param, int argc, char **argv, char **column)
{
int i;
fprintf(stderr, "record::::\n");
for (i = 0; i < argc; i++)
fprintf(stderr, "%s,\t", argv[i]);
fprintf(stderr, "\n");
return 0;
} }
static ERL_NIF_TERM static ERL_NIF_TERM
do_exec(ErlNifEnv *env, esqlite_db *db, const ERL_NIF_TERM argv[]) do_exec(ErlNifEnv *env, esqlite_db *db, const ERL_NIF_TERM arg)
{ {
fprintf(stderr, "do_exec\n"); ErlNifBinary bin;
return _atom_ok; int rc;
if(!db->db)
return make_error_tuple(env, "database_not_open");
/* Get the query as a binary -- and the end of string -- */
enif_inspect_iolist_as_binary(env, arg, &bin);
rc = sqlite3_exec(db->db, bin.data, the_callback, NULL, NULL);
fprintf(stderr, "do_exec %s: %d\n", bin.data, rc);
return _atom_ok;
} }
static ERL_NIF_TERM static ERL_NIF_TERM
do_close(ErlNifEnv *env, esqlite_db *db, const ERL_NIF_TERM argv[]) do_close(ErlNifEnv *env, esqlite_db *db, const ERL_NIF_TERM arg)
{ {
fprintf(stderr, "do_close\n"); fprintf(stderr, "do_close\n");
return _atom_ok; return _atom_ok;
} }
static ERL_NIF_TERM static ERL_NIF_TERM
evaluate_command(ErlNifEnv *env, command_type type, esqlite_db *db, const ERL_NIF_TERM argv[]) evaluate_command(ErlNifEnv *env, command_type type, esqlite_db *db, const ERL_NIF_TERM arg)
{ {
switch(type) { switch(type) {
case cmd_open: case cmd_open:
return do_open(env, db, NULL); return do_open(env, db, arg);
case cmd_exec: case cmd_exec:
return do_exec(env, db, NULL); return do_exec(env, db, arg);
case cmd_close: case cmd_close:
return do_close(env, db, NULL); return do_close(env, db, arg);
default: default:
return make_error_tuple(env, "invalid_command"); return make_error_tuple(env, "invalid_command");
} }
} }
static void * static void *
esqlite_db_run(void *arg) esqlite_db_run(void *arg)
{ {
esqlite_db *db = (esqlite_db *) arg; esqlite_db *db = (esqlite_db *) arg;
esqlite_command *cmd; esqlite_command *cmd;
int continue_running = 1; int continue_running = 1;
db->alive = 1; db->alive = 1;
while(continue_running) { while(continue_running) {
cmd = queue_pop(db->commands); cmd = queue_pop(db->commands);
if(cmd->type == cmd_stop) {
continue_running = 0;
} else {
enif_send(NULL, &cmd->pid, cmd->env,
enif_make_tuple2(cmd->env, cmd->ref,
evaluate_command(cmd->env, cmd->type, db, NULL)));
}
command_destroy(cmd); if(cmd->type == cmd_stop) {
} continue_running = 0;
} else {
db->alive = 0; enif_send(NULL, &cmd->pid, cmd->env,
return NULL; enif_make_tuple2(cmd->env, cmd->ref,
evaluate_command(cmd->env, cmd->type, db, cmd->arg)));
}
command_destroy(cmd);
}
db->alive = 0;
return NULL;
} }
/* /*
@@ -187,31 +236,31 @@ esqlite_db_run(void *arg)
static ERL_NIF_TERM static ERL_NIF_TERM
start_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) start_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{ {
esqlite_db *esqldb; esqlite_db *esqldb;
ERL_NIF_TERM db; ERL_NIF_TERM db;
/* Initialize the resource */ /* Initialize the resource */
esqldb = enif_alloc_resource(esqlite_db_type, sizeof(esqlite_db)); esqldb = enif_alloc_resource(esqlite_db_type, sizeof(esqlite_db));
esqldb->db = NULL; esqldb->db = NULL;
/* Create command queue */ /* Create command queue */
esqldb->commands = queue_create(); esqldb->commands = queue_create();
if(!esqldb->commands) { if(!esqldb->commands) {
enif_release_resource(esqldb); enif_release_resource(esqldb);
return make_error_tuple(env, "command_queue_create_failed"); return make_error_tuple(env, "command_queue_create_failed");
} }
/* Start command processing thread */ /* Start command processing thread */
esqldb->opts = enif_thread_opts_create("esqldb_thread_opts"); esqldb->opts = enif_thread_opts_create("esqldb_thread_opts");
if(enif_thread_create("", &esqldb->tid, esqlite_db_run, esqldb, esqldb->opts) != 0) { if(enif_thread_create("", &esqldb->tid, esqlite_db_run, esqldb, esqldb->opts) != 0) {
enif_release_resource(esqldb); enif_release_resource(esqldb);
return make_error_tuple(env, "thread_create_failed"); return make_error_tuple(env, "thread_create_failed");
} }
db = enif_make_resource(env, esqldb); db = enif_make_resource(env, esqldb);
enif_release_resource(esqldb); enif_release_resource(esqldb);
return make_ok_tuple(env, db); return make_ok_tuple(env, db);
} }
/* /*
@@ -220,36 +269,38 @@ start_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
static ERL_NIF_TERM static ERL_NIF_TERM
esqlite_open_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) esqlite_open_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{ {
esqlite_db *db; esqlite_db *db;
esqlite_command *cmd = NULL; esqlite_command *cmd = NULL;
ErlNifPid pid; ErlNifPid pid;
if(argc != 4)
return enif_make_badarg(env);
if(!enif_get_resource(env, argv[0], esqlite_db_type, (void **) &db))
return enif_make_badarg(env);
if(!enif_is_ref(env, argv[1]))
return make_error_tuple(env, "invalid_ref");
if(argc != 4) if(!enif_get_local_pid(env, argv[2], &pid))
return enif_make_badarg(env); return make_error_tuple(env, "invalid_pid");
if(!enif_get_resource(env, argv[0], esqlite_db_type, (void **) &db)) /* Note, no check is made for the type of the argument */
return enif_make_badarg(env);
if(!enif_is_ref(env, argv[1])) cmd = command_create();
return make_error_tuple(env, "invalid_ref"); if(!cmd)
return make_error_tuple(env, "command_create_failed");
if(!enif_get_local_pid(env, argv[2], &pid)) /* command */
return make_error_tuple(env, "invalid_pid"); cmd->type = cmd_open;
cmd->ref = enif_make_copy(cmd->env, argv[1]);
cmd->pid = pid;
cmd->arg = enif_make_copy(cmd->env, argv[3]);
cmd = command_create(); if(!queue_push(db->commands, cmd))
if(!cmd) return make_error_tuple(env, "command_push_failed");
return make_error_tuple(env, "command_create_failed");
/* command */
cmd->type = cmd_open;
cmd->ref = enif_make_copy(cmd->env, argv[1]);
cmd->pid = pid;
/* TODO add the filename as an argument */
if(!queue_push(db->commands, cmd))
return make_error_tuple(env, "command_push_failed");
return _atom_ok; return _atom_ok;
} }
/* /*
@@ -258,36 +309,36 @@ esqlite_open_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
static ERL_NIF_TERM static ERL_NIF_TERM
esqlite_exec_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) esqlite_exec_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{ {
esqlite_db *db; esqlite_db *db;
esqlite_command *cmd = NULL; esqlite_command *cmd = NULL;
ErlNifPid pid; ErlNifPid pid;
if(argc != 4)
return enif_make_badarg(env);
if(!enif_get_resource(env, argv[0], esqlite_db_type, (void **) &db))
return enif_make_badarg(env);
if(!enif_is_ref(env, argv[1]))
return make_error_tuple(env, "invalid_ref");
if(!enif_get_local_pid(env, argv[2], &pid))
return make_error_tuple(env, "invalid_pid");
cmd = command_create();
if(!cmd)
return make_error_tuple(env, "command_create_failed");
/* command */
cmd->type = cmd_exec;
cmd->ref = enif_make_copy(cmd->env, argv[1]);
cmd->pid = pid;
cmd->arg = enif_make_copy(cmd->env, argv[3]);
if(!queue_push(db->commands, cmd))
return make_error_tuple(env, "command_push_failed");
if(argc != 4) return _atom_ok;
return enif_make_badarg(env);
if(!enif_get_resource(env, argv[0], esqlite_db_type, (void **) &db))
return enif_make_badarg(env);
if(!enif_is_ref(env, argv[1]))
return make_error_tuple(env, "invalid_ref");
if(!enif_get_local_pid(env, argv[2], &pid))
return make_error_tuple(env, "invalid_pid");
cmd = command_create();
if(!cmd)
return make_error_tuple(env, "command_create_failed");
/* command */
cmd->type = cmd_exec;
cmd->ref = enif_make_copy(cmd->env, argv[1]);
cmd->pid = pid;
/* TODO add the query as an argument */
if(!queue_push(db->commands, cmd))
return make_error_tuple(env, "command_push_failed");
return _atom_ok;
} }
/* /*
@@ -296,7 +347,7 @@ esqlite_exec_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
static ERL_NIF_TERM static ERL_NIF_TERM
esqlite_close_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) esqlite_close_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{ {
return _atom_ok; return _atom_ok;
} }
/* /*
@@ -305,24 +356,24 @@ esqlite_close_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
static int static int
on_load(ErlNifEnv* env, void** priv, ERL_NIF_TERM info) on_load(ErlNifEnv* env, void** priv, ERL_NIF_TERM info)
{ {
ErlNifResourceType *rt = enif_open_resource_type(env, "esqlite", "esqlite_sqlite3_db", ErlNifResourceType *rt = enif_open_resource_type(env, "esqlite", "esqlite_sqlite3_db",
descruct_esqlite_db, ERL_NIF_RT_CREATE, NULL); descruct_esqlite_db, ERL_NIF_RT_CREATE, NULL);
if(!rt) if(!rt)
return -1; return -1;
esqlite_db_type = rt; esqlite_db_type = rt;
_atom_ok = make_atom(env, "ok"); _atom_ok = make_atom(env, "ok");
_atom_error = make_atom(env, "error"); _atom_error = make_atom(env, "error");
return 0; return 0;
} }
static ErlNifFunc nif_funcs[] = { static ErlNifFunc nif_funcs[] = {
{"esqlite_start", 0, start_nif}, {"esqlite_start", 0, start_nif},
{"esqlite_open", 4, esqlite_open_nif}, {"esqlite_open", 4, esqlite_open_nif},
{"esqlite_exec", 4, esqlite_exec_nif}, {"esqlite_exec", 4, esqlite_exec_nif},
{"esqlite_close", 3, esqlite_close_nif} {"esqlite_close", 3, esqlite_close_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

@@ -40,7 +40,9 @@ exec(Db, Sql) ->
exec(Db, Sql, Timeout) -> exec(Db, Sql, Timeout) ->
Ref = make_ref(), Ref = make_ref(),
ok = esqlite_exec(Db, Ref, self(), Sql), %% sqlite doesn't support length parameters for queries... add the
%% end of string here.
ok = esqlite_exec(Db, Ref, self(), [Sql, 0]),
receive_answer(Ref, Timeout). receive_answer(Ref, Timeout).
%% @doc Close the database %% @doc Close the database

View File

@@ -21,9 +21,10 @@ open_multiple_different_databases_test() ->
ok. ok.
simple_query_test() -> simple_query_test() ->
{ok, Db} = esqlite:open("test.db"), {ok, Db} = esqlite:open(":memory:"),
esqlite:exec(Db, "create table test_table(one varchar(10), two, smallint);"), esqlite:exec(Db, "create table test_table(one varchar(10), two int);"),
esqlite:exec(Db, ["insert into test_table values(", "hello", ",", "10" ");"]), esqlite:exec(Db, ["insert into test_table values(", "\"hello\"", ",", "10" ");"]),
esqlite:exec(Db, "select * from test_table;"),
ok. ok.