Skip to content

Commit

Permalink
Fixed tests and documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyr committed May 10, 2011
1 parent b0af679 commit d35c52a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
17 changes: 11 additions & 6 deletions c_src/sqlite3_drv.c
Expand Up @@ -594,7 +594,7 @@ static void sql_free_async(void *_async_command) {
driver_free(async_command);
}

static void sql_exec_one_statement(
static int sql_exec_one_statement(
sqlite3_stmt *statement, async_sqlite3_command *async_command,
int *term_count_p, int *term_allocated_p, ErlDrvTermData **dataset_p) {
int column_count = sqlite3_column_count(statement);
Expand Down Expand Up @@ -740,14 +740,14 @@ static void sql_exec_one_statement(
dataset_p, term_count_p,
term_allocated_p, &async_command->error_code);
async_command->finalize_statement_on_free = 1;
return;
return next_row;
}
if (next_row != SQLITE_DONE) {
return_error(drv, next_row, sqlite3_errmsg(drv->db),
dataset_p, term_count_p,
term_allocated_p, &async_command->error_code);
async_command->finalize_statement_on_free = 1;
return;
return next_row;
}

if (column_count > 0) {
Expand Down Expand Up @@ -796,6 +796,8 @@ static void sql_exec_one_statement(
fflush(drv->log);
#endif
async_command->finalize_statement_on_free = 1;

return 0;
}

static void sql_exec_async(void *_async_command) {
Expand Down Expand Up @@ -836,16 +838,19 @@ static void sql_exec_async(void *_async_command) {
}
result = sqlite3_prepare_v2(drv->db, rest, end - rest, &statement, &rest);
if (result != SQLITE_OK) {
num_statements++;
return_error(drv, result, sqlite3_errmsg(drv->db), &dataset,
&term_count, &term_allocated, &async_command->error_code);
num_statements++;
break;
} else if (statement == NULL) {
break;
} else {
num_statements++;
sql_exec_one_statement(statement, async_command, &term_count,
&term_allocated, &dataset);
result = sql_exec_one_statement(statement, async_command, &term_count,
&term_allocated, &dataset);
if (result) {
break;
}
}
}

Expand Down
33 changes: 24 additions & 9 deletions src/sqlite3.erl
Expand Up @@ -218,10 +218,11 @@ sql_exec_timeout(Db, SQL, Params, Timeout) ->
%% @spec sql_exec_script(Db :: atom(), Sql :: iodata()) -> [sql_result()]
%% @doc
%% Executes the Sql script (consisting of semicolon-separated statements)
%% directly on the Db database. Returns the list of their results (same as
%% if sql_exec/2 was called for all of them in order, but more efficient).
%% Note that any whitespace or comments after the last semicolon will be
%% considered an empty statement and produce the corresponding error.
%% directly on the Db database.
%%
%% If an error happens while executing a statement, no further statements are executed.
%%
%% The return value is the list of results of all executed statements.
%% @end
%%--------------------------------------------------------------------
-spec sql_exec_script(atom(), iodata()) -> [sql_result()].
Expand All @@ -232,10 +233,11 @@ sql_exec_script(Db, SQL) ->
%% @spec sql_exec_script_timeout(Db :: atom(), Sql :: iodata(), Timeout :: timeout()) -> [sql_result()]
%% @doc
%% Executes the Sql script (consisting of semicolon-separated statements)
%% directly on the Db database. Returns the list of their results (same as
%% if sql_exec/3 was called for all of them in order, but more efficient).
%% Note that any whitespace or comments after the last semicolon will be
%% considered an empty statement and produce the corresponding error.
%% directly on the Db database.
%%
%% If an error happens while executing a statement, no further statements are executed.
%%
%% The return value is the list of results of all executed statements.
%% @end
%%--------------------------------------------------------------------
-spec sql_exec_script_timeout(atom(), iodata(), timeout()) -> [sql_result()].
Expand Down Expand Up @@ -1077,7 +1079,20 @@ do_sql_bind_and_exec(SQL, Params, #state{port = Port}) ->

do_sql_exec_script(SQL, #state{port = Port}) ->
?dbgF("SQL: ~s~n", [SQL]),
exec(Port, {sql_exec_script, SQL}).
Results = exec(Port, {sql_exec_script, SQL}),
%% last element of Results may be an error
case Results of
[_|_] ->
case lists:last(Results) of
{error, _Code, Reason} ->
error_logger:error_msg("sqlite3 driver error: ~s~n",
[Reason]);
_ -> ok
end;
_ ->
ok
end,
Results.

exec(_Port, {create_function, _FunctionName, _Function}) ->
error_logger:error_report([{application, sqlite3}, "NOT IMPL YET"]);
Expand Down
2 changes: 0 additions & 2 deletions test/sqlite3_test.erl
Expand Up @@ -210,7 +210,6 @@ large_number() ->
Query1 = io_lib:format("select ~p, ~p", [N1, N2]),
?assertEqual([{N1, N2}], rows(sqlite3:sql_exec(ct, Query1))),
Query2 = "select ?, ?",
?debugMsg("Error message \"sqlite3 driver error: bind or column index out of range\" should be shown..."),
?assertEqual([{N1, N2}], rows(sqlite3:sql_exec(ct, Query2, [N1, N2]))),
?assertNot([{N1 + 1, N2 - 1}] == rows(sqlite3:sql_exec(ct, Query2, [N1 + 1, N2 - 1]))).

Expand Down Expand Up @@ -262,7 +261,6 @@ script_test() ->
"INSERT INTO person (id) VALUES (2);",
" "
], "\n"),
?WARN_ERROR_MESSAGE,
?assertEqual(
[ok, ok, ok],
sqlite3:sql_exec_script(script, Script)),
Expand Down

0 comments on commit d35c52a

Please sign in to comment.