diff options
Diffstat (limited to 'src/odbc')
-rw-r--r-- | src/odbc/ejabberd_odbc.erl | 123 | ||||
-rw-r--r-- | src/odbc/pg.sql | 89 |
2 files changed, 212 insertions, 0 deletions
diff --git a/src/odbc/ejabberd_odbc.erl b/src/odbc/ejabberd_odbc.erl new file mode 100644 index 00000000..c496bf2b --- /dev/null +++ b/src/odbc/ejabberd_odbc.erl @@ -0,0 +1,123 @@ +%%%---------------------------------------------------------------------- +%%% File : ejabberd_odbc.erl +%%% Author : Alexey Shchepin <alexey@sevcom.net> +%%% Purpose : Serve ODBC connection +%%% Created : 8 Dec 2004 by Alexey Shchepin <alexey@sevcom.net> +%%% Id : $Id$ +%%%---------------------------------------------------------------------- + +-module(ejabberd_odbc). +-author('alexey@sevcom.net'). +-vsn('$Revision$ '). + +-behaviour(gen_server). + +%% External exports +-export([start/0, start_link/0, + sql_query/1, + escape/1]). + +%% gen_server callbacks +-export([init/1, + handle_call/3, + handle_cast/2, + code_change/3, + handle_info/2, + terminate/2]). + +-record(state, {odbc_ref}). + +%%%---------------------------------------------------------------------- +%%% API +%%%---------------------------------------------------------------------- +start() -> + gen_server:start({local, ejabberd_odbc}, ejabberd_odbc, [], []). + +start_link() -> + gen_server:start_link({local, ejabberd_odbc}, ejabberd_odbc, [], []). + +sql_query(Query) -> + gen_server:call(ejabberd_odbc, {sql_query, Query}, 60000). + +escape(S) -> + [case C of + $\0 -> "\\0"; + $\n -> "\\n"; + $\t -> "\\t"; + $\b -> "\\b"; + $\r -> "\\r"; + $' -> "\\'"; + $" -> "\\\""; + $% -> "\\%"; + $_ -> "\\_"; + $\\ -> "\\\\"; + _ -> C + end || C <- S]. + + +%%%---------------------------------------------------------------------- +%%% Callback functions from gen_server +%%%---------------------------------------------------------------------- + +%%---------------------------------------------------------------------- +%% Func: init/1 +%% Returns: {ok, State} | +%% {ok, State, Timeout} | +%% ignore | +%% {stop, Reason} +%%---------------------------------------------------------------------- +init([]) -> + {ok, Ref} = odbc:connect("DSN=ejabberd;UID=ejabberd;PWD=ejabberd", + [{scrollable_cursors, off}]), + {ok, #state{odbc_ref = Ref}}. + +%%---------------------------------------------------------------------- +%% Func: handle_call/3 +%% Returns: {reply, Reply, State} | +%% {reply, Reply, State, Timeout} | +%% {noreply, State} | +%% {noreply, State, Timeout} | +%% {stop, Reason, Reply, State} | (terminate/2 is called) +%% {stop, Reason, State} (terminate/2 is called) +%%---------------------------------------------------------------------- +handle_call({sql_query, Query}, _From, State) -> + Reply = odbc:sql_query(State#state.odbc_ref, Query), + {reply, Reply, State}; +handle_call(_Request, _From, State) -> + Reply = ok, + {reply, Reply, State}. + +%%---------------------------------------------------------------------- +%% Func: handle_cast/2 +%% Returns: {noreply, State} | +%% {noreply, State, Timeout} | +%% {stop, Reason, State} (terminate/2 is called) +%%---------------------------------------------------------------------- +handle_cast(_Msg, State) -> + {noreply, State}. + + +code_change(_OldVsn, State, _Extra) -> + {ok, State}. + +%%---------------------------------------------------------------------- +%% Func: handle_info/2 +%% Returns: {noreply, State} | +%% {noreply, State, Timeout} | +%% {stop, Reason, State} (terminate/2 is called) +%%---------------------------------------------------------------------- +handle_info(_Info, State) -> + {noreply, State}. + +%%---------------------------------------------------------------------- +%% Func: terminate/2 +%% Purpose: Shutdown the server +%% Returns: any (ignored by gen_server) +%%---------------------------------------------------------------------- +terminate(_Reason, _State) -> + ok. + +%%%---------------------------------------------------------------------- +%%% Internal functions +%%%---------------------------------------------------------------------- + diff --git a/src/odbc/pg.sql b/src/odbc/pg.sql new file mode 100644 index 00000000..9e0c154d --- /dev/null +++ b/src/odbc/pg.sql @@ -0,0 +1,89 @@ + + +CREATE TABLE users ( + username text NOT NULL, + "password" text NOT NULL +); + + +CREATE TABLE last ( + username text NOT NULL, + seconds text NOT NULL, + state text +); + + +CREATE TABLE rosterusers ( + username text NOT NULL, + jid text NOT NULL, + nick text, + subscription character(1) NOT NULL, + ask character(1) NOT NULL, + server character(1) NOT NULL, + subscribe text, + "type" text +); + + + +CREATE TABLE rostergroups ( + username text NOT NULL, + jid text NOT NULL, + grp text NOT NULL +); + + +CREATE TABLE spool ( + username text NOT NULL, + xml text +); + + + +CREATE TABLE vcard ( + username text NOT NULL, + full_name text, + first_name text, + last_name text, + nick_name text, + url text, + address1 text, + address2 text, + locality text, + region text, + pcode text, + country text, + telephone text, + email text, + orgname text, + orgunit text, + title text, + role text, + b_day date, + descr text +); + + + + +CREATE INDEX i_users_login ON users USING btree (username, "password"); + +CREATE INDEX i_rosteru_user_jid ON rosterusers USING btree (username, jid); + +CREATE INDEX i_rosteru_username ON rosterusers USING btree (username); + +CREATE INDEX pk_rosterg_user_jid ON rostergroups USING btree (username, jid); + +CREATE INDEX i_despool ON spool USING btree (username); + +CREATE INDEX i_rosteru_jid ON rosterusers USING btree (jid); + +ALTER TABLE ONLY users + ADD CONSTRAINT users_pkey PRIMARY KEY (username); + +ALTER TABLE ONLY last + ADD CONSTRAINT last_pkey PRIMARY KEY (username); + +ALTER TABLE ONLY vcard + ADD CONSTRAINT vcard_pkey PRIMARY KEY (username); + |