First test case

This commit is contained in:
Maas-Maarten Zeeman
2011-10-13 21:40:24 +02:00
parent e7cab83784
commit 6fbb1cad4e
3 changed files with 34 additions and 6 deletions

View File

@@ -2,43 +2,59 @@
* esqlite -- an erlang sqlite nif.
*/
#include <stdio.h> /* for debugging */
#include <erl_nif.h>
#include "sqlite3.h"
#define MAX_PATHNAME 512 /* unfortunately not in sqlite.h */
#define MAX_PATHNAME 512 /* unfortunately not in sqlite.h. */
static ERL_NIF_TERM _atom_ok;
static ERL_NIF_TERM _atom_error;
/*
* Open database. Expects utf-8 input
*/
static ERL_NIF_TERM eisqlite_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];
sqlite3
sqlite3 *db;
/* TODO ignore the utf-8 stuff for now */
if(!enif_get_string(env, argv[0], filename, MAX_PATHNAME, ERL_NIF_LATIN1))
return enif_make_badarg(env);
/* open the database */
rc = sqlite3_open(filename, &db);
if( rc ) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
/* maak een error tuple */
}
/* and wrap it in an erlang resource */
fprintf(stderr, "The database is open.\n");
sqlite3_close(db);
fprintf(stderr, "And closed\n");
return enif_make_tuple2(env, _atom_ok, _atom_ok);
}
static int on_load(ErlNifEnv* env, void** priv, ERL_NIF_TERM info)
{
_atom_ok = enif_make_atom(env, "ok");
_atom_error = enif_make_atom(env, "error");
return 0;
}
static ErlNifFunc nif_funcs[] = {
{"open", 1, esqlite_open_nif},
};
ERL_NIF_INIT(esqlite, nif_funcs, on_load, NULL, NULL, NULL);

View File

@@ -5,6 +5,8 @@
-module(esqlite).
-author("Maas-Maarten Zeeman <mmzeeman@xs4all.nl>").
-export([open/1]).
-on_load(init/0).
init() ->
@@ -12,6 +14,6 @@ init() ->
%% @doc Open a new database connection
%%
open(Name) ->
ok.
open(_Filename) ->
exit(nif_library_not_loaded).

10
test/esqlite_test.erl Normal file
View File

@@ -0,0 +1,10 @@
%%
%% Test suite for esqlite.
%%
-module(esqlite_test).
-include_lib("eunit/include/eunit.hrl").
open_test() ->
{ok, C} = esqlite:open("test.db").