From 1285bd52c95a566a4b58e174cbc1fa014e432ae7 Mon Sep 17 00:00:00 2001 From: Badlop Date: Thu, 22 Jul 2010 18:50:07 +0200 Subject: [PATCH] Fix ejabberd_odbc code --- src/odbc/ejabberd_odbc.erl | 13 +++++-------- src/odbc/ejabberd_odbc_sup.erl | 26 +++++++++++++++++++------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/odbc/ejabberd_odbc.erl b/src/odbc/ejabberd_odbc.erl index 72e56f6de..bc7f9cada 100644 --- a/src/odbc/ejabberd_odbc.erl +++ b/src/odbc/ejabberd_odbc.erl @@ -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 diff --git a/src/odbc/ejabberd_odbc_sup.erl b/src/odbc/ejabberd_odbc_sup.erl index 819028d44..8aa924742 100644 --- a/src/odbc/ejabberd_odbc_sup.erl +++ b/src/odbc/ejabberd_odbc_sup.erl @@ -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).