1
0
Fork 0
mirror of https://github.com/processone/ejabberd synced 2025-10-03 09:49:18 +02:00

Add SQL_INSERT macro and update SQL queries to use server_host field

This commit is contained in:
Alexey Shchepin 2017-11-02 17:03:30 +03:00
parent 354a710e70
commit 78dfb832b8
25 changed files with 1638 additions and 297 deletions

View file

@ -50,7 +50,7 @@ init(_Host, _Opts) ->
list_groups(Host) ->
case ejabberd_sql:sql_query(
Host,
?SQL("select @(name)s from sr_group")) of
?SQL("select @(name)s from sr_group where %(Host)H")) of
{selected, Rs} -> [G || {G} <- Rs];
_ -> []
end.
@ -58,7 +58,7 @@ list_groups(Host) ->
groups_with_opts(Host) ->
case ejabberd_sql:sql_query(
Host,
?SQL("select @(name)s, @(opts)s from sr_group"))
?SQL("select @(name)s, @(opts)s from sr_group where %(Host)H"))
of
{selected, Rs} ->
[{G, mod_shared_roster:opts_to_binary(ejabberd_sql:decode_term(Opts))}
@ -72,6 +72,7 @@ create_group(Host, Group, Opts) ->
?SQL_UPSERT_T(
"sr_group",
["!name=%(Group)s",
"!server_host=%(Host)s",
"opts=%(SOpts)s"])
end,
ejabberd_sql:sql_transaction(Host, F).
@ -79,9 +80,9 @@ create_group(Host, Group, Opts) ->
delete_group(Host, Group) ->
F = fun () ->
ejabberd_sql:sql_query_t(
?SQL("delete from sr_group where name=%(Group)s")),
?SQL("delete from sr_group where name=%(Group)s and %(Host)H")),
ejabberd_sql:sql_query_t(
?SQL("delete from sr_user where grp=%(Group)s"))
?SQL("delete from sr_user where grp=%(Group)s and %(Host)H"))
end,
case ejabberd_sql:sql_transaction(Host, F) of
{atomic,{updated,_}} -> {atomic, ok};
@ -91,7 +92,8 @@ delete_group(Host, Group) ->
get_group_opts(Host, Group) ->
case catch ejabberd_sql:sql_query(
Host,
?SQL("select @(opts)s from sr_group where name=%(Group)s")) of
?SQL("select @(opts)s from sr_group"
" where name=%(Group)s and %(Host)H")) of
{selected, [{SOpts}]} ->
mod_shared_roster:opts_to_binary(ejabberd_sql:decode_term(SOpts));
_ -> error
@ -103,6 +105,7 @@ set_group_opts(Host, Group, Opts) ->
?SQL_UPSERT_T(
"sr_group",
["!name=%(Group)s",
"!server_host=%(Host)s",
"opts=%(SOpts)s"])
end,
ejabberd_sql:sql_transaction(Host, F).
@ -111,7 +114,8 @@ get_user_groups(US, Host) ->
SJID = make_jid_s(US),
case catch ejabberd_sql:sql_query(
Host,
?SQL("select @(grp)s from sr_user where jid=%(SJID)s")) of
?SQL("select @(grp)s from sr_user"
" where jid=%(SJID)s and %(Host)H")) of
{selected, Rs} -> [G || {G} <- Rs];
_ -> []
end.
@ -119,7 +123,8 @@ get_user_groups(US, Host) ->
get_group_explicit_users(Host, Group) ->
case catch ejabberd_sql:sql_query(
Host,
?SQL("select @(jid)s from sr_user where grp=%(Group)s")) of
?SQL("select @(jid)s from sr_user"
" where grp=%(Group)s and %(Host)H")) of
{selected, Rs} ->
lists:map(
fun({JID}) ->
@ -134,7 +139,8 @@ get_user_displayed_groups(LUser, LServer, GroupsOpts) ->
SJID = make_jid_s(LUser, LServer),
case catch ejabberd_sql:sql_query(
LServer,
?SQL("select @(grp)s from sr_user where jid=%(SJID)s")) of
?SQL("select @(grp)s from sr_user"
" where jid=%(SJID)s and %(LServer)H")) of
{selected, Rs} ->
[{Group, proplists:get_value(Group, GroupsOpts, [])}
|| {Group} <- Rs];
@ -146,7 +152,7 @@ is_user_in_group(US, Group, Host) ->
case catch ejabberd_sql:sql_query(
Host,
?SQL("select @(jid)s from sr_user where jid=%(SJID)s"
" and grp=%(Group)s")) of
" and %(Host)H and grp=%(Group)s")) of
{selected, []} -> false;
_ -> true
end.
@ -155,15 +161,18 @@ add_user_to_group(Host, US, Group) ->
SJID = make_jid_s(US),
ejabberd_sql:sql_query(
Host,
?SQL("insert into sr_user(jid, grp) values ("
"%(SJID)s, %(Group)s)")).
?SQL_INSERT(
"sr_user",
["jid=%(SJID)s",
"server_host=%(Host)s",
"grp=%(Group)s"])).
remove_user_from_group(Host, US, Group) ->
SJID = make_jid_s(US),
F = fun () ->
ejabberd_sql:sql_query_t(
?SQL("delete from sr_user where jid=%(SJID)s and"
" grp=%(Group)s")),
?SQL("delete from sr_user where jid=%(SJID)s and %(Host)H"
" and grp=%(Group)s")),
ok
end,
ejabberd_sql:sql_transaction(Host, F).
@ -173,9 +182,12 @@ export(_Server) ->
fun(Host, #sr_group{group_host = {Group, LServer}, opts = Opts})
when LServer == Host ->
SOpts = misc:term_to_expr(Opts),
[?SQL("delete from sr_group where name=%(Group)s;"),
?SQL("insert into sr_group(name, opts) values ("
"%(Group)s, %(SOpts)s);")];
[?SQL("delete from sr_group where name=%(Group)s and %(Host)H;"),
?SQL_INSERT(
"sr_group",
["name=%(Group)s",
"server_host=%(Host)s",
"opts=%(SOpts)s"])];
(_Host, _R) ->
[]
end},
@ -184,9 +196,12 @@ export(_Server) ->
when LServer == Host ->
SJID = make_jid_s(U, S),
[?SQL("select @(jid)s from sr_user where jid=%(SJID)s"
" and grp=%(Group)s;"),
?SQL("insert into sr_user(jid, grp) values ("
"%(SJID)s, %(Group)s);")];
" and %(Host)H and grp=%(Group)s;"),
?SQL_INSERT(
"sr_user",
["jid=%(SJID)s",
"server_host=%(Host)s",
"grp=%(Group)s"])];
(_Host, _R) ->
[]
end}].