summaryrefslogtreecommitdiff
path: root/src/mod_vcard_xupdate_sql.erl
diff options
context:
space:
mode:
authorEvgeniy Khramtsov <ekhramtsov@process-one.net>2016-04-13 11:06:59 +0300
committerEvgeniy Khramtsov <ekhramtsov@process-one.net>2016-04-13 11:06:59 +0300
commit2d7e03f5e1ee09f006b206a18365ae07b855c875 (patch)
treed83af66325534f4d7f75aeebfad9416342c25d8b /src/mod_vcard_xupdate_sql.erl
parentClean mod_last.erl from DB specific code (diff)
Clean mod_vcard_xupdate.erl from DB specific code
Diffstat (limited to 'src/mod_vcard_xupdate_sql.erl')
-rw-r--r--src/mod_vcard_xupdate_sql.erl79
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 00000000..d580b151
--- /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
+%%%===================================================================