Mercurial > hg > orthanc-databases
annotate Framework/PostgreSQL/PostgreSQLParameters.cpp @ 385:346fe629d638 db-protobuf
clarifying types of since/limit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 03 Apr 2023 11:19:19 +0200 |
parents | 16aac0287485 |
children | d2b5d9c92214 3d6886f3e5b3 |
rev | line source |
---|---|
0 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
359
16aac0287485
copyright upgraded to 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
16aac0287485
copyright upgraded to 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
0 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
9 * modify it under the terms of the GNU Affero General Public License | |
10 * as published by the Free Software Foundation, either version 3 of | |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * Affero General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU Affero General Public License | |
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 **/ | |
21 | |
22 | |
23 #include "PostgreSQLParameters.h" | |
24 | |
152 | 25 #include <Logging.h> |
26 #include <OrthancException.h> | |
0 | 27 |
28 #include <boost/lexical_cast.hpp> | |
29 | |
30 | |
31 namespace OrthancDatabases | |
32 { | |
33 void PostgreSQLParameters::Reset() | |
34 { | |
35 host_ = "localhost"; | |
36 port_ = 5432; | |
37 username_ = ""; | |
38 password_ = ""; | |
39 database_.clear(); | |
40 uri_.clear(); | |
80
16df1a6ea452
Fix issue 105 (Unable to connect to PostgreSQL database using SSL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
41 ssl_ = false; |
0 | 42 lock_ = true; |
141
0b3e9ee53c46
Added 'MaximumConnectionRetries' & 'ConnectionRetryInterval' to configure the retries when connecting to the DB at startup
Alain Mazy <alain@mazy.be>
parents:
140
diff
changeset
|
43 maxConnectionRetries_ = 10; |
0b3e9ee53c46
Added 'MaximumConnectionRetries' & 'ConnectionRetryInterval' to configure the retries when connecting to the DB at startup
Alain Mazy <alain@mazy.be>
parents:
140
diff
changeset
|
44 connectionRetryInterval_ = 5; |
0 | 45 } |
46 | |
47 | |
48 PostgreSQLParameters::PostgreSQLParameters() | |
49 { | |
50 Reset(); | |
51 } | |
52 | |
53 | |
54 PostgreSQLParameters::PostgreSQLParameters(const OrthancPlugins::OrthancConfiguration& configuration) | |
55 { | |
56 Reset(); | |
57 | |
58 std::string s; | |
59 | |
60 if (configuration.LookupStringValue(s, "ConnectionUri")) | |
61 { | |
62 SetConnectionUri(s); | |
63 } | |
64 else | |
65 { | |
66 if (configuration.LookupStringValue(s, "Host")) | |
67 { | |
68 SetHost(s); | |
69 } | |
70 | |
71 unsigned int port; | |
72 if (configuration.LookupUnsignedIntegerValue(port, "Port")) | |
73 { | |
74 SetPortNumber(port); | |
75 } | |
76 | |
77 if (configuration.LookupStringValue(s, "Database")) | |
78 { | |
79 SetDatabase(s); | |
80 } | |
81 | |
82 if (configuration.LookupStringValue(s, "Username")) | |
83 { | |
84 SetUsername(s); | |
85 } | |
86 | |
87 if (configuration.LookupStringValue(s, "Password")) | |
88 { | |
89 SetPassword(s); | |
90 } | |
80
16df1a6ea452
Fix issue 105 (Unable to connect to PostgreSQL database using SSL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
91 |
16df1a6ea452
Fix issue 105 (Unable to connect to PostgreSQL database using SSL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
92 ssl_ = configuration.GetBooleanValue("EnableSsl", false); |
0 | 93 } |
94 | |
95 lock_ = configuration.GetBooleanValue("Lock", true); // Use locking by default | |
141
0b3e9ee53c46
Added 'MaximumConnectionRetries' & 'ConnectionRetryInterval' to configure the retries when connecting to the DB at startup
Alain Mazy <alain@mazy.be>
parents:
140
diff
changeset
|
96 |
0b3e9ee53c46
Added 'MaximumConnectionRetries' & 'ConnectionRetryInterval' to configure the retries when connecting to the DB at startup
Alain Mazy <alain@mazy.be>
parents:
140
diff
changeset
|
97 maxConnectionRetries_ = configuration.GetUnsignedIntegerValue("MaximumConnectionRetries", 10); |
0b3e9ee53c46
Added 'MaximumConnectionRetries' & 'ConnectionRetryInterval' to configure the retries when connecting to the DB at startup
Alain Mazy <alain@mazy.be>
parents:
140
diff
changeset
|
98 connectionRetryInterval_ = configuration.GetUnsignedIntegerValue("ConnectionRetryInterval", 5); |
0 | 99 } |
100 | |
101 | |
102 void PostgreSQLParameters::SetConnectionUri(const std::string& uri) | |
103 { | |
104 uri_ = uri; | |
105 } | |
106 | |
107 | |
108 std::string PostgreSQLParameters::GetConnectionUri() const | |
109 { | |
110 if (uri_.empty()) | |
111 { | |
112 std::string actualUri = "postgresql://"; | |
113 | |
114 if (!username_.empty()) | |
115 { | |
116 actualUri += username_; | |
117 | |
118 if (!password_.empty()) | |
119 { | |
120 actualUri += ":" + password_; | |
121 } | |
122 | |
123 actualUri += "@" + host_; | |
124 } | |
125 else | |
126 { | |
127 actualUri += host_; | |
128 } | |
129 | |
130 if (port_ > 0) | |
131 { | |
132 actualUri += ":" + boost::lexical_cast<std::string>(port_); | |
133 } | |
134 | |
135 actualUri += "/" + database_; | |
136 | |
137 return actualUri; | |
138 } | |
139 else | |
140 { | |
141 return uri_; | |
142 } | |
143 } | |
144 | |
145 | |
146 void PostgreSQLParameters::SetHost(const std::string& host) | |
147 { | |
148 uri_.clear(); | |
149 host_ = host; | |
150 } | |
151 | |
152 void PostgreSQLParameters::SetPortNumber(unsigned int port) | |
153 { | |
252 | 154 if (port == 0 || |
0 | 155 port >= 65535) |
156 { | |
157 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
158 } | |
159 | |
160 uri_.clear(); | |
161 port_ = port; | |
162 } | |
163 | |
164 void PostgreSQLParameters::SetUsername(const std::string& username) | |
165 { | |
166 uri_.clear(); | |
167 username_ = username; | |
168 } | |
169 | |
170 void PostgreSQLParameters::SetPassword(const std::string& password) | |
171 { | |
172 uri_.clear(); | |
173 password_ = password; | |
174 } | |
175 | |
176 void PostgreSQLParameters::SetDatabase(const std::string& database) | |
177 { | |
178 uri_.clear(); | |
179 database_ = database; | |
180 } | |
181 | |
182 void PostgreSQLParameters::Format(std::string& target) const | |
183 { | |
184 if (uri_.empty()) | |
185 { | |
80
16df1a6ea452
Fix issue 105 (Unable to connect to PostgreSQL database using SSL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
186 // Note about SSL: "require" means that "I want my data to be |
16df1a6ea452
Fix issue 105 (Unable to connect to PostgreSQL database using SSL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
187 // encrypted, and I accept the overhead. I trust that the |
16df1a6ea452
Fix issue 105 (Unable to connect to PostgreSQL database using SSL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
188 // network will make sure I always connect to the server I want." |
16df1a6ea452
Fix issue 105 (Unable to connect to PostgreSQL database using SSL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
189 // https://www.postgresql.org/docs/current/libpq-ssl.html |
16df1a6ea452
Fix issue 105 (Unable to connect to PostgreSQL database using SSL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
190 target = std::string(ssl_ ? "sslmode=require" : "sslmode=disable") + |
0 | 191 " user=" + username_ + |
192 " host=" + host_ + | |
193 " port=" + boost::lexical_cast<std::string>(port_); | |
194 | |
10
3686ba3f9cdb
don't include password in connection string if it is empty; it's seems its not supported by some pg drivers
am@osimis.io
parents:
0
diff
changeset
|
195 if (!password_.empty()) |
3686ba3f9cdb
don't include password in connection string if it is empty; it's seems its not supported by some pg drivers
am@osimis.io
parents:
0
diff
changeset
|
196 { |
3686ba3f9cdb
don't include password in connection string if it is empty; it's seems its not supported by some pg drivers
am@osimis.io
parents:
0
diff
changeset
|
197 target += " password=" + password_; |
3686ba3f9cdb
don't include password in connection string if it is empty; it's seems its not supported by some pg drivers
am@osimis.io
parents:
0
diff
changeset
|
198 } |
3686ba3f9cdb
don't include password in connection string if it is empty; it's seems its not supported by some pg drivers
am@osimis.io
parents:
0
diff
changeset
|
199 |
0 | 200 if (database_.size() > 0) |
201 { | |
202 target += " dbname=" + database_; | |
203 } | |
204 } | |
205 else | |
206 { | |
207 target = uri_; | |
208 } | |
209 } | |
210 } |