Browse Source

Execute qeries and prepared statements on connect

Viktor Söderqvist 10 years ago
parent
commit
f43a6a777f
2 changed files with 42 additions and 5 deletions
  1. 34 3
      src/mysql.erl
  2. 8 2
      test/mysql_tests.erl

+ 34 - 3
src/mysql.erl

@@ -92,6 +92,13 @@
 %%       `false' and `integer() > 0' for an explicit interval in milliseconds.
 %%       `false' and `integer() > 0' for an explicit interval in milliseconds.
 %%       The default is `false'. For `true' a default ping timeout is used.
 %%       The default is `false'. For `true' a default ping timeout is used.
 %%       </dd>
 %%       </dd>
+%%   <dt>`{prepare, NamedStatements}'</dt>
+%%   <dd>Named prepared statements to be created as soon as the connection is
+%%       ready.</dd>
+%%   <dt>`{queries, Queries}'</dt>
+%%   <dd>Queries to be executed as soon as the connection is ready. Any results
+%%       are discarded. Typically, this is used for setting time zone and other
+%%       session variables.</dd>
 %%   <dt>`{query_timeout, Timeout}'</dt>
 %%   <dt>`{query_timeout, Timeout}'</dt>
 %%   <dd>The default time to wait for a response when executing a query or a
 %%   <dd>The default time to wait for a response when executing a query or a
 %%       prepared statement. This can be given per query using `query/3,4' and
 %%       prepared statement. This can be given per query using `query/3,4' and
@@ -107,20 +114,44 @@
                    {database, iodata()} |
                    {database, iodata()} |
                    {connect_timeout, timeout()} |
                    {connect_timeout, timeout()} |
                    {log_warnings, boolean()} |
                    {log_warnings, boolean()} |
+                   {keep_alive, boolean() | timeout()} |
+                   {prepare, NamedStatements} |
+                   {queries, [iodata()]} |
                    {query_timeout, timeout()} |
                    {query_timeout, timeout()} |
                    {query_cache_time, non_neg_integer()},
                    {query_cache_time, non_neg_integer()},
          ServerName :: {local, Name :: atom()} |
          ServerName :: {local, Name :: atom()} |
                        {global, GlobalName :: term()} |
                        {global, GlobalName :: term()} |
-                       {via, Module :: atom(), ViaName :: term()}.
+                       {via, Module :: atom(), ViaName :: term()},
+         NamedStatements :: [{StatementName :: atom(), Statement :: iodata()}].
 start_link(Options) ->
 start_link(Options) ->
     GenSrvOpts = [{timeout, proplists:get_value(connect_timeout, Options,
     GenSrvOpts = [{timeout, proplists:get_value(connect_timeout, Options,
                                                 ?default_connect_timeout)}],
                                                 ?default_connect_timeout)}],
-    case proplists:get_value(name, Options) of
+    Ret = case proplists:get_value(name, Options) of
         undefined ->
         undefined ->
             gen_server:start_link(?MODULE, Options, GenSrvOpts);
             gen_server:start_link(?MODULE, Options, GenSrvOpts);
         ServerName ->
         ServerName ->
             gen_server:start_link(ServerName, ?MODULE, Options, GenSrvOpts)
             gen_server:start_link(ServerName, ?MODULE, Options, GenSrvOpts)
-    end.
+    end,
+    case Ret of
+        {ok, Pid} ->
+            %% Initial queries
+            Queries = proplists:get_value(queries, Options, []),
+            lists:foreach(fun (Query) ->
+                              case mysql:query(Pid, Query) of
+                                  ok -> ok;
+                                  {ok, _, _} -> ok
+                              end
+                          end,
+                          Queries),
+            %% Prepare
+            Prepare = proplists:get_value(prepare, Options, []),
+            lists:foreach(fun ({Name, Stmt}) ->
+                              {ok, Name} = mysql:prepare(Pid, Name, Stmt)
+                          end,
+                          Prepare);
+        _ -> ok
+    end,
+    Ret.
 
 
 %% @doc Executes a query with the query timeout as given to start_link/1.
 %% @doc Executes a query with the query timeout as given to start_link/1.
 -spec query(Conn, Query) -> ok | {ok, ColumnNames, Rows} | {error, Reason}
 -spec query(Conn, Query) -> ok | {ok, ColumnNames, Rows} | {error, Reason}

+ 8 - 2
test/mysql_tests.erl

@@ -50,9 +50,15 @@ failing_connect_test() ->
     process_flag(trap_exit, false).
     process_flag(trap_exit, false).
 
 
 successful_connect_test() ->
 successful_connect_test() ->
-    %% A connection with a registered name
-    Options = [{name, {local, tardis}}, {user, ?user}, {password, ?password}],
+    %% A connection with a registered name and execute initial queries and
+    %% create prepared statements.
+    Options = [{name, {local, tardis}}, {user, ?user}, {password, ?password},
+               {queries, ["SET @foo = 'bar'"]},
+               {prepare, [{foo, "SELECT @foo"}]}],
     {ok, Pid} = mysql:start_link(Options),
     {ok, Pid} = mysql:start_link(Options),
+    %% Check that queries and prepare has been done.
+    ?assertEqual({ok, [<<"@foo">>], [[<<"bar">>]]},
+                 mysql:execute(Pid, foo, [])),
     %% Test some gen_server callbacks not tested elsewhere
     %% Test some gen_server callbacks not tested elsewhere
     State = get_state(Pid),
     State = get_state(Pid),
     ?assertMatch({ok, State}, mysql:code_change("0.1.0", State, [])),
     ?assertMatch({ok, State}, mysql:code_change("0.1.0", State, [])),