Mercurial > hg > orthanc-databases
annotate Framework/PostgreSQL/PostgreSQLParameters.cpp @ 496:4a76aca03774
fixed uninitialized variable PostgreSQL::isolationMode_
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 02 Apr 2024 20:21:04 +0200 |
parents | ff84104f7842 |
children | 8e6a95629a22 |
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 | |
459
ecd0b719cff5
update year to 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
5 * Copyright (C) 2017-2024 Osimis S.A., Belgium |
ecd0b719cff5
update year to 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
389
diff
changeset
|
6 * Copyright (C) 2021-2024 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; |
429
dbf811b1bb43
new configuration 'EnableVerboseLogs' to log SQL statements being executed
Alain Mazy <am@osimis.io>
parents:
417
diff
changeset
|
45 isVerboseEnabled_ = false; |
496
4a76aca03774
fixed uninitialized variable PostgreSQL::isolationMode_
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
467
diff
changeset
|
46 isolationMode_ = IsolationMode_Serializable; |
0 | 47 } |
48 | |
49 | |
50 PostgreSQLParameters::PostgreSQLParameters() | |
51 { | |
52 Reset(); | |
53 } | |
54 | |
55 | |
56 PostgreSQLParameters::PostgreSQLParameters(const OrthancPlugins::OrthancConfiguration& configuration) | |
57 { | |
58 Reset(); | |
59 | |
60 std::string s; | |
61 | |
62 if (configuration.LookupStringValue(s, "ConnectionUri")) | |
63 { | |
64 SetConnectionUri(s); | |
65 } | |
66 else | |
67 { | |
68 if (configuration.LookupStringValue(s, "Host")) | |
69 { | |
70 SetHost(s); | |
71 } | |
72 | |
73 unsigned int port; | |
74 if (configuration.LookupUnsignedIntegerValue(port, "Port")) | |
75 { | |
76 SetPortNumber(port); | |
77 } | |
78 | |
79 if (configuration.LookupStringValue(s, "Database")) | |
80 { | |
81 SetDatabase(s); | |
82 } | |
83 | |
84 if (configuration.LookupStringValue(s, "Username")) | |
85 { | |
86 SetUsername(s); | |
87 } | |
88 | |
89 if (configuration.LookupStringValue(s, "Password")) | |
90 { | |
91 SetPassword(s); | |
92 } | |
80
16df1a6ea452
Fix issue 105 (Unable to connect to PostgreSQL database using SSL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
93 |
16df1a6ea452
Fix issue 105 (Unable to connect to PostgreSQL database using SSL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
94 ssl_ = configuration.GetBooleanValue("EnableSsl", false); |
0 | 95 } |
96 | |
97 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
|
98 |
429
dbf811b1bb43
new configuration 'EnableVerboseLogs' to log SQL statements being executed
Alain Mazy <am@osimis.io>
parents:
417
diff
changeset
|
99 isVerboseEnabled_ = configuration.GetBooleanValue("EnableVerboseLogs", false); |
dbf811b1bb43
new configuration 'EnableVerboseLogs' to log SQL statements being executed
Alain Mazy <am@osimis.io>
parents:
417
diff
changeset
|
100 |
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
|
101 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
|
102 connectionRetryInterval_ = configuration.GetUnsignedIntegerValue("ConnectionRetryInterval", 5); |
370
d2b5d9c92214
PG: test feature: configurable transaction isolation level
Alain Mazy <am@osimis.io>
parents:
359
diff
changeset
|
103 |
467
ff84104f7842
renamed v6.2 to REV2 + removed 'default' TransactionMode + renamed 'READ COMMITTED' into 'ReadCommitted'
Alain Mazy <am@osimis.io>
parents:
460
diff
changeset
|
104 std::string transactionMode = configuration.GetStringValue("TransactionMode", "Serializable"); |
ff84104f7842
renamed v6.2 to REV2 + removed 'default' TransactionMode + renamed 'READ COMMITTED' into 'ReadCommitted'
Alain Mazy <am@osimis.io>
parents:
460
diff
changeset
|
105 if (transactionMode == "ReadCommitted") |
432
8b7c1c423367
new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents:
429
diff
changeset
|
106 { |
457 | 107 LOG(WARNING) << "PostgreSQL: using READ COMMITTED transaction mode"; |
432
8b7c1c423367
new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents:
429
diff
changeset
|
108 SetIsolationMode(IsolationMode_ReadCommited); |
370
d2b5d9c92214
PG: test feature: configurable transaction isolation level
Alain Mazy <am@osimis.io>
parents:
359
diff
changeset
|
109 } |
467
ff84104f7842
renamed v6.2 to REV2 + removed 'default' TransactionMode + renamed 'READ COMMITTED' into 'ReadCommitted'
Alain Mazy <am@osimis.io>
parents:
460
diff
changeset
|
110 else if (transactionMode == "Serializable") |
370
d2b5d9c92214
PG: test feature: configurable transaction isolation level
Alain Mazy <am@osimis.io>
parents:
359
diff
changeset
|
111 { |
457 | 112 LOG(WARNING) << "PostgreSQL: using SERIALIZABLE transaction mode"; |
432
8b7c1c423367
new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents:
429
diff
changeset
|
113 SetIsolationMode(IsolationMode_Serializable); |
370
d2b5d9c92214
PG: test feature: configurable transaction isolation level
Alain Mazy <am@osimis.io>
parents:
359
diff
changeset
|
114 } |
432
8b7c1c423367
new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents:
429
diff
changeset
|
115 else |
8b7c1c423367
new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents:
429
diff
changeset
|
116 { |
8b7c1c423367
new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents:
429
diff
changeset
|
117 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadParameterType, std::string("Invalid value for 'TransactionMode': ") + transactionMode); |
8b7c1c423367
new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents:
429
diff
changeset
|
118 } |
0 | 119 } |
120 | |
121 | |
122 void PostgreSQLParameters::SetConnectionUri(const std::string& uri) | |
123 { | |
124 uri_ = uri; | |
125 } | |
126 | |
127 | |
128 std::string PostgreSQLParameters::GetConnectionUri() const | |
129 { | |
130 if (uri_.empty()) | |
131 { | |
132 std::string actualUri = "postgresql://"; | |
133 | |
134 if (!username_.empty()) | |
135 { | |
136 actualUri += username_; | |
137 | |
138 if (!password_.empty()) | |
139 { | |
140 actualUri += ":" + password_; | |
141 } | |
142 | |
143 actualUri += "@" + host_; | |
144 } | |
145 else | |
146 { | |
147 actualUri += host_; | |
148 } | |
149 | |
150 if (port_ > 0) | |
151 { | |
152 actualUri += ":" + boost::lexical_cast<std::string>(port_); | |
153 } | |
154 | |
155 actualUri += "/" + database_; | |
156 | |
157 return actualUri; | |
158 } | |
159 else | |
160 { | |
161 return uri_; | |
162 } | |
163 } | |
164 | |
165 | |
166 void PostgreSQLParameters::SetHost(const std::string& host) | |
167 { | |
168 uri_.clear(); | |
169 host_ = host; | |
170 } | |
171 | |
172 void PostgreSQLParameters::SetPortNumber(unsigned int port) | |
173 { | |
252 | 174 if (port == 0 || |
0 | 175 port >= 65535) |
176 { | |
177 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
178 } | |
179 | |
180 uri_.clear(); | |
181 port_ = port; | |
182 } | |
183 | |
184 void PostgreSQLParameters::SetUsername(const std::string& username) | |
185 { | |
186 uri_.clear(); | |
187 username_ = username; | |
188 } | |
189 | |
190 void PostgreSQLParameters::SetPassword(const std::string& password) | |
191 { | |
192 uri_.clear(); | |
193 password_ = password; | |
194 } | |
195 | |
196 void PostgreSQLParameters::SetDatabase(const std::string& database) | |
197 { | |
198 uri_.clear(); | |
199 database_ = database; | |
200 } | |
201 | |
202 void PostgreSQLParameters::Format(std::string& target) const | |
203 { | |
204 if (uri_.empty()) | |
205 { | |
80
16df1a6ea452
Fix issue 105 (Unable to connect to PostgreSQL database using SSL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
206 // 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
|
207 // 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
|
208 // 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
|
209 // 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
|
210 target = std::string(ssl_ ? "sslmode=require" : "sslmode=disable") + |
0 | 211 " user=" + username_ + |
212 " host=" + host_ + | |
213 " port=" + boost::lexical_cast<std::string>(port_); | |
214 | |
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
|
215 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
|
216 { |
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
|
217 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
|
218 } |
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
|
219 |
0 | 220 if (database_.size() > 0) |
221 { | |
222 target += " dbname=" + database_; | |
223 } | |
224 } | |
225 else | |
226 { | |
227 target = uri_; | |
228 } | |
229 } | |
230 } |