25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-26 16:26:24 +01:00

Fix ejabberd_odbc code

This commit is contained in:
Badlop 2010-07-22 18:50:07 +02:00
parent 597c1c87d4
commit 1285bd52c9
2 changed files with 24 additions and 15 deletions

View File

@ -166,8 +166,7 @@ sql_query_t(Query) ->
end.
db_type(Host) ->
?GEN_FSM:sync_send_event(ejabberd_odbc_sup:get_random_pid(Host),
db_type, 60000).
ejabberd_odbc_sup:get_dbtype(Host).
%% Escape character that will confuse an SQL engine
escape(S) when is_list(S) ->
@ -208,7 +207,7 @@ init([Host, StartInterval]) ->
end,
[DBType | _] = db_opts(Host),
?GEN_FSM:send_event(self(), connect),
ejabberd_odbc_sup:add_pid(Host, self()),
ejabberd_odbc_sup:add_pid(Host, self(), DBType),
{ok, connecting, #state{db_type = DBType,
host = Host,
max_pending_requests_len = max_fsm_queue(),
@ -282,9 +281,6 @@ session_established(Request, {Who, _Ref}, State) ->
session_established({sql_cmd, Command, From, Timestamp}, State) ->
run_sql_cmd(Command, From, State, Timestamp);
session_established(db_type, State) ->
Reply = State#state.db_type,
{reply, Reply, session_established, State};
session_established(Event, State) ->
?WARNING_MSG("unexpected event in 'session_established': ~p", [Event]),
{next_state, session_established, State}.
@ -308,8 +304,9 @@ handle_info(Info, StateName, State) ->
{next_state, StateName, State}.
terminate(_Reason, _StateName, State) ->
ejabberd_odbc_sup:remove_pid(State#state.host, self()),
case State#state.db_type of
DBType = State#state.db_type,
ejabberd_odbc_sup:remove_pid(State#state.host, self(), DBType),
case DBType of
mysql ->
%% old versions of mysql driver don't have the stop function
%% so the catch

View File

@ -30,8 +30,9 @@
%% API
-export([start_link/1,
init/1,
add_pid/2,
remove_pid/2,
add_pid/3,
remove_pid/3,
get_dbtype/1,
get_pids/1,
get_random_pid/1
]).
@ -46,7 +47,7 @@
-define(CONNECT_TIMEOUT, 500). % milliseconds
-record(sql_pool, {host, pid}).
-record(sql_pool, {host, pid, dbtype}).
start_link(Host) ->
mnesia:create_table(sql_pool,
@ -112,18 +113,29 @@ get_random_pid(Host) ->
Pids = get_pids(ejabberd:normalize_host(Host)),
lists:nth(erlang:phash(now(), length(Pids)), Pids).
add_pid(Host, Pid) ->
get_dbtype(Host) ->
case ejabberd_config:get_local_option({odbc_server, Host}) of
{host, Host1} ->
get_dbtype(Host1);
_ ->
[#sql_pool{dbtype = Dbtype}|_] = mnesia:dirty_read(sql_pool, Host),
Dbtype
end.
add_pid(Host, Pid, Dbtype) ->
F = fun() ->
mnesia:write(
#sql_pool{host = Host,
pid = Pid})
pid = Pid,
dbtype = Dbtype})
end,
mnesia:ets(F).
remove_pid(Host, Pid) ->
remove_pid(Host, Pid, Dbtype) ->
F = fun() ->
mnesia:delete_object(
#sql_pool{host = Host,
pid = Pid})
pid = Pid,
dbtype = Dbtype})
end,
mnesia:ets(F).