Fixed tests and documentation.

This commit is contained in:
Alexey Romanov
2011-05-10 12:03:47 +04:00
parent b0af67943d
commit d35c52a370
3 changed files with 35 additions and 17 deletions

View File

@@ -594,7 +594,7 @@ static void sql_free_async(void *_async_command) {
driver_free(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, sqlite3_stmt *statement, async_sqlite3_command *async_command,
int *term_count_p, int *term_allocated_p, ErlDrvTermData **dataset_p) { int *term_count_p, int *term_allocated_p, ErlDrvTermData **dataset_p) {
int column_count = sqlite3_column_count(statement); int column_count = sqlite3_column_count(statement);
@@ -740,14 +740,14 @@ static void sql_exec_one_statement(
dataset_p, term_count_p, dataset_p, term_count_p,
term_allocated_p, &async_command->error_code); term_allocated_p, &async_command->error_code);
async_command->finalize_statement_on_free = 1; async_command->finalize_statement_on_free = 1;
return; return next_row;
} }
if (next_row != SQLITE_DONE) { if (next_row != SQLITE_DONE) {
return_error(drv, next_row, sqlite3_errmsg(drv->db), return_error(drv, next_row, sqlite3_errmsg(drv->db),
dataset_p, term_count_p, dataset_p, term_count_p,
term_allocated_p, &async_command->error_code); term_allocated_p, &async_command->error_code);
async_command->finalize_statement_on_free = 1; async_command->finalize_statement_on_free = 1;
return; return next_row;
} }
if (column_count > 0) { if (column_count > 0) {
@@ -796,6 +796,8 @@ static void sql_exec_one_statement(
fflush(drv->log); fflush(drv->log);
#endif #endif
async_command->finalize_statement_on_free = 1; async_command->finalize_statement_on_free = 1;
return 0;
} }
static void sql_exec_async(void *_async_command) { static void sql_exec_async(void *_async_command) {
@@ -836,16 +838,19 @@ static void sql_exec_async(void *_async_command) {
} }
result = sqlite3_prepare_v2(drv->db, rest, end - rest, &statement, &rest); result = sqlite3_prepare_v2(drv->db, rest, end - rest, &statement, &rest);
if (result != SQLITE_OK) { if (result != SQLITE_OK) {
num_statements++;
return_error(drv, result, sqlite3_errmsg(drv->db), &dataset, return_error(drv, result, sqlite3_errmsg(drv->db), &dataset,
&term_count, &term_allocated, &async_command->error_code); &term_count, &term_allocated, &async_command->error_code);
num_statements++;
break; break;
} else if (statement == NULL) { } else if (statement == NULL) {
break; break;
} else { } else {
num_statements++; num_statements++;
sql_exec_one_statement(statement, async_command, &term_count, result = sql_exec_one_statement(statement, async_command, &term_count,
&term_allocated, &dataset); &term_allocated, &dataset);
if (result) {
break;
}
} }
} }

View File

@@ -218,10 +218,11 @@ sql_exec_timeout(Db, SQL, Params, Timeout) ->
%% @spec sql_exec_script(Db :: atom(), Sql :: iodata()) -> [sql_result()] %% @spec sql_exec_script(Db :: atom(), Sql :: iodata()) -> [sql_result()]
%% @doc %% @doc
%% Executes the Sql script (consisting of semicolon-separated statements) %% Executes the Sql script (consisting of semicolon-separated statements)
%% directly on the Db database. Returns the list of their results (same as %% directly on the Db database.
%% 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 %% If an error happens while executing a statement, no further statements are executed.
%% considered an empty statement and produce the corresponding error. %%
%% The return value is the list of results of all executed statements.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec sql_exec_script(atom(), iodata()) -> [sql_result()]. -spec sql_exec_script(atom(), iodata()) -> [sql_result()].
@@ -232,10 +233,11 @@ sql_exec_script(Db, SQL) ->
%% @spec sql_exec_script_timeout(Db :: atom(), Sql :: iodata(), Timeout :: timeout()) -> [sql_result()] %% @spec sql_exec_script_timeout(Db :: atom(), Sql :: iodata(), Timeout :: timeout()) -> [sql_result()]
%% @doc %% @doc
%% Executes the Sql script (consisting of semicolon-separated statements) %% Executes the Sql script (consisting of semicolon-separated statements)
%% directly on the Db database. Returns the list of their results (same as %% directly on the Db database.
%% 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 %% If an error happens while executing a statement, no further statements are executed.
%% considered an empty statement and produce the corresponding error. %%
%% The return value is the list of results of all executed statements.
%% @end %% @end
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-spec sql_exec_script_timeout(atom(), iodata(), timeout()) -> [sql_result()]. -spec sql_exec_script_timeout(atom(), iodata(), timeout()) -> [sql_result()].
@@ -1077,7 +1079,20 @@ do_sql_bind_and_exec(SQL, Params, #state{port = Port}) ->
do_sql_exec_script(SQL, #state{port = Port}) -> do_sql_exec_script(SQL, #state{port = Port}) ->
?dbgF("SQL: ~s~n", [SQL]), ?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}) -> exec(_Port, {create_function, _FunctionName, _Function}) ->
error_logger:error_report([{application, sqlite3}, "NOT IMPL YET"]); error_logger:error_report([{application, sqlite3}, "NOT IMPL YET"]);

View File

@@ -210,7 +210,6 @@ large_number() ->
Query1 = io_lib:format("select ~p, ~p", [N1, N2]), Query1 = io_lib:format("select ~p, ~p", [N1, N2]),
?assertEqual([{N1, N2}], rows(sqlite3:sql_exec(ct, Query1))), ?assertEqual([{N1, N2}], rows(sqlite3:sql_exec(ct, Query1))),
Query2 = "select ?, ?", 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]))), ?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]))). ?assertNot([{N1 + 1, N2 - 1}] == rows(sqlite3:sql_exec(ct, Query2, [N1 + 1, N2 - 1]))).
@@ -262,7 +261,6 @@ script_test() ->
"INSERT INTO person (id) VALUES (2);", "INSERT INTO person (id) VALUES (2);",
" " " "
], "\n"), ], "\n"),
?WARN_ERROR_MESSAGE,
?assertEqual( ?assertEqual(
[ok, ok, ok], [ok, ok, ok],
sqlite3:sql_exec_script(script, Script)), sqlite3:sql_exec_script(script, Script)),