25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-20 17:27:00 +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],
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()} |
{error, binary()} |
{selected, [binary()],

View File

@ -694,12 +694,9 @@ roster_subscribe_t(LUser, LServer, LJID, Item) ->
roster_subscribe_t(_LUser, _LServer, _LJID, Item,
mnesia) ->
mnesia:write(Item);
roster_subscribe_t(LUser, LServer, LJID, Item, odbc) ->
ItemVals = record_to_string(Item),
Username = ejabberd_odbc:escape(LUser),
SJID = ejabberd_odbc:escape(jid:to_string(LJID)),
odbc_queries:roster_subscribe(LServer, Username, SJID,
ItemVals);
roster_subscribe_t(_LUser, _LServer, _LJID, Item, odbc) ->
ItemVals = record_to_row(Item),
odbc_queries:roster_subscribe(ItemVals);
roster_subscribe_t(LUser, LServer, _LJID, Item, riak) ->
ejabberd_riak:put(Item, roster_schema(),
[{'2i', [{<<"us">>, {LUser, LServer}}]}]).
@ -1034,11 +1031,10 @@ update_roster_t(_LUser, _LServer, _LJID, Item,
mnesia) ->
mnesia:write(Item);
update_roster_t(LUser, LServer, LJID, Item, odbc) ->
Username = ejabberd_odbc:escape(LUser),
SJID = ejabberd_odbc:escape(jid:to_string(LJID)),
ItemVals = record_to_string(Item),
ItemGroups = groups_to_string(Item),
odbc_queries:update_roster(LServer, Username, SJID, ItemVals,
SJID = jid:to_string(LJID),
ItemVals = record_to_row(Item),
ItemGroups = Item#roster.groups,
odbc_queries:update_roster(LServer, LUser, SJID, ItemVals,
ItemGroups);
update_roster_t(LUser, LServer, _LJID, Item, riak) ->
ejabberd_riak:put(Item, roster_schema(),
@ -1051,9 +1047,8 @@ del_roster_t(LUser, LServer, LJID) ->
del_roster_t(LUser, LServer, LJID, mnesia) ->
mnesia:delete({roster, {LUser, LServer, LJID}});
del_roster_t(LUser, LServer, LJID, odbc) ->
Username = ejabberd_odbc:escape(LUser),
SJID = ejabberd_odbc:escape(jid:to_string(LJID)),
odbc_queries:del_roster(LServer, Username, SJID);
SJID = jid:to_string(LJID),
odbc_queries:del_roster(LServer, LUser, SJID);
del_roster_t(LUser, LServer, LJID, riak) ->
ejabberd_riak:delete(roster, {LUser, LServer, LJID}).
@ -1319,6 +1314,27 @@ record_to_string(#roster{us = {User, _Server},
[Username, SJID, Nick, SSubscription, SAsk, SAskMessage,
<<"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},
jid = JID, groups = Groups}) ->
Username = ejabberd_odbc:escape(User),

View File

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

View File

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