comparison Framework/PostgreSQL/PostgreSQLParameters.cpp @ 0:7cea966b6829

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 04 Jul 2018 08:16:29 +0200
parents
children 3686ba3f9cdb
comparison
equal deleted inserted replaced
-1:000000000000 0:7cea966b6829
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "PostgreSQLParameters.h"
23
24 #include <Core/Logging.h>
25 #include <Core/OrthancException.h>
26
27 #include <boost/lexical_cast.hpp>
28
29
30 namespace OrthancDatabases
31 {
32 void PostgreSQLParameters::Reset()
33 {
34 host_ = "localhost";
35 port_ = 5432;
36 username_ = "";
37 password_ = "";
38 database_.clear();
39 uri_.clear();
40 lock_ = true;
41 }
42
43
44 PostgreSQLParameters::PostgreSQLParameters()
45 {
46 Reset();
47 }
48
49
50 PostgreSQLParameters::PostgreSQLParameters(const OrthancPlugins::OrthancConfiguration& configuration)
51 {
52 Reset();
53
54 std::string s;
55
56 if (configuration.LookupStringValue(s, "ConnectionUri"))
57 {
58 SetConnectionUri(s);
59 }
60 else
61 {
62 if (configuration.LookupStringValue(s, "Host"))
63 {
64 SetHost(s);
65 }
66
67 unsigned int port;
68 if (configuration.LookupUnsignedIntegerValue(port, "Port"))
69 {
70 SetPortNumber(port);
71 }
72
73 if (configuration.LookupStringValue(s, "Database"))
74 {
75 SetDatabase(s);
76 }
77
78 if (configuration.LookupStringValue(s, "Username"))
79 {
80 SetUsername(s);
81 }
82
83 if (configuration.LookupStringValue(s, "Password"))
84 {
85 SetPassword(s);
86 }
87 }
88
89 lock_ = configuration.GetBooleanValue("Lock", true); // Use locking by default
90 }
91
92
93 void PostgreSQLParameters::SetConnectionUri(const std::string& uri)
94 {
95 uri_ = uri;
96 }
97
98
99 std::string PostgreSQLParameters::GetConnectionUri() const
100 {
101 if (uri_.empty())
102 {
103 std::string actualUri = "postgresql://";
104
105 if (!username_.empty())
106 {
107 actualUri += username_;
108
109 if (!password_.empty())
110 {
111 actualUri += ":" + password_;
112 }
113
114 actualUri += "@" + host_;
115 }
116 else
117 {
118 actualUri += host_;
119 }
120
121 if (port_ > 0)
122 {
123 actualUri += ":" + boost::lexical_cast<std::string>(port_);
124 }
125
126 actualUri += "/" + database_;
127
128 return actualUri;
129 }
130 else
131 {
132 return uri_;
133 }
134 }
135
136
137 void PostgreSQLParameters::SetHost(const std::string& host)
138 {
139 uri_.clear();
140 host_ = host;
141 }
142
143 void PostgreSQLParameters::SetPortNumber(unsigned int port)
144 {
145 if (port <= 0 ||
146 port >= 65535)
147 {
148 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
149 }
150
151 uri_.clear();
152 port_ = port;
153 }
154
155 void PostgreSQLParameters::SetUsername(const std::string& username)
156 {
157 uri_.clear();
158 username_ = username;
159 }
160
161 void PostgreSQLParameters::SetPassword(const std::string& password)
162 {
163 uri_.clear();
164 password_ = password;
165 }
166
167 void PostgreSQLParameters::SetDatabase(const std::string& database)
168 {
169 uri_.clear();
170 database_ = database;
171 }
172
173 void PostgreSQLParameters::Format(std::string& target) const
174 {
175 if (uri_.empty())
176 {
177 target = std::string("sslmode=disable") + // TODO WHY SSL DOES NOT WORK? ("SSL error: wrong version number")
178 " user=" + username_ +
179 " password=" + password_ +
180 " host=" + host_ +
181 " port=" + boost::lexical_cast<std::string>(port_);
182
183 if (database_.size() > 0)
184 {
185 target += " dbname=" + database_;
186 }
187 }
188 else
189 {
190 target = uri_;
191 }
192 }
193 }