Browse Source

Merge branch 'batch-error' into async-integration

Anton Lebedevich 12 years ago
parent
commit
7b6c71e5b3
3 changed files with 27 additions and 6 deletions
  1. 4 2
      src/pgsql_sock.erl
  2. 1 4
      test_src/pgsql_incremental.erl
  3. 22 0
      test_src/pgsql_tests.erl

+ 4 - 2
src/pgsql_sock.erl

@@ -597,13 +597,15 @@ on_message({$I, _Bin}, State) ->
 %% ReadyForQuery
 on_message({$Z, <<Status:8>>}, State) ->
     State2 = case command_tag(State) of
-                 C when C == squery; C == execute_batch ->
+                 squery ->
                      case State#state.results of
                          [Result] ->
                              finish(State, done, Result);
                          Results ->
                              finish(State, done, lists:reverse(Results))
                      end;
+                 execute_batch ->
+                     finish(State, done, lists:reverse(State#state.results));
                  equery ->
                      case State#state.results of
                          [Result] ->
@@ -618,7 +620,7 @@ on_message({$Z, <<Status:8>>}, State) ->
 
 on_message(Error = {error, _}, State) ->
     State2 = case command_tag(State) of
-                 C when C == squery; C == equery ->
+                 C when C == squery; C == equery; C == execute_batch ->
                      add_result(State, Error, Error);
                  _ ->
                      sync_required(finish(State, Error))

+ 1 - 4
test_src/pgsql_incremental.erl

@@ -95,10 +95,7 @@ execute(C, S, PortalName, N) ->
 
 execute_batch(C, Batch) ->
     Ref = ipgsql:execute_batch(C, Batch),
-    case receive_extended_results(C, Ref, []) of
-        [Result] -> Result;
-        Results  -> Results
-    end.
+    receive_extended_results(C, Ref, []).
 
 %% statement/portal functions
 

+ 22 - 0
test_src/pgsql_tests.erl

@@ -159,6 +159,28 @@ execute_batch_test(Module) ->
                   Module:execute_batch(C, [{S1, [1]}, {S2, [1, 2]}])
       end).
 
+batch_error_test(Module) ->
+    with_rollback(
+      Module,
+      fun(C) ->
+              {ok, S} = Module:parse(C, "insert into test_table1(id, value) values($1, $2)"),
+              [{ok, 1}, {error, _}] =
+                  Module:execute_batch(
+                    C,
+                    [{S, [3, "batch_error 3"]},
+                     {S, [2, "batch_error 2"]}, % duplicate key error
+                     {S, [5, "batch_error 5"]}  % won't be executed
+                    ])
+      end).
+
+single_batch_test(Module) ->
+    with_connection(
+      Module,
+      fun(C) ->
+              {ok, S1} = Module:parse(C, "one", "select $1", [int4]),
+              [{ok, [{1}]}] = Module:execute_batch(C, [{S1, [1]}])
+      end).
+
 extended_select_test(Module) ->
     with_connection(
       Module,