fixes sqlite3_column_count() return check
sqlite3_column_count() may return 0 for SQL statements that do not return rows from the database. This should not be treated as an error. See: https://sqlite.org/c3ref/column_count.html
This commit is contained in:
@@ -546,8 +546,10 @@ do_column_names(ErlNifEnv *env, sqlite3_stmt *stmt)
|
|||||||
ERL_NIF_TERM column_names;
|
ERL_NIF_TERM column_names;
|
||||||
|
|
||||||
size = sqlite3_column_count(stmt);
|
size = sqlite3_column_count(stmt);
|
||||||
if(size <= 0)
|
if(size == 0)
|
||||||
return make_error_tuple(env, "no_columns");
|
return enif_make_tuple(env, 0);
|
||||||
|
else if(size < 0)
|
||||||
|
return make_error_tuple(env, "invalid_column_count");
|
||||||
|
|
||||||
array = (ERL_NIF_TERM *) malloc(sizeof(ERL_NIF_TERM) * size);
|
array = (ERL_NIF_TERM *) malloc(sizeof(ERL_NIF_TERM) * size);
|
||||||
if(!array)
|
if(!array)
|
||||||
@@ -577,8 +579,10 @@ do_column_types(ErlNifEnv *env, sqlite3_stmt *stmt)
|
|||||||
ERL_NIF_TERM column_types;
|
ERL_NIF_TERM column_types;
|
||||||
|
|
||||||
size = sqlite3_column_count(stmt);
|
size = sqlite3_column_count(stmt);
|
||||||
if(size <= 0)
|
if(size == 0)
|
||||||
return make_error_tuple(env, "no_columns");
|
return enif_make_tuple(env, 0);
|
||||||
|
else if(size < 0)
|
||||||
|
return make_error_tuple(env, "invalid_column_count");
|
||||||
|
|
||||||
array = (ERL_NIF_TERM *) malloc(sizeof(ERL_NIF_TERM) * size);
|
array = (ERL_NIF_TERM *) malloc(sizeof(ERL_NIF_TERM) * size);
|
||||||
if(!array)
|
if(!array)
|
||||||
|
|||||||
@@ -178,6 +178,10 @@ column_names_test() ->
|
|||||||
{row, {Date}} = esqlite3:step(Stmt4),
|
{row, {Date}} = esqlite3:step(Stmt4),
|
||||||
true = is_binary(Date),
|
true = is_binary(Date),
|
||||||
|
|
||||||
|
%% Some statements have no column names
|
||||||
|
{ok, Stmt5} = esqlite3:prepare("create table dummy(a, b, c);", Db),
|
||||||
|
{} = esqlite3:column_names(Stmt5),
|
||||||
|
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
column_types_test() ->
|
column_types_test() ->
|
||||||
@@ -198,6 +202,10 @@ column_types_test() ->
|
|||||||
'$done' = esqlite3:step(Stmt),
|
'$done' = esqlite3:step(Stmt),
|
||||||
{'varchar(10)', int} = esqlite3:column_types(Stmt),
|
{'varchar(10)', int} = esqlite3:column_types(Stmt),
|
||||||
|
|
||||||
|
%% Some statements have no column types
|
||||||
|
{ok, Stmt2} = esqlite3:prepare("create table dummy(a, b, c);", Db),
|
||||||
|
{} = esqlite3:column_types(Stmt2),
|
||||||
|
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
nil_column_types_test() ->
|
nil_column_types_test() ->
|
||||||
|
|||||||
Reference in New Issue
Block a user