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

processone / ejabberd / 1148

09 Oct 2025 11:54AM UTC coverage: 33.894% (+0.1%) from 33.778%
1148

Pull #4460

github

web-flow
Merge ad4876821 into 4a7238afe
Pull Request #4460: Build arm64 container in runner instead of QEMU

0 of 32 new or added lines in 1 file covered. (0.0%)

11037 existing lines in 174 files now uncovered.

15493 of 45710 relevant lines covered (33.89%)

1083.08 hits per line

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

63.16
/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
         import/3, export/1]).
31
-export([sql_schemas/0]).
32

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

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

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

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

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

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

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

127
export(_Server) ->
128
    [{private_storage,
×
129
      fun(Host, #private_storage{usns = {LUser, LServer, XMLNS},
130
                                 xml = Data})
131
            when LServer == Host ->
132
              SData = fxml:element_to_binary(Data),
×
133
              [?SQL("delete from private_storage where"
×
134
                    " username=%(LUser)s and %(LServer)H and namespace=%(XMLNS)s;"),
135
               ?SQL_INSERT(
×
136
                  "private_storage",
137
                  ["username=%(LUser)s",
138
                   "server_host=%(LServer)s",
139
                   "namespace=%(XMLNS)s",
140
                   "data=%(SData)s"])];
141
         (_Host, _R) ->
142
              []
×
143
      end}].
144

145
import(_, _, _) ->
146
    ok.
×
147

148
%%%===================================================================
149
%%% Internal functions
150
%%%===================================================================
151
parse_element(LUser, LServer, XML) ->
UNCOV
152
    case fxml_stream:parse_element(XML) of
18✔
153
        El when is_record(El, xmlel) ->
UNCOV
154
            {ok, El};
18✔
155
        _ ->
156
            ?ERROR_MSG("Malformed XML element in SQL table "
×
157
                       "'private_storage' for user ~ts@~ts: ~ts",
158
                       [LUser, LServer, XML]),
×
159
            error
×
160
    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