Yeah processing queries....
This commit is contained in:
@@ -42,9 +42,7 @@ typedef struct {
|
|||||||
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
|
||||||
@@ -77,6 +75,7 @@ command_destroy(void *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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,6 +94,7 @@ command_create()
|
|||||||
|
|
||||||
cmd->type = cmd_unknown;
|
cmd->type = cmd_unknown;
|
||||||
cmd->ref = 0;
|
cmd->ref = 0;
|
||||||
|
cmd->arg = 0;
|
||||||
|
|
||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
@@ -116,39 +116,88 @@ descruct_esqlite_db(ErlNifEnv *env, void *arg)
|
|||||||
/* wait for the thread to finish */
|
/* wait for the thread to finish */
|
||||||
enif_thread_join(db->tid, NULL);
|
enif_thread_join(db->tid, NULL);
|
||||||
enif_thread_opts_destroy(db->opts);
|
enif_thread_opts_destroy(db->opts);
|
||||||
|
|
||||||
|
/* the thread is finished... remove the command queue, and close the datbase. */
|
||||||
|
queue_destroy(db->commands);
|
||||||
|
|
||||||
|
if(db->db)
|
||||||
|
sqlite3_close(db->db);
|
||||||
}
|
}
|
||||||
|
|
||||||
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];
|
||||||
|
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
|
||||||
|
do_exec(ErlNifEnv *env, esqlite_db *db, const ERL_NIF_TERM arg)
|
||||||
|
{
|
||||||
|
ErlNifBinary bin;
|
||||||
|
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;
|
return _atom_ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ERL_NIF_TERM
|
static ERL_NIF_TERM
|
||||||
do_exec(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_exec\n");
|
|
||||||
return _atom_ok;
|
|
||||||
}
|
|
||||||
|
|
||||||
static ERL_NIF_TERM
|
|
||||||
do_close(ErlNifEnv *env, esqlite_db *db, const ERL_NIF_TERM argv[])
|
|
||||||
{
|
{
|
||||||
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");
|
||||||
}
|
}
|
||||||
@@ -171,7 +220,7 @@ esqlite_db_run(void *arg)
|
|||||||
} else {
|
} else {
|
||||||
enif_send(NULL, &cmd->pid, cmd->env,
|
enif_send(NULL, &cmd->pid, cmd->env,
|
||||||
enif_make_tuple2(cmd->env, cmd->ref,
|
enif_make_tuple2(cmd->env, cmd->ref,
|
||||||
evaluate_command(cmd->env, cmd->type, db, NULL)));
|
evaluate_command(cmd->env, cmd->type, db, cmd->arg)));
|
||||||
}
|
}
|
||||||
|
|
||||||
command_destroy(cmd);
|
command_destroy(cmd);
|
||||||
@@ -236,6 +285,8 @@ esqlite_open_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
|||||||
if(!enif_get_local_pid(env, argv[2], &pid))
|
if(!enif_get_local_pid(env, argv[2], &pid))
|
||||||
return make_error_tuple(env, "invalid_pid");
|
return make_error_tuple(env, "invalid_pid");
|
||||||
|
|
||||||
|
/* Note, no check is made for the type of the argument */
|
||||||
|
|
||||||
cmd = command_create();
|
cmd = command_create();
|
||||||
if(!cmd)
|
if(!cmd)
|
||||||
return make_error_tuple(env, "command_create_failed");
|
return make_error_tuple(env, "command_create_failed");
|
||||||
@@ -244,7 +295,7 @@ esqlite_open_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
|||||||
cmd->type = cmd_open;
|
cmd->type = cmd_open;
|
||||||
cmd->ref = enif_make_copy(cmd->env, argv[1]);
|
cmd->ref = enif_make_copy(cmd->env, argv[1]);
|
||||||
cmd->pid = pid;
|
cmd->pid = pid;
|
||||||
/* TODO add the filename as an argument */
|
cmd->arg = enif_make_copy(cmd->env, argv[3]);
|
||||||
|
|
||||||
if(!queue_push(db->commands, cmd))
|
if(!queue_push(db->commands, cmd))
|
||||||
return make_error_tuple(env, "command_push_failed");
|
return make_error_tuple(env, "command_push_failed");
|
||||||
@@ -282,7 +333,7 @@ esqlite_exec_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
|||||||
cmd->type = cmd_exec;
|
cmd->type = cmd_exec;
|
||||||
cmd->ref = enif_make_copy(cmd->env, argv[1]);
|
cmd->ref = enif_make_copy(cmd->env, argv[1]);
|
||||||
cmd->pid = pid;
|
cmd->pid = pid;
|
||||||
/* TODO add the query as an argument */
|
cmd->arg = enif_make_copy(cmd->env, argv[3]);
|
||||||
|
|
||||||
if(!queue_push(db->commands, cmd))
|
if(!queue_push(db->commands, cmd))
|
||||||
return make_error_tuple(env, "command_push_failed");
|
return make_error_tuple(env, "command_push_failed");
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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.
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user