Browse Source

Add autocommit/1

Viktor Söderqvist 10 years ago
parent
commit
a35284b6bf
3 changed files with 18 additions and 2 deletions
  1. 6 1
      src/mysql.erl
  2. 2 0
      src/mysql_connection.erl
  3. 10 1
      test/mysql_tests.erl

+ 6 - 1
src/mysql.erl

@@ -23,7 +23,7 @@
 -module(mysql).
 
 -export([start_link/1, query/2, execute/3, prepare/2, warning_count/1,
-         affected_rows/1, insert_id/1, in_transaction/1,
+         affected_rows/1, autocommit/1, insert_id/1, in_transaction/1,
          transaction/2, transaction/3]).
 
 -export_type([connection/0]).
@@ -86,6 +86,11 @@ warning_count(Conn) ->
 affected_rows(Conn) ->
     gen_server:call(Conn, affected_rows).
 
+%% @doc Returns true if auto-commit is enabled and false otherwise.
+-spec autocommit(connection()) -> boolean().
+autocommit(Conn) ->
+    gen_server:call(Conn, autocommit).
+
 %% @doc Returns the last insert-id.
 -spec insert_id(connection()) -> integer().
 insert_id(Conn) ->

+ 2 - 0
src/mysql_connection.erl

@@ -123,6 +123,8 @@ handle_call(insert_id, _From, State) ->
     {reply, State#state.insert_id, State};
 handle_call(affected_rows, _From, State) ->
     {reply, State#state.affected_rows, State};
+handle_call(autocommit, _From, State) ->
+    {reply, State#state.status band ?SERVER_STATUS_AUTOCOMMIT /= 0, State};
 handle_call(in_transaction, _From, State) ->
     {reply, State#state.status band ?SERVER_STATUS_IN_TRANS /= 0, State};
 handle_call(get_state, _From, State) ->

+ 10 - 1
test/mysql_tests.erl

@@ -47,19 +47,28 @@ query_test_() ->
          ok = mysql:query(Pid, <<"DROP DATABASE IF EXISTS otptest">>),
          ok = mysql:query(Pid, <<"CREATE DATABASE otptest">>),
          ok = mysql:query(Pid, <<"USE otptest">>),
+         ok = mysql:query(Pid, <<"SET autocommit = 1">>),
          Pid
      end,
      fun (Pid) ->
          ok = mysql:query(Pid, <<"DROP DATABASE otptest">>),
          exit(Pid, normal)
      end,
-     {with, [fun basic_queries/1,
+     {with, [fun autocommit/1,
+             fun basic_queries/1,
              fun text_protocol/1,
              fun binary_protocol/1,
              fun float_rounding/1,
              fun time/1,
              fun microseconds/1]}}.
 
+autocommit(Pid) ->
+    ?assert(mysql:autocommit(Pid)),
+    ok = mysql:query(Pid, <<"SET autocommit = 0">>),
+    ?assertNot(mysql:autocommit(Pid)),
+    ok = mysql:query(Pid, <<"SET autocommit = 1">>),
+    ?assert(mysql:autocommit(Pid)).
+
 basic_queries(Pid) ->
 
     %% warning count