25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00

Update more SQL queries

This commit is contained in:
Alexey Shchepin 2016-02-26 15:45:28 +03:00
parent 1f9fd25ff8
commit e21f25f5b9
4 changed files with 127 additions and 142 deletions

View File

@ -121,7 +121,8 @@ start_link(Host, StartInterval) ->
[Host, StartInterval], [Host, StartInterval],
fsm_limit_opts() ++ (?FSMOPTS)). fsm_limit_opts() ++ (?FSMOPTS)).
-type sql_query() :: [sql_query() | binary()] | #sql_query{}. -type sql_query() :: [sql_query() | binary()] | #sql_query{} |
fun(() -> any()) | fun((atom(), _) -> any()).
-type sql_query_result() :: {updated, non_neg_integer()} | -type sql_query_result() :: {updated, non_neg_integer()} |
{error, binary()} | {error, binary()} |
{selected, [binary()], {selected, [binary()],

View File

@ -694,12 +694,9 @@ roster_subscribe_t(LUser, LServer, LJID, Item) ->
roster_subscribe_t(_LUser, _LServer, _LJID, Item, roster_subscribe_t(_LUser, _LServer, _LJID, Item,
mnesia) -> mnesia) ->
mnesia:write(Item); mnesia:write(Item);
roster_subscribe_t(LUser, LServer, LJID, Item, odbc) -> roster_subscribe_t(_LUser, _LServer, _LJID, Item, odbc) ->
ItemVals = record_to_string(Item), ItemVals = record_to_row(Item),
Username = ejabberd_odbc:escape(LUser), odbc_queries:roster_subscribe(ItemVals);
SJID = ejabberd_odbc:escape(jid:to_string(LJID)),
odbc_queries:roster_subscribe(LServer, Username, SJID,
ItemVals);
roster_subscribe_t(LUser, LServer, _LJID, Item, riak) -> roster_subscribe_t(LUser, LServer, _LJID, Item, riak) ->
ejabberd_riak:put(Item, roster_schema(), ejabberd_riak:put(Item, roster_schema(),
[{'2i', [{<<"us">>, {LUser, LServer}}]}]). [{'2i', [{<<"us">>, {LUser, LServer}}]}]).
@ -1034,11 +1031,10 @@ update_roster_t(_LUser, _LServer, _LJID, Item,
mnesia) -> mnesia) ->
mnesia:write(Item); mnesia:write(Item);
update_roster_t(LUser, LServer, LJID, Item, odbc) -> update_roster_t(LUser, LServer, LJID, Item, odbc) ->
Username = ejabberd_odbc:escape(LUser), SJID = jid:to_string(LJID),
SJID = ejabberd_odbc:escape(jid:to_string(LJID)), ItemVals = record_to_row(Item),
ItemVals = record_to_string(Item), ItemGroups = Item#roster.groups,
ItemGroups = groups_to_string(Item), odbc_queries:update_roster(LServer, LUser, SJID, ItemVals,
odbc_queries:update_roster(LServer, Username, SJID, ItemVals,
ItemGroups); ItemGroups);
update_roster_t(LUser, LServer, _LJID, Item, riak) -> update_roster_t(LUser, LServer, _LJID, Item, riak) ->
ejabberd_riak:put(Item, roster_schema(), ejabberd_riak:put(Item, roster_schema(),
@ -1051,9 +1047,8 @@ del_roster_t(LUser, LServer, LJID) ->
del_roster_t(LUser, LServer, LJID, mnesia) -> del_roster_t(LUser, LServer, LJID, mnesia) ->
mnesia:delete({roster, {LUser, LServer, LJID}}); mnesia:delete({roster, {LUser, LServer, LJID}});
del_roster_t(LUser, LServer, LJID, odbc) -> del_roster_t(LUser, LServer, LJID, odbc) ->
Username = ejabberd_odbc:escape(LUser), SJID = jid:to_string(LJID),
SJID = ejabberd_odbc:escape(jid:to_string(LJID)), odbc_queries:del_roster(LServer, LUser, SJID);
odbc_queries:del_roster(LServer, Username, SJID);
del_roster_t(LUser, LServer, LJID, riak) -> del_roster_t(LUser, LServer, LJID, riak) ->
ejabberd_riak:delete(roster, {LUser, LServer, LJID}). ejabberd_riak:delete(roster, {LUser, LServer, LJID}).
@ -1319,6 +1314,27 @@ record_to_string(#roster{us = {User, _Server},
[Username, SJID, Nick, SSubscription, SAsk, SAskMessage, [Username, SJID, Nick, SSubscription, SAsk, SAskMessage,
<<"N">>, <<"">>, <<"item">>]. <<"N">>, <<"">>, <<"item">>].
record_to_row(
#roster{us = {LUser, _LServer},
jid = JID, name = Name, subscription = Subscription,
ask = Ask, askmessage = AskMessage}) ->
SJID = jid:to_string(jid:tolower(JID)),
SSubscription = case Subscription of
both -> <<"B">>;
to -> <<"T">>;
from -> <<"F">>;
none -> <<"N">>
end,
SAsk = case Ask of
subscribe -> <<"S">>;
unsubscribe -> <<"U">>;
both -> <<"B">>;
out -> <<"O">>;
in -> <<"I">>;
none -> <<"N">>
end,
{LUser, SJID, Name, SSubscription, SAsk, AskMessage}.
groups_to_string(#roster{us = {User, _Server}, groups_to_string(#roster{us = {User, _Server},
jid = JID, groups = Groups}) -> jid = JID, groups = Groups}) ->
Username = ejabberd_odbc:escape(User), Username = ejabberd_odbc:escape(User),

View File

@ -25,6 +25,8 @@
-module(mod_vcard). -module(mod_vcard).
-compile([{parse_transform, ejabberd_sql_pt}]).
-author('alexey@process-one.net'). -author('alexey@process-one.net').
-protocol({xep, 54, '1.2'}). -protocol({xep, 54, '1.2'}).
@ -39,6 +41,7 @@
-include("ejabberd.hrl"). -include("ejabberd.hrl").
-include("logger.hrl"). -include("logger.hrl").
-include("ejabberd_sql_pt.hrl").
-include("jlib.hrl"). -include("jlib.hrl").
@ -212,14 +215,13 @@ get_vcard(LUser, LServer, mnesia) ->
{aborted, _Reason} -> error {aborted, _Reason} -> error
end; end;
get_vcard(LUser, LServer, odbc) -> get_vcard(LUser, LServer, odbc) ->
Username = ejabberd_odbc:escape(LUser), case catch odbc_queries:get_vcard(LServer, LUser) of
case catch odbc_queries:get_vcard(LServer, Username) of {selected, [{SVCARD}]} ->
{selected, [<<"vcard">>], [[SVCARD]]} ->
case fxml_stream:parse_element(SVCARD) of case fxml_stream:parse_element(SVCARD) of
{error, _Reason} -> error; {error, _Reason} -> error;
VCARD -> [VCARD] VCARD -> [VCARD]
end; end;
{selected, [<<"vcard">>], []} -> []; {selected, []} -> [];
_ -> error _ -> error
end; end;
get_vcard(LUser, LServer, riak) -> get_vcard(LUser, LServer, riak) ->
@ -336,39 +338,14 @@ set_vcard(User, LServer, VCARD) ->
{<<"orgunit">>, OrgUnit}, {<<"orgunit">>, OrgUnit},
{<<"lorgunit">>, LOrgUnit}]}]); {<<"lorgunit">>, LOrgUnit}]}]);
odbc -> odbc ->
Username = ejabberd_odbc:escape(User), SVCARD = fxml:element_to_binary(VCARD),
LUsername = ejabberd_odbc:escape(LUser), odbc_queries:set_vcard(LServer, LUser, BDay, CTRY,
SVCARD = EMail, FN, Family, Given, LBDay,
ejabberd_odbc:escape(fxml:element_to_binary(VCARD)), LCTRY, LEMail, LFN, LFamily,
SFN = ejabberd_odbc:escape(FN), LGiven, LLocality, LMiddle,
SLFN = ejabberd_odbc:escape(LFN), LNickname, LOrgName, LOrgUnit,
SFamily = ejabberd_odbc:escape(Family), Locality, Middle, Nickname, OrgName,
SLFamily = ejabberd_odbc:escape(LFamily), OrgUnit, SVCARD, User)
SGiven = ejabberd_odbc:escape(Given),
SLGiven = ejabberd_odbc:escape(LGiven),
SMiddle = ejabberd_odbc:escape(Middle),
SLMiddle = ejabberd_odbc:escape(LMiddle),
SNickname = ejabberd_odbc:escape(Nickname),
SLNickname = ejabberd_odbc:escape(LNickname),
SBDay = ejabberd_odbc:escape(BDay),
SLBDay = ejabberd_odbc:escape(LBDay),
SCTRY = ejabberd_odbc:escape(CTRY),
SLCTRY = ejabberd_odbc:escape(LCTRY),
SLocality = ejabberd_odbc:escape(Locality),
SLLocality = ejabberd_odbc:escape(LLocality),
SEMail = ejabberd_odbc:escape(EMail),
SLEMail = ejabberd_odbc:escape(LEMail),
SOrgName = ejabberd_odbc:escape(OrgName),
SLOrgName = ejabberd_odbc:escape(LOrgName),
SOrgUnit = ejabberd_odbc:escape(OrgUnit),
SLOrgUnit = ejabberd_odbc:escape(LOrgUnit),
odbc_queries:set_vcard(LServer, LUsername, SBDay, SCTRY,
SEMail, SFN, SFamily, SGiven, SLBDay,
SLCTRY, SLEMail, SLFN, SLFamily,
SLGiven, SLLocality, SLMiddle,
SLNickname, SLOrgName, SLOrgUnit,
SLocality, SMiddle, SNickname, SOrgName,
SOrgUnit, SVCARD, Username)
end, end,
ejabberd_hooks:run(vcard_set, LServer, ejabberd_hooks:run(vcard_set, LServer,
[LUser, LServer, VCARD]) [LUser, LServer, VCARD])
@ -929,12 +906,14 @@ remove_user(LUser, LServer, mnesia) ->
end, end,
mnesia:transaction(F); mnesia:transaction(F);
remove_user(LUser, LServer, odbc) -> remove_user(LUser, LServer, odbc) ->
Username = ejabberd_odbc:escape(LUser), ejabberd_odbc:sql_transaction(
ejabberd_odbc:sql_transaction(LServer, LServer,
[[<<"delete from vcard where username='">>, fun() ->
Username, <<"';">>], ejabberd_odbc:sql_query_t(
[<<"delete from vcard_search where lusername='">>, ?SQL("delete from vcard where username=%(LUser)s")),
Username, <<"';">>]]); ejabberd_odbc:sql_query_t(
?SQL("delete from vcard_search where lusername=%(LUser)s"))
end);
remove_user(LUser, LServer, riak) -> remove_user(LUser, LServer, riak) ->
{atomic, ejabberd_riak:delete(vcard, {LUser, LServer})}. {atomic, ejabberd_riak:delete(vcard, {LUser, LServer})}.

View File

@ -42,7 +42,7 @@
get_roster_groups/3, del_user_roster_t/2, get_roster_groups/3, del_user_roster_t/2,
get_roster_by_jid/3, get_rostergroup_by_jid/3, get_roster_by_jid/3, get_rostergroup_by_jid/3,
del_roster/3, del_roster_sql/2, update_roster/5, del_roster/3, del_roster_sql/2, update_roster/5,
update_roster_sql/4, roster_subscribe/4, update_roster_sql/4, roster_subscribe/1,
get_subscription/3, set_private_data/4, get_subscription/3, set_private_data/4,
set_private_data_sql/3, get_private_data/3, set_private_data_sql/3, get_private_data/3,
get_private_data/2, del_user_private_storage/2, get_private_data/2, del_user_private_storage/2,
@ -339,15 +339,13 @@ get_rostergroup_by_jid(LServer, LUser, SJID) ->
?SQL("select @(grp)s from rostergroups" ?SQL("select @(grp)s from rostergroups"
" where username=%(LUser)s and jid=%(SJID)s")). " where username=%(LUser)s and jid=%(SJID)s")).
del_roster(_LServer, Username, SJID) -> del_roster(_LServer, LUser, SJID) ->
ejabberd_odbc:sql_query_t([<<"delete from rosterusers where " ejabberd_odbc:sql_query_t(
"username='">>, ?SQL("delete from rosterusers"
Username, <<"' and jid='">>, SJID, " where username=%(LUser)s and jid=%(SJID)s")),
<<"';">>]), ejabberd_odbc:sql_query_t(
ejabberd_odbc:sql_query_t([<<"delete from rostergroups where " ?SQL("delete from rostergroups"
"username='">>, " where username=%(LUser)s and jid=%(SJID)s")).
Username, <<"' and jid='">>, SJID,
<<"';">>]).
del_roster_sql(Username, SJID) -> del_roster_sql(Username, SJID) ->
[[<<"delete from rosterusers where " [[<<"delete from rosterusers where "
@ -357,27 +355,19 @@ del_roster_sql(Username, SJID) ->
"username='">>, "username='">>,
Username, <<"' and jid='">>, SJID, <<"';">>]]. Username, <<"' and jid='">>, SJID, <<"';">>]].
update_roster(_LServer, Username, SJID, ItemVals, update_roster(_LServer, LUser, SJID, ItemVals,
ItemGroups) -> ItemGroups) ->
update_t(<<"rosterusers">>, roster_subscribe(ItemVals),
[<<"username">>, <<"jid">>, <<"nick">>, ejabberd_odbc:sql_query_t(
<<"subscription">>, <<"ask">>, <<"askmessage">>, ?SQL("delete from rostergroups"
<<"server">>, <<"subscribe">>, <<"type">>], " where username=%(LUser)s and jid=%(SJID)s")),
ItemVals, lists:foreach(
[<<"username='">>, Username, <<"' and jid='">>, SJID, fun(ItemGroup) ->
<<"'">>]), ejabberd_odbc:sql_query_t(
ejabberd_odbc:sql_query_t([<<"delete from rostergroups where " ?SQL("insert into rostergroups(username, jid, grp) "
"username='">>, "values (%(LUser)s, %(SJID)s, %(ItemGroup)s)"))
Username, <<"' and jid='">>, SJID, end,
<<"';">>]), ItemGroups).
lists:foreach(fun (ItemGroup) ->
ejabberd_odbc:sql_query_t([<<"insert into rostergroups( "
" username, jid, grp) values ('">>,
join(ItemGroup,
<<"', '">>),
<<"');">>])
end,
ItemGroups).
update_roster_sql(Username, SJID, ItemVals, update_roster_sql(Username, SJID, ItemVals,
ItemGroups) -> ItemGroups) ->
@ -399,14 +389,18 @@ update_roster_sql(Username, SJID, ItemVals,
join(ItemGroup, <<"', '">>), <<"');">>] join(ItemGroup, <<"', '">>), <<"');">>]
|| ItemGroup <- ItemGroups]. || ItemGroup <- ItemGroups].
roster_subscribe(_LServer, Username, SJID, ItemVals) -> roster_subscribe({LUser, SJID, Name, SSubscription, SAsk, AskMessage}) ->
update_t(<<"rosterusers">>, ?SQL_UPSERT_T(
[<<"username">>, <<"jid">>, <<"nick">>, "rosterusers",
<<"subscription">>, <<"ask">>, <<"askmessage">>, ["!username=%(LUser)s",
<<"server">>, <<"subscribe">>, <<"type">>], "!jid=%(SJID)s",
ItemVals, "nick=%(Name)s",
[<<"username='">>, Username, <<"' and jid='">>, SJID, "subscription=%(SSubscription)s",
<<"'">>]). "ask=%(SAsk)s",
"askmessage=%(AskMessage)s",
"server='N'",
"subscribe=''",
"type='item'"]).
get_subscription(LServer, LUser, SJID) -> get_subscription(LServer, LUser, SJID) ->
ejabberd_odbc:sql_query( ejabberd_odbc:sql_query(
@ -447,53 +441,48 @@ del_user_private_storage(LServer, LUser) ->
?SQL("delete from private_storage" ?SQL("delete from private_storage"
" where username=%(LUser)s")). " where username=%(LUser)s")).
set_vcard(LServer, LUsername, SBDay, SCTRY, SEMail, SFN, set_vcard(LServer, LUser, BDay, CTRY, EMail, FN,
SFamily, SGiven, SLBDay, SLCTRY, SLEMail, SLFN, Family, Given, LBDay, LCTRY, LEMail, LFN,
SLFamily, SLGiven, SLLocality, SLMiddle, SLNickname, LFamily, LGiven, LLocality, LMiddle, LNickname,
SLOrgName, SLOrgUnit, SLocality, SMiddle, SNickname, LOrgName, LOrgUnit, Locality, Middle, Nickname,
SOrgName, SOrgUnit, SVCARD, Username) -> OrgName, OrgUnit, SVCARD, User) ->
ejabberd_odbc:sql_transaction(LServer, ejabberd_odbc:sql_transaction(
fun () -> LServer,
update_t(<<"vcard">>, fun() ->
[<<"username">>, ?SQL_UPSERT(LServer, "vcard",
<<"vcard">>], ["!username=%(LUser)s",
[LUsername, SVCARD], "vcard=%(SVCARD)s"]),
[<<"username='">>, LUsername, ?SQL_UPSERT(LServer, "vcard_search",
<<"'">>]), ["username=%(User)s",
update_t(<<"vcard_search">>, "!lusername=%(LUser)s",
[<<"username">>, "fn=%(FN)s",
<<"lusername">>, <<"fn">>, "lfn=%(LFN)s",
<<"lfn">>, <<"family">>, "family=%(Family)s",
<<"lfamily">>, <<"given">>, "lfamily=%(LFamily)s",
<<"lgiven">>, <<"middle">>, "given=%(Given)s",
<<"lmiddle">>, "lgiven=%(LGiven)s",
<<"nickname">>, "middle=%(Middle)s",
<<"lnickname">>, <<"bday">>, "lmiddle=%(LMiddle)s",
<<"lbday">>, <<"ctry">>, "nickname=%(Nickname)s",
<<"lctry">>, <<"locality">>, "lnickname=%(LNickname)s",
<<"llocality">>, "bday=%(BDay)s",
<<"email">>, <<"lemail">>, "lbday=%(LBDay)s",
<<"orgname">>, "ctry=%(CTRY)s",
<<"lorgname">>, "lctry=%(LCTRY)s",
<<"orgunit">>, "locality=%(Locality)s",
<<"lorgunit">>], "llocality=%(LLocality)s",
[Username, LUsername, SFN, "email=%(EMail)s",
SLFN, SFamily, SLFamily, "lemail=%(LEMail)s",
SGiven, SLGiven, SMiddle, "orgname=%(OrgName)s",
SLMiddle, SNickname, "lorgname=%(LOrgName)s",
SLNickname, SBDay, SLBDay, "orgunit=%(OrgUnit)s",
SCTRY, SLCTRY, SLocality, "lorgunit=%(LOrgUnit)s"])
SLLocality, SEMail, SLEMail, end).
SOrgName, SLOrgName,
SOrgUnit, SLOrgUnit],
[<<"lusername='">>,
LUsername, <<"'">>])
end).
get_vcard(LServer, Username) -> get_vcard(LServer, LUser) ->
ejabberd_odbc:sql_query(LServer, ejabberd_odbc:sql_query(
[<<"select vcard from vcard where username='">>, LServer,
Username, <<"';">>]). ?SQL("select @(vcard)s from vcard where username=%(LUser)s")).
get_default_privacy_list(LServer, LUser) -> get_default_privacy_list(LServer, LUser) ->
ejabberd_odbc:sql_query( ejabberd_odbc:sql_query(