Browse Source

Add option {name, ServerName} and mysql_connection:start_link/1

Viktor Söderqvist 10 years ago
parent
commit
f63603cfb9
3 changed files with 59 additions and 10 deletions
  1. 38 7
      src/mysql.erl
  2. 17 1
      src/mysql_connection.erl
  3. 4 2
      test/mysql_tests.erl

+ 38 - 7
src/mysql.erl

@@ -43,15 +43,46 @@
 %% @doc Starts a connection gen_server process and connects to a database. To
 %% disconnect just do `exit(Pid, normal)'.
 %%
-%% This is just a wrapper for `gen_server:start_link(mysql_connection, Options,
-%% [])'. If you need to specify gen_server options, use gen_server:start_link/3
-%% directly.
+%% Note that the callback module for the gen_server is `mysql_connection'.
+%% This function is actually a synonym for mysql_connection:start_link/1. In
+%% some applications (e.g. Poolboy) the callback module needs to be the module
+%% where the start_link function is located. In those cases use
+%% mysql_connection:start_link/1 instead.
+%%
+%% Options:
+%%
+%% <dl>
+%%   <dt>`{name, ServerName}'</dt>
+%%   <dd>If a name is provided, the gen_server will be registered with this
+%%       name. For detauls, see the documentation for the first argument to
+%%       gen_server:start_link/4.</dd>
+%%   <dt>`{host, Host}'</dt>
+%%   <dd>Hostname of the MySQL database; default `"localhost"'.</dd>
+%%   <dt>`{port, Port}'</dt>
+%%   <dd>Port; default 3306 if omitted.</dd>
+%%   <dt>`{user, User}'</dt>
+%%   <dd>Username.</dd>
+%%   <dt>`{password, Password}'</dt>
+%%   <dd>Password.</dd>
+%%   <dt>`{database, Database}'</dt>
+%%   <dd>The name of the database AKA schema to use. This can be changed later
+%%       using the query `USE <database>'.</dd>
+%% </dl>
+%%
+%% TODO: Implement {database, Database}. Currently the database has to be
+%% selected using a `USE <database>' query after connecting.
+%%
+%% @see mysql_connection:start_link/1
 -spec start_link(Options) -> {ok, pid()} | ignore | {error, term()}
     when Options :: [Option],
-         Option :: {host, iodata()} | {port, integer()} | {user, iodata()} |
-                   {password, iodata()} | {database, iodata()}.
-start_link(Opts) ->
-    gen_server:start_link(mysql_connection, Opts, []).
+         Option :: {name, ServerName} | {host, iodata()} | {port, integer()} | 
+                   {user, iodata()} | {password, iodata()} |
+                   {database, iodata()},
+         ServerName :: {local, Name :: atom()} |
+                       {global, GlobalName :: term()} |
+                       {via, Module :: atom(), ViaName :: term()}.
+start_link(Options) ->
+    mysql_connection:start_link(Options).
 
 %% @doc Executes a query.
 -spec query(Conn, Query) -> ok | {ok, ColumnNames, Rows} | {error, Reason}

+ 17 - 1
src/mysql_connection.erl

@@ -21,10 +21,13 @@
 -module(mysql_connection).
 -behaviour(gen_server).
 
+%% API functions
+-export([start_link/1]).
+
+%% Gen_server callbacks
 -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
          code_change/3]).
 
-%% Some defaults
 -define(default_host, "localhost").
 -define(default_port, 3306).
 -define(default_user, <<>>).
@@ -42,6 +45,19 @@
 %% {error, reason()}.
 -type reason() :: {Code :: integer(), SQLState :: binary(), Msg :: binary()}.
 
+%% @doc Starts a connection gen_server and links to it. The options are
+%% described in mysql:start_link/1.
+%% @see mysql:start_link/1.
+start_link(Options) ->
+    case proplists:get_value(name, Options) of
+        undefined ->
+            gen_server:start_link(mysql_connection, Options, []);
+        ServerName ->
+            gen_server:start_link(ServerName, mysql_connection, Options, [])
+    end.
+
+%% --- Gen_server callbacks ---
+
 init(Opts) ->
     %% Connect
     Host     = proplists:get_value(host,     Opts, ?default_host),

+ 4 - 2
test/mysql_tests.erl

@@ -37,8 +37,10 @@
                           ") ENGINE=InnoDB">>).
 
 connect_test() ->
-    {ok, Pid} = mysql:start_link([{user, ?user}, {password, ?password}]),
-    exit(Pid, normal).
+    %% A connection with a registered name
+    Options = [{name, {local, tardis}}, {user, ?user}, {password, ?password}],
+    ?assertMatch({ok, Pid} when is_pid(Pid), mysql:start_link(Options)),
+    exit(whereis(tardis), normal).
 
 query_test_() ->
     {setup,