From 2d7e03f5e1ee09f006b206a18365ae07b855c875 Mon Sep 17 00:00:00 2001 From: Evgeniy Khramtsov Date: Wed, 13 Apr 2016 11:06:59 +0300 Subject: Clean mod_vcard_xupdate.erl from DB specific code --- src/mod_vcard_xupdate_sql.erl | 79 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/mod_vcard_xupdate_sql.erl (limited to 'src/mod_vcard_xupdate_sql.erl') diff --git a/src/mod_vcard_xupdate_sql.erl b/src/mod_vcard_xupdate_sql.erl new file mode 100644 index 000000000..d580b1515 --- /dev/null +++ b/src/mod_vcard_xupdate_sql.erl @@ -0,0 +1,79 @@ +%%%------------------------------------------------------------------- +%%% @author Evgeny Khramtsov +%%% @copyright (C) 2016, Evgeny Khramtsov +%%% @doc +%%% +%%% @end +%%% Created : 13 Apr 2016 by Evgeny Khramtsov +%%%------------------------------------------------------------------- +-module(mod_vcard_xupdate_sql). + +%% API +-export([init/2, import/2, add_xupdate/3, get_xupdate/2, remove_xupdate/2, + import/1, export/1]). + +-include("mod_vcard_xupdate.hrl"). + +%%%=================================================================== +%%% API +%%%=================================================================== +init(_Host, _Opts) -> + ok. + +add_xupdate(LUser, LServer, Hash) -> + Username = ejabberd_odbc:escape(LUser), + SHash = ejabberd_odbc:escape(Hash), + F = fun () -> + odbc_queries:update_t(<<"vcard_xupdate">>, + [<<"username">>, <<"hash">>], + [Username, SHash], + [<<"username='">>, Username, <<"'">>]) + end, + ejabberd_odbc:sql_transaction(LServer, F). + +get_xupdate(LUser, LServer) -> + Username = ejabberd_odbc:escape(LUser), + case ejabberd_odbc:sql_query(LServer, + [<<"select hash from vcard_xupdate where " + "username='">>, + Username, <<"';">>]) + of + {selected, [<<"hash">>], [[Hash]]} -> Hash; + _ -> undefined + end. + +remove_xupdate(LUser, LServer) -> + Username = ejabberd_odbc:escape(LUser), + F = fun () -> + ejabberd_odbc:sql_query_t([<<"delete from vcard_xupdate where username='">>, + Username, <<"';">>]) + end, + ejabberd_odbc:sql_transaction(LServer, F). + +export(_Server) -> + [{vcard_xupdate, + fun(Host, #vcard_xupdate{us = {LUser, LServer}, hash = Hash}) + when LServer == Host -> + Username = ejabberd_odbc:escape(LUser), + SHash = ejabberd_odbc:escape(Hash), + [[<<"delete from vcard_xupdate where username='">>, + Username, <<"';">>], + [<<"insert into vcard_xupdate(username, " + "hash) values ('">>, + Username, <<"', '">>, SHash, <<"');">>]]; + (_Host, _R) -> + [] + end}]. + +import(LServer) -> + [{<<"select username, hash from vcard_xupdate;">>, + fun([LUser, Hash]) -> + #vcard_xupdate{us = {LUser, LServer}, hash = Hash} + end}]. + +import(_LServer, _) -> + pass. + +%%%=================================================================== +%%% Internal functions +%%%=================================================================== -- cgit v1.2.3