Adding column names functionality

This commit is contained in:
Maas-Maarten Zeeman
2011-11-21 20:34:13 +01:00
parent e04da6dcc7
commit a082740cb6
3 changed files with 31 additions and 3 deletions

View File

@@ -388,8 +388,25 @@ do_step(ErlNifEnv *env, sqlite3_stmt *stmt)
static ERL_NIF_TERM
do_column_names(ErlNifEnv *env, sqlite3_stmt *stmt)
{
fprintf(stderr, "Called column names\n");
return _atom_ok;
int i, size;
const char *name;
ERL_NIF_TERM *array;
ERL_NIF_TERM column_names;
size = sqlite3_column_count(stmt);
array = (ERL_NIF_TERM *) malloc(sizeof(ERL_NIF_TERM) * size);
if(!array)
return make_error_tuple(env, "no_memory");
for(i = 0; i < size; i++) {
name = sqlite3_column_name(stmt, i);
array[i] = make_atom(env, name);
}
column_names = enif_make_tuple_from_array(env, array, size);
free(array);
return column_names;
}
static ERL_NIF_TERM

View File

@@ -26,6 +26,7 @@
prepare/2, prepare/3,
step/1, step/2,
bind/2, bind/3,
column_names/1, column_names/2,
close/1, close/2]).
-export([q/2, map/3, foreach/3]).
@@ -164,6 +165,16 @@ bind(Stmt, Args, Timeout) ->
ok = esqlite3_nif:bind(Stmt, Ref, self(), Args),
receive_answer(Ref, Timeout).
%% @doc Return the column names of the prepared statement.
%%
column_names(Stmt) ->
column_names(Stmt, ?DEFAULT_TIMEOUT).
column_names(Stmt, Timeout) ->
Ref = make_ref(),
ok = esqlite3_nif:column_names(Stmt, Ref, self()),
receive_answer(Ref, Timeout).
%% @doc Close the database
%%
%% @spec close(connection()) -> ok | {error, error_message()}

View File

@@ -83,7 +83,7 @@ column_names_test() ->
{ok, Stmt} = esqlite3:prepare("select * from test_table", Db),
test = esqlite3:column_names(Stmt),
{one, two} = esqlite3:column_names(Stmt),
ok.