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

processone / ejabberd / 603

17 Oct 2023 01:57PM UTC coverage: 32.654% (-0.4%) from 33.021%
603

push

github

badlop
Fixing minor typos in CHANGELOG

13497 of 41333 relevant lines covered (32.65%)

646.75 hits per line

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

60.53
/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-2023   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

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

37
%%%===================================================================
38
%%% API
39
%%%===================================================================
40
init(Host, _Opts) ->
41
    ejabberd_sql_schema:update_schema(Host, ?MODULE, schemas()),
3✔
42
    ok.
3✔
43

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

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

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

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

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

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

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

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