diff options
author | Evgeniy Khramtsov <ekhramtsov@process-one.net> | 2016-04-15 15:12:12 +0300 |
---|---|---|
committer | Evgeniy Khramtsov <ekhramtsov@process-one.net> | 2016-04-15 15:12:12 +0300 |
commit | fb0ecf33612d5de004817824096d950fac5b5cc3 (patch) | |
tree | f29c5855c6737aa11ef1f71dee92a998407df89f /src/mod_vcard_xupdate_sql.erl | |
parent | Add preliminary tests on ACL module and prepare clean-up / refactor (diff) | |
parent | Clean mod_mam.erl from DB specific code (diff) |
Merge branch 'move-db-code'
Diffstat (limited to 'src/mod_vcard_xupdate_sql.erl')
-rw-r--r-- | src/mod_vcard_xupdate_sql.erl | 79 |
1 files changed, 79 insertions, 0 deletions
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 <ekhramtsov@process-one.net> +%%% @copyright (C) 2016, Evgeny Khramtsov +%%% @doc +%%% +%%% @end +%%% Created : 13 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net> +%%%------------------------------------------------------------------- +-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 +%%%=================================================================== |