Просмотр исходного кода

Do not construct a deep list for skipped commands but simply return {error, #error{}} and update README

Enid Gjoleka 5 лет назад
Родитель
Сommit
1eebe39468
3 измененных файлов с 9 добавлено и 12 удалено
  1. 5 4
      README.md
  2. 2 4
      src/commands/epgsql_cmd_batch.erl
  3. 2 4
      test/epgsql_SUITE.erl

+ 5 - 4
README.md

@@ -430,10 +430,11 @@ epgsql:execute_batch(C, "INSERT INTO account (name, age) VALUES ($1, $2) RETURNI
                      [ ["Joe", 35], ["Paul", 26], ["Mary", 24] ]).
 ```
 
-In case one of the batch items causes an error, the returned result will be
-`{error, [{error, #error{}} | {error, skipped}]}`. The first element of the list
-will represent the received error. The remaining elements will represent
-every skipped command.
+In case one of the batch items causes an error, all the remaining queries of
+that batch will be ignored. So, last element of the result list will be 
+`{error, #error{}}` and the length of the result list might be shorter that 
+the length of the batch. For a better illustration of such scenario please 
+refer to `epgsql_SUITE:batch_error/1`
 
 `epgsqla:execute_batch/{2,3}` sends `{C, Ref, Results}`
 

+ 2 - 4
src/commands/epgsql_cmd_batch.erl

@@ -36,7 +36,7 @@
 -type response() :: [{ok, Count :: non_neg_integer(), Rows :: [tuple()]}
                      | {ok, Count :: non_neg_integer()}
                      | {ok, Rows :: [tuple()]}
-                     | {error, [{error, epgsql:query_error()} | {error, skipped}]}
+                     | {error, epgsql:query_error()}
                      ].
 -type state() :: #batch{}.
 
@@ -116,9 +116,7 @@ handle_message(?READY_FOR_QUERY, _Status, Sock, _State) ->
     {finish, Results, done, Sock};
 handle_message(?ERROR, Error, Sock, #batch{batch = [_ | Batch]} = State) ->
     Result = {error, Error},
-    Skipped = lists:duplicate(length(Batch), {error, skipped}),
-    FinalResult = {error, [Result | Skipped]},
-    {add_result, FinalResult, FinalResult, Sock, State#batch{batch = Batch}};
+    {add_result, Result, Result, Sock, State#batch{batch = Batch}};
 handle_message(_, _, _, _) ->
     unknown.
 

+ 2 - 4
test/epgsql_SUITE.erl

@@ -479,7 +479,7 @@ batch_error(Config) ->
     Module = ?config(module, Config),
     epgsql_ct:with_rollback(Config, fun(C) ->
         {ok, S} = Module:parse(C, "insert into test_table1(id, value) values($1, $2)"),
-        [{ok, 1}, {error, [Error, Skipped1, Skipped2]}] =
+        [{ok, 1}, {error, Error}] =
             Module:execute_batch(
               C,
               [{S, [3, "batch_error 3"]},
@@ -487,9 +487,7 @@ batch_error(Config) ->
                {S, [5, "batch_error 5"]},  % won't be executed
                {S, [6, "batch_error 6"]}  % won't be executed
               ]),
-        ?assertMatch({error, #error{}}, Error),
-        ?assertEqual({error, skipped}, Skipped1),
-        ?assertEqual({error, skipped}, Skipped2)
+        ?assertMatch(#error{}, Error)
     end).
 
 single_batch(Config) ->