• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

processone / ejabberd / 1173

28 Oct 2025 11:02AM UTC coverage: 33.768% (-0.02%) from 33.79%
1173

push

github

badlop
CHANGELOG.md: Update to 25.10

15513 of 45940 relevant lines covered (33.77%)

1078.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

48.0
/src/mod_private_sql.erl
1
%%%-------------------------------------------------------------------
2
%%% File    : mod_private_sql.erl
3
%%% Author  : Evgeny Khramtsov <ekhramtsov@process-one.net>
4
%%% Created : 13 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
5
%%%
6
%%%
7
%%% ejabberd, Copyright (C) 2002-2025   ProcessOne
8
%%%
9
%%% This program is free software; you can redistribute it and/or
10
%%% modify it under the terms of the GNU General Public License as
11
%%% published by the Free Software Foundation; either version 2 of the
12
%%% License, or (at your option) any later version.
13
%%%
14
%%% This program is distributed in the hope that it will be useful,
15
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
16
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
%%% General Public License for more details.
18
%%%
19
%%% You should have received a copy of the GNU General Public License along
20
%%% with this program; if not, write to the Free Software Foundation, Inc.,
21
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
%%%
23
%%%----------------------------------------------------------------------
24

25
-module(mod_private_sql).
26
-behaviour(mod_private).
27

28
%% API
29
-export([init/2, set_data/3, get_data/3, get_all_data/2, del_data/2,
30
    del_data/3, get_users_with_data/2, count_users_with_data/2,
31
    import/3, export/1]).
32
-export([sql_schemas/0]).
33

34
-include_lib("xmpp/include/xmpp.hrl").
35
-include("mod_private.hrl").
36
-include("ejabberd_sql_pt.hrl").
37
-include("logger.hrl").
38

39
%%%===================================================================
40
%%% API
41
%%%===================================================================
42
init(Host, _Opts) ->
43
    ejabberd_sql_schema:update_schema(Host, ?MODULE, sql_schemas()),
6✔
44
    ok.
6✔
45

46
sql_schemas() ->
47
    [#sql_schema{
6✔
48
        version = 1,
49
        tables =
50
            [#sql_table{
51
                name = <<"private_storage">>,
52
                columns =
53
                    [#sql_column{name = <<"username">>, type = text},
54
                     #sql_column{name = <<"server_host">>, type = text},
55
                     #sql_column{name = <<"namespace">>, type = text},
56
                     #sql_column{name = <<"data">>, type = text},
57
                     #sql_column{name = <<"created_at">>, type = timestamp,
58
                                 default = true}],
59
                indices = [#sql_index{
60
                              columns = [<<"server_host">>, <<"username">>,
61
                                         <<"namespace">>],
62
                              unique = true}]}]}].
63

64
set_data(LUser, LServer, Data) ->
65
    F = fun() ->
18✔
66
                lists:foreach(
18✔
67
                  fun({XMLNS, El}) ->
68
                          SData = fxml:element_to_binary(El),
18✔
69
                          ?SQL_UPSERT_T(
18✔
70
                             "private_storage",
71
                             ["!username=%(LUser)s",
72
                              "!server_host=%(LServer)s",
73
                              "!namespace=%(XMLNS)s",
74
                              "data=%(SData)s"])
75
                  end, Data)
76
        end,
77
    case ejabberd_sql:sql_transaction(LServer, F) of
18✔
78
        {atomic, ok} ->
79
            ok;
18✔
80
        _ ->
81
            {error, db_failure}
×
82
    end.
83

84
get_data(LUser, LServer, XMLNS) ->
85
    case ejabberd_sql:sql_query(
50✔
86
           LServer,
87
           ?SQL("select @(data)s from private_storage"
50✔
88
                " where username=%(LUser)s and %(LServer)H"
89
                " and namespace=%(XMLNS)s")) of
90
        {selected, [{SData}]} ->
91
            parse_element(LUser, LServer, SData);
12✔
92
        {selected, []} ->
93
            error;
38✔
94
        _ ->
95
            {error, db_failure}
×
96
    end.
97

98
get_all_data(LUser, LServer) ->
99
    case ejabberd_sql:sql_query(
12✔
100
           LServer,
101
           ?SQL("select @(namespace)s, @(data)s from private_storage"
12✔
102
                " where username=%(LUser)s and %(LServer)H")) of
103
        {selected, []} ->
104
            error;
6✔
105
        {selected, Res} ->
106
            {ok, lists:flatmap(
6✔
107
                   fun({_, SData}) ->
108
                           case parse_element(LUser, LServer, SData) of
6✔
109
                               {ok, El} -> [El];
6✔
110
                               error -> []
×
111
                           end
112
                   end, Res)};
113
        _ ->
114
            {error, db_failure}
×
115
    end.
116

117
del_data(LUser, LServer) ->
118
    case ejabberd_sql:sql_query(
12✔
119
           LServer,
120
           ?SQL("delete from private_storage"
12✔
121
                " where username=%(LUser)s and %(LServer)H")) of
122
        {updated, _} ->
123
            ok;
12✔
124
        _ ->
125
            {error, db_failure}
×
126
    end.
127

128
-spec del_data(binary(), binary(), binary()) -> ok | {error, any()}.
129
del_data(LUser, LServer, NS) ->
130
    case ejabberd_sql:sql_query(
×
131
           LServer,
132
           ?SQL("delete from private_storage"
×
133
                " where username=%(LUser)s and namespace=%(NS)s and %(LServer)H")) of
134
        {updated, _} ->
135
            ok;
×
136
        _ ->
137
            {error, db_failure}
×
138
    end.
139

140
-spec get_users_with_data(binary(), binary()) -> {ok, [binary()]} | {error, any()}.
141
get_users_with_data(LServer, NS) ->
142
    case ejabberd_sql:sql_query(
×
143
           LServer,
144
           ?SQL("select @(username)s from private_storage"
×
145
                " where namespace=%(NS)s and %(LServer)H")) of
146
            {selected, Value} ->
147
            {ok, Value};
×
148
        _ ->
149
            {error, db_failure}
×
150
    end.
151

152
-spec count_users_with_data(binary(), binary()) -> {ok, integer()} | {error, any()}.
153
count_users_with_data(LServer, NS) ->
154
    case ejabberd_sql:sql_query(
×
155
           LServer,
156
           ?SQL("select @(count(*))d from private_storage"
×
157
                " where namespace=%(NS)s and %(LServer)H")) of
158
            {selected, Value} ->
159
            {ok, Value};
×
160
        _ ->
161
            {error, db_failure}
×
162
    end.
163

164
export(_Server) ->
165
    [{private_storage,
×
166
      fun(Host, #private_storage{usns = {LUser, LServer, XMLNS},
167
                                 xml = Data})
168
            when LServer == Host ->
169
              SData = fxml:element_to_binary(Data),
×
170
              [?SQL("delete from private_storage where"
×
171
                    " username=%(LUser)s and %(LServer)H and namespace=%(XMLNS)s;"),
172
               ?SQL_INSERT(
×
173
                  "private_storage",
174
                  ["username=%(LUser)s",
175
                   "server_host=%(LServer)s",
176
                   "namespace=%(XMLNS)s",
177
                   "data=%(SData)s"])];
178
         (_Host, _R) ->
179
              []
×
180
      end}].
181

182
import(_, _, _) ->
183
    ok.
×
184

185
%%%===================================================================
186
%%% Internal functions
187
%%%===================================================================
188
parse_element(LUser, LServer, XML) ->
189
    case fxml_stream:parse_element(XML) of
18✔
190
        El when is_record(El, xmlel) ->
191
            {ok, El};
18✔
192
        _ ->
193
            ?ERROR_MSG("Malformed XML element in SQL table "
×
194
                       "'private_storage' for user ~ts@~ts: ~ts",
195
                       [LUser, LServer, XML]),
×
196
            error
×
197
    end.
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc