* src/odbc/ejabberd_odbc.erl: Experimental support for pgsql

library

SVN Revision: 418
This commit is contained in:
Alexey Shchepin 2005-10-13 01:36:07 +00:00
parent b89eb4a52d
commit a773fb0a25
2 changed files with 42 additions and 6 deletions

View File

@ -1,5 +1,8 @@
2005-10-13 Alexey Shchepin <alexey@sevcom.net>
* src/odbc/ejabberd_odbc.erl: Experimental support for pgsql
library
* src/ejabberd_auth_odbc.erl: Bugfix
* src/mod_roster_odbc.erl: Bugfix

View File

@ -25,7 +25,7 @@
handle_info/2,
terminate/2]).
-record(state, {odbc_ref}).
-record(state, {db_ref, db_type}).
%%%----------------------------------------------------------------------
%%% API
@ -68,10 +68,18 @@ escape(S) ->
%% {stop, Reason}
%%----------------------------------------------------------------------
init([Host]) ->
{ok, Ref} = odbc:connect(ejabberd_config:get_local_option(
{odbc_server, Host}),
[{scrollable_cursors, off}]),
{ok, #state{odbc_ref = Ref}}.
SQLServer = ejabberd_config:get_local_option({odbc_server, Host}),
case SQLServer of
{pgsql, Server, DB, Username, Password} ->
{ok, Ref} = pgsql:connect(Server, DB, Username, Password),
{ok, #state{db_ref = Ref,
db_type = pgsql}};
_ when is_list(SQLServer) ->
{ok, Ref} = odbc:connect(SQLServer,
[{scrollable_cursors, off}]),
{ok, #state{db_ref = Ref,
db_type = odbc}}
end.
%%----------------------------------------------------------------------
%% Func: handle_call/3
@ -83,7 +91,12 @@ init([Host]) ->
%% {stop, Reason, State} (terminate/2 is called)
%%----------------------------------------------------------------------
handle_call({sql_query, Query}, _From, State) ->
Reply = odbc:sql_query(State#state.odbc_ref, Query),
Reply = case State#state.db_type of
odbc ->
odbc:sql_query(State#state.db_ref, Query);
pgsql ->
pgsql_to_odbc(pgsql:squery(State#state.db_ref, Query))
end,
{reply, Reply, State};
handle_call(_Request, _From, State) ->
Reply = ok,
@ -123,3 +136,23 @@ terminate(_Reason, _State) ->
%%% Internal functions
%%%----------------------------------------------------------------------
pgsql_to_odbc({ok, PGSQLResult}) ->
case PGSQLResult of
[Item] ->
pgsql_item_to_odbc(Item);
Items ->
[pgsql_item_to_odbc(Item) || Item <- Items]
end.
pgsql_item_to_odbc({"SELECT", Rows, Recs}) ->
{selected,
[element(1, Row) || Row <- Rows],
[list_to_tuple(Rec) || Rec <- Recs]};
pgsql_item_to_odbc("INSERT " ++ OIDN) ->
[OID, N] = string:tokens(OIDN, " "),
{updated, list_to_integer(N)};
pgsql_item_to_odbc("DELETE " ++ N) ->
{updated, list_to_integer(N)};
pgsql_item_to_odbc(_) ->
{updated,undefined}.