From a773fb0a25686bdf966dc431e34785a8a7f21be9 Mon Sep 17 00:00:00 2001 From: Alexey Shchepin Date: Thu, 13 Oct 2005 01:36:07 +0000 Subject: [PATCH] * src/odbc/ejabberd_odbc.erl: Experimental support for pgsql library SVN Revision: 418 --- ChangeLog | 3 +++ src/odbc/ejabberd_odbc.erl | 45 +++++++++++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2432292b3..bab0c9fb8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2005-10-13 Alexey Shchepin + * src/odbc/ejabberd_odbc.erl: Experimental support for pgsql + library + * src/ejabberd_auth_odbc.erl: Bugfix * src/mod_roster_odbc.erl: Bugfix diff --git a/src/odbc/ejabberd_odbc.erl b/src/odbc/ejabberd_odbc.erl index e84bce8ff..edb865079 100644 --- a/src/odbc/ejabberd_odbc.erl +++ b/src/odbc/ejabberd_odbc.erl @@ -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}. +