Work in progress
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
#include <stdio.h> /* for debugging */
|
#include <stdio.h> /* for debugging */
|
||||||
#include <erl_nif.h>
|
#include <erl_nif.h>
|
||||||
|
|
||||||
|
#include "queue.h"
|
||||||
#include "sqlite3.h"
|
#include "sqlite3.h"
|
||||||
|
|
||||||
#define MAX_PATHNAME 512 /* unfortunately not in sqlite.h. */
|
#define MAX_PATHNAME 512 /* unfortunately not in sqlite.h. */
|
||||||
@@ -14,65 +15,176 @@ 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;
|
||||||
|
|
||||||
|
/* database connection context */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
ErlNifTid tid;
|
||||||
|
ErlNifThreadOpts* opts;
|
||||||
|
|
||||||
sqlite3 *db;
|
sqlite3 *db;
|
||||||
} esqlite_sqlite3;
|
queue *commands;
|
||||||
|
|
||||||
|
int alive;
|
||||||
|
} esqlite_db;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
cmd_unknown,
|
||||||
|
cmd_open,
|
||||||
|
cmd_exec,
|
||||||
|
cmd_close,
|
||||||
|
cmd_stop
|
||||||
|
} command_type;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
command_type type;
|
||||||
|
|
||||||
|
ErlNifEnv *env;
|
||||||
|
ERL_NIF_TERM ref;
|
||||||
|
ErlNifPid pid;
|
||||||
|
|
||||||
|
/* Args */
|
||||||
|
|
||||||
|
} esqlite_command;
|
||||||
|
|
||||||
|
static void
|
||||||
|
command_destroy(void *obj)
|
||||||
|
{
|
||||||
|
esqlite_command *cmd = (esqlite_command *) obj;
|
||||||
|
|
||||||
|
if(cmd->env != NULL)
|
||||||
|
enif_free_env(cmd->env);
|
||||||
|
enif_free(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
static esqlite_command *
|
||||||
|
command_create()
|
||||||
|
{
|
||||||
|
esqlite_command *cmd = (esqlite_command *) enif_alloc(sizeof(esqlite_command));
|
||||||
|
if(cmd == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
cmd->env = enif_alloc_env();
|
||||||
|
if(cmd->env == NULL) {
|
||||||
|
command_destroy(cmd);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd->type = cmd_unknown;
|
||||||
|
cmd->ref = 0;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
*/
|
*/
|
||||||
static void descruct_esqlite_sqlite3(ErlNifEnv *env, void *esqlite_db)
|
static void
|
||||||
|
descruct_esqlite_db(ErlNifEnv *env, void *arg)
|
||||||
{
|
{
|
||||||
/* The destructor should only be called after all the prepared statements are finalized... */
|
esqlite_db *db = (esqlite_db *) arg;
|
||||||
/* TODO keep references, so this is the case */
|
esqlite_command *cmd = command_create();
|
||||||
int rc;
|
|
||||||
|
/* send the stop command */
|
||||||
rc = sqlite3_close(((esqlite_sqlite3 *) esqlite_db)->db);
|
cmd->type = cmd_stop;
|
||||||
if(rc == SQLITE_BUSY) {
|
queue_push(db->commands, cmd);
|
||||||
/* Can I raise exceptions here errors here? */
|
queue_send(db->commands, cmd);
|
||||||
fprintf(stderr, "Close failed, still busy\n");
|
|
||||||
|
/* wait for the thread to finish */
|
||||||
|
enif_thread_join(db->tid, NULL);
|
||||||
|
enif_thread_opts_destroy(db->opts);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void *
|
||||||
|
esqlite_db_run(void *arg)
|
||||||
|
{
|
||||||
|
esqlite_db *db = (esqlite_db *) arg;
|
||||||
|
esqlite_command *cmd;
|
||||||
|
|
||||||
|
db->alive = 1;
|
||||||
|
|
||||||
|
/* Wait for incoming commands and execute them */
|
||||||
|
while(1) {
|
||||||
|
cmd = get_command(db);
|
||||||
|
|
||||||
|
/* We are stopping... */
|
||||||
|
if(cmd_stop == command->type) {
|
||||||
|
command_destroy(command);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Evaluate the command */
|
||||||
|
switch(command->type) {
|
||||||
|
cmd_open:
|
||||||
|
/* do open */
|
||||||
|
break;
|
||||||
|
cmd_exec:
|
||||||
|
/* do exec */
|
||||||
|
break;
|
||||||
|
cmd_close:
|
||||||
|
/* do close */
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
assert(0 && "Invalid command");
|
||||||
|
}
|
||||||
|
|
||||||
|
enif_send(NULL, &(command->pid), command->env, _atom_ok);
|
||||||
|
command_destroy(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db->alive = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 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[])
|
* Note the database is opened in a thread. New commands are send to
|
||||||
|
* the thread and when it finishes the result is send back. The reason
|
||||||
|
* for this is that we don't want to block the erlang scheduler of the
|
||||||
|
* calling function.
|
||||||
|
*/
|
||||||
|
static ERL_NIF_TERM
|
||||||
|
start_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
|
||||||
{
|
{
|
||||||
char filename[MAX_PATHNAME];
|
esqlite_db *esqldb;
|
||||||
sqlite3 *db;
|
ERL_NIF_TERM esqlite_db;
|
||||||
esqlite_sqlite3 *esqldb;
|
|
||||||
ERL_NIF_TERM esqlite_db;
|
|
||||||
|
|
||||||
/* 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))
|
|
||||||
return enif_make_badarg(env);
|
|
||||||
|
|
||||||
if(sqlite3_open(filename, &db)) {
|
/* initialize the resource */
|
||||||
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
|
esqldb = enif_alloc_resource(esqlite_sqlite3_type, sizeof(esqlite_db));
|
||||||
sqlite3_close(db);
|
esqldb->db = NULL;
|
||||||
/* TODO maak een error tuple */
|
esqldb->alive = 0;
|
||||||
return enif_make_tuple2(env, _atom_error, _atom_error); /* todo add error message */
|
|
||||||
}
|
|
||||||
|
|
||||||
esqldb = enif_alloc_resource(esqlite_sqlite3_type, sizeof(esqlite_sqlite3));
|
/* Start the command processing thread */
|
||||||
esqldb->db = db;
|
esqldb->opts = enif_thread_opts_create("esqldb_thread_opts");
|
||||||
esqlite_db = enif_make_resource(env, esqldb);
|
if(enif_thread_create("", &esqldb->tid, esqlite_db_run, esqldb, esqldb->opts) != 0) {
|
||||||
enif_release_resource(esqldb);
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
return enif_make_tuple2(env, _atom_ok, esqlite_db);
|
/* */
|
||||||
|
esqlite_db = enif_make_resource(env, esqldb);
|
||||||
|
enif_release_resource(esqldb);
|
||||||
|
|
||||||
|
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[])
|
static ERL_NIF_TERM
|
||||||
|
esqlite_open_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
||||||
{
|
{
|
||||||
|
return _atom_ok;
|
||||||
return enif_make_tuple2(env, _atom_ok, _atom_ok);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static ERL_NIF_TERM
|
||||||
|
esqlite_exec_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
||||||
|
{
|
||||||
|
return _atom_ok;
|
||||||
|
}
|
||||||
|
|
||||||
static int on_load(ErlNifEnv* env, void** priv, ERL_NIF_TERM info)
|
static ERL_NIF_TERM
|
||||||
|
esqlite_close_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
|
||||||
|
{
|
||||||
|
return _atom_ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
on_load(ErlNifEnv* env, void** priv, ERL_NIF_TERM info)
|
||||||
{
|
{
|
||||||
ErlNifResourceType *rt = enif_open_resource_type(env, "esqlite", "esqlite_sqlite3_type",
|
ErlNifResourceType *rt = enif_open_resource_type(env, "esqlite", "esqlite_sqlite3_type",
|
||||||
descruct_esqlite_sqlite3, ERL_NIF_RT_CREATE, NULL);
|
descruct_esqlite_db, ERL_NIF_RT_CREATE, NULL);
|
||||||
if(!rt)
|
if(!rt)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
@@ -86,8 +198,10 @@ 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},
|
{"esqlite_start", 0, start_nif},
|
||||||
{"prepare", 2, esqlite_prepare_nif}
|
{"esqlite_open", 2, esqlite_open_nif},
|
||||||
|
{"esqlite_exec", 4, esqlite_exec_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);
|
||||||
|
|||||||
@@ -5,18 +5,79 @@
|
|||||||
-module(esqlite).
|
-module(esqlite).
|
||||||
-author("Maas-Maarten Zeeman <mmzeeman@xs4all.nl>").
|
-author("Maas-Maarten Zeeman <mmzeeman@xs4all.nl>").
|
||||||
|
|
||||||
-export([open/1, prepare/2]).
|
-export([open/1, open/2, exec/2, exec/3, close/1, close/2]).
|
||||||
|
|
||||||
-on_load(init/0).
|
-on_load(init/0).
|
||||||
|
|
||||||
|
-define(DEFAULT_TIMEOUT, infinity).
|
||||||
|
|
||||||
init() ->
|
init() ->
|
||||||
ok = erlang:load_nif(code:priv_dir(esqlite) ++ "/esqlite_nif", 0).
|
ok = erlang:load_nif(code:priv_dir(esqlite) ++ "/esqlite_nif", 0).
|
||||||
|
|
||||||
%% @doc Open a new database connection
|
%% @doc Open a new database connection
|
||||||
%%
|
%%
|
||||||
open(_Filename) ->
|
open(Filename) ->
|
||||||
|
open(Filename, ?DEFAULT_TIMEOUT).
|
||||||
|
|
||||||
|
%% @doc Open a database connection
|
||||||
|
%%
|
||||||
|
open(Filename, Timeout) ->
|
||||||
|
Db = esqlite_start(),
|
||||||
|
|
||||||
|
Ref = make_ref(),
|
||||||
|
ok = esqlite_open(Db, Ref, Filename, self()),
|
||||||
|
case receive_answer(Ref, Timeout) of
|
||||||
|
ok ->
|
||||||
|
{ok, Db};
|
||||||
|
Other ->
|
||||||
|
{error, Other}
|
||||||
|
end.
|
||||||
|
|
||||||
|
%% @doc Execute Sql statement
|
||||||
|
%%
|
||||||
|
exec(Sql, Db) ->
|
||||||
|
exec(Sql, Db, ?DEFAULT_TIMEOUT).
|
||||||
|
|
||||||
|
exec(Sql, Db, Timeout) ->
|
||||||
|
Ref = make_ref(),
|
||||||
|
ok = esqlite_exec(Db, Ref, self(), Sql),
|
||||||
|
receive_answer(Ref, Timeout).
|
||||||
|
|
||||||
|
%% @doc Close the database
|
||||||
|
%%
|
||||||
|
close(Db) ->
|
||||||
|
close(Db, ?DEFAULT_TIMEOUT).
|
||||||
|
|
||||||
|
close(Db, Timeout) ->
|
||||||
|
Ref = make_ref(),
|
||||||
|
ok = esqlite_close(Db, Ref, self()),
|
||||||
|
receive_answer(Ref, Timeout).
|
||||||
|
|
||||||
|
|
||||||
|
%% ---- Internal ----
|
||||||
|
esqlite_start() ->
|
||||||
exit(nif_library_not_loaded).
|
exit(nif_library_not_loaded).
|
||||||
|
|
||||||
prepare(_Db, _Sql) ->
|
esqlite_open(_Db, _Ref, _Dest, _Filename) ->
|
||||||
exit(nif_library_not_loaded).
|
exit(nif_library_not_loaded).
|
||||||
|
|
||||||
|
esqlite_exec(_Db, _Ref, _Dest, _Sql) ->
|
||||||
|
exit(nif_library_not_loaded).
|
||||||
|
|
||||||
|
esqlite_close(_Db, _Ref, _Dest) ->
|
||||||
|
exit(nif_library_not_loaded).
|
||||||
|
|
||||||
|
receive_answer(Ref, Timeout) ->
|
||||||
|
receive
|
||||||
|
{Ref, Resp} ->
|
||||||
|
Resp;
|
||||||
|
Other ->
|
||||||
|
throw(Other)
|
||||||
|
after Timeout ->
|
||||||
|
throw({error, timeout, Ref})
|
||||||
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,5 +6,13 @@
|
|||||||
|
|
||||||
-include_lib("eunit/include/eunit.hrl").
|
-include_lib("eunit/include/eunit.hrl").
|
||||||
|
|
||||||
open_test() ->
|
open_single_database_test() ->
|
||||||
{ok, C} = esqlite:open("test.db").
|
{ok, _C1} = esqlite:open("test.db"),
|
||||||
|
ok.
|
||||||
|
|
||||||
|
open_multiple_databases_test() ->
|
||||||
|
{ok, _C1} = esqlite:open("test.db"),
|
||||||
|
{ok, _C2} = esqlite:open("test.db"),
|
||||||
|
|
||||||
|
ok.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user