Mercurial > hg > orthanc-databases
annotate Framework/Common/DatabaseManager.cpp @ 263:29d2b76516f6
fix mysql and postgresql builds
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 20 Apr 2021 15:08:09 +0200 |
parents | d663d9e44f8d |
children | 86265ef5f3e3 |
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 | |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
157
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
0 | 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 "DatabaseManager.h" | |
23 | |
263
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
24 #include "Integer64Value.h" |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
25 #include "BinaryStringValue.h" |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
26 #include "Utf8StringValue.h" |
152 | 27 #include "../../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h" |
116 | 28 |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
29 #include <Compatibility.h> // For std::unique_ptr<> |
152 | 30 #include <Logging.h> |
31 #include <OrthancException.h> | |
0 | 32 |
33 #include <boost/thread.hpp> | |
34 | |
35 namespace OrthancDatabases | |
36 { | |
37 void DatabaseManager::Close() | |
38 { | |
39 LOG(TRACE) << "Closing the connection to the database"; | |
40 | |
41 // Rollback active transaction, if any | |
42 transaction_.reset(NULL); | |
43 | |
44 // Delete all the cached statements (must occur before closing | |
45 // the database) | |
46 for (CachedStatements::iterator it = cachedStatements_.begin(); | |
47 it != cachedStatements_.end(); ++it) | |
48 { | |
49 assert(it->second != NULL); | |
50 delete it->second; | |
51 } | |
52 | |
53 cachedStatements_.clear(); | |
54 | |
55 // Close the database | |
56 database_.reset(NULL); | |
57 | |
58 LOG(TRACE) << "Connection to the database is closed"; | |
59 } | |
60 | |
61 | |
62 void DatabaseManager::CloseIfUnavailable(Orthanc::ErrorCode e) | |
63 { | |
235
f2b32d31fc99
fix lsb build, fix backward compatibility with SDK <= 1.9.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
64 bool failure; |
f2b32d31fc99
fix lsb build, fix backward compatibility with SDK <= 1.9.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
65 |
f2b32d31fc99
fix lsb build, fix backward compatibility with SDK <= 1.9.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
66 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 2) |
f2b32d31fc99
fix lsb build, fix backward compatibility with SDK <= 1.9.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
67 failure = (e != Orthanc::ErrorCode_Success && |
f2b32d31fc99
fix lsb build, fix backward compatibility with SDK <= 1.9.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
68 e != Orthanc::ErrorCode_DatabaseCannotSerialize); |
f2b32d31fc99
fix lsb build, fix backward compatibility with SDK <= 1.9.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
69 #else |
f2b32d31fc99
fix lsb build, fix backward compatibility with SDK <= 1.9.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
70 failure = (e != Orthanc::ErrorCode_Success); |
f2b32d31fc99
fix lsb build, fix backward compatibility with SDK <= 1.9.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
71 #endif |
f2b32d31fc99
fix lsb build, fix backward compatibility with SDK <= 1.9.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
72 |
f2b32d31fc99
fix lsb build, fix backward compatibility with SDK <= 1.9.1
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
234
diff
changeset
|
73 if (failure) |
0 | 74 { |
75 transaction_.reset(NULL); | |
76 } | |
77 | |
78 if (e == Orthanc::ErrorCode_DatabaseUnavailable) | |
79 { | |
80 LOG(ERROR) << "The database is not available, closing the connection"; | |
81 Close(); | |
82 } | |
83 } | |
84 | |
85 | |
86 IPrecompiledStatement* DatabaseManager::LookupCachedStatement(const StatementLocation& location) const | |
87 { | |
88 CachedStatements::const_iterator found = cachedStatements_.find(location); | |
89 | |
90 if (found == cachedStatements_.end()) | |
91 { | |
92 return NULL; | |
93 } | |
94 else | |
95 { | |
96 assert(found->second != NULL); | |
97 return found->second; | |
98 } | |
99 } | |
100 | |
101 | |
102 IPrecompiledStatement& DatabaseManager::CacheStatement(const StatementLocation& location, | |
103 const Query& query) | |
104 { | |
105 LOG(TRACE) << "Caching statement from " << location.GetFile() << ":" << location.GetLine(); | |
106 | |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
107 std::unique_ptr<IPrecompiledStatement> statement(GetDatabase().Compile(query)); |
0 | 108 |
109 IPrecompiledStatement* tmp = statement.get(); | |
110 if (tmp == NULL) | |
111 { | |
112 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
113 } | |
114 | |
115 assert(cachedStatements_.find(location) == cachedStatements_.end()); | |
116 cachedStatements_[location] = statement.release(); | |
117 | |
118 return *tmp; | |
119 } | |
120 | |
121 | |
122 ITransaction& DatabaseManager::GetTransaction() | |
123 { | |
124 if (transaction_.get() == NULL) | |
125 { | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
126 LOG(TRACE) << "Automatically creating an implicit database transaction"; |
0 | 127 |
128 try | |
129 { | |
215
b40b30075c51
added TransactionType_Implicit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
130 transaction_.reset(GetDatabase().CreateTransaction(TransactionType_Implicit)); |
0 | 131 } |
132 catch (Orthanc::OrthancException& e) | |
133 { | |
134 CloseIfUnavailable(e.GetErrorCode()); | |
135 throw; | |
136 } | |
137 } | |
138 | |
139 assert(transaction_.get() != NULL); | |
140 return *transaction_; | |
141 } | |
142 | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
143 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
144 void DatabaseManager::ReleaseImplicitTransaction() |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
145 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
146 if (transaction_.get() != NULL && |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
147 transaction_->IsImplicit()) |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
148 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
149 LOG(TRACE) << "Committing an implicit database transaction"; |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
150 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
151 try |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
152 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
153 transaction_->Commit(); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
154 transaction_.reset(NULL); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
155 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
156 catch (Orthanc::OrthancException& e) |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
157 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
158 // Don't throw the exception, as we are in CachedStatement destructor |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
159 LOG(ERROR) << "Error while committing an implicit database transaction: " << e.What(); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
160 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
161 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
162 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
163 |
0 | 164 |
255
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
165 DatabaseManager::DatabaseManager(IDatabaseFactory* factory) : |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
166 factory_(factory), |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
167 dialect_(Dialect_Unknown) |
0 | 168 { |
255
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
169 if (factory == NULL) |
0 | 170 { |
171 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
172 } | |
173 } | |
174 | |
175 | |
254
8a4ce70f456a
fix crash if connection to server is lost
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
242
diff
changeset
|
176 IDatabase& DatabaseManager::GetDatabase() |
8a4ce70f456a
fix crash if connection to server is lost
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
242
diff
changeset
|
177 { |
255
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
178 assert(factory_.get() != NULL); |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
179 |
254
8a4ce70f456a
fix crash if connection to server is lost
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
242
diff
changeset
|
180 if (database_.get() == NULL) |
8a4ce70f456a
fix crash if connection to server is lost
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
242
diff
changeset
|
181 { |
255
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
182 database_.reset(factory_->Open()); |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
183 |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
184 if (database_.get() == NULL) |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
185 { |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
186 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
187 } |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
188 |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
189 dialect_ = database_->GetDialect(); |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
190 if (dialect_ == Dialect_Unknown) |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
191 { |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
192 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
193 } |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
194 } |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
195 |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
196 return *database_; |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
197 } |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
198 |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
199 |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
200 Dialect DatabaseManager::GetDialect() const |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
201 { |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
202 if (database_.get() == NULL) |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
203 { |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
204 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); |
254
8a4ce70f456a
fix crash if connection to server is lost
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
242
diff
changeset
|
205 } |
8a4ce70f456a
fix crash if connection to server is lost
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
242
diff
changeset
|
206 else |
8a4ce70f456a
fix crash if connection to server is lost
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
242
diff
changeset
|
207 { |
255
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
208 assert(dialect_ != Dialect_Unknown); |
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
209 return dialect_; |
254
8a4ce70f456a
fix crash if connection to server is lost
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
242
diff
changeset
|
210 } |
8a4ce70f456a
fix crash if connection to server is lost
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
242
diff
changeset
|
211 } |
255
d663d9e44f8d
reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
254
diff
changeset
|
212 |
254
8a4ce70f456a
fix crash if connection to server is lost
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
242
diff
changeset
|
213 |
207
d9ef3f16e6a2
wrapping transactions in API v3
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
214 void DatabaseManager::StartTransaction(TransactionType type) |
0 | 215 { |
216 try | |
217 { | |
218 if (transaction_.get() != NULL) | |
219 { | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
220 LOG(ERROR) << "Cannot start another transaction while there is an uncommitted transaction"; |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
221 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); |
0 | 222 } |
223 | |
215
b40b30075c51
added TransactionType_Implicit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
207
diff
changeset
|
224 transaction_.reset(GetDatabase().CreateTransaction(type)); |
0 | 225 } |
226 catch (Orthanc::OrthancException& e) | |
227 { | |
228 CloseIfUnavailable(e.GetErrorCode()); | |
229 throw; | |
230 } | |
231 } | |
232 | |
233 | |
234 void DatabaseManager::CommitTransaction() | |
235 { | |
236 if (transaction_.get() == NULL) | |
237 { | |
238 LOG(ERROR) << "Cannot commit a non-existing transaction"; | |
239 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
240 } | |
241 else | |
242 { | |
243 try | |
244 { | |
245 transaction_->Commit(); | |
246 transaction_.reset(NULL); | |
247 } | |
248 catch (Orthanc::OrthancException& e) | |
249 { | |
250 CloseIfUnavailable(e.GetErrorCode()); | |
251 throw; | |
252 } | |
253 } | |
254 } | |
255 | |
256 | |
257 void DatabaseManager::RollbackTransaction() | |
258 { | |
259 if (transaction_.get() == NULL) | |
260 { | |
234
d1b124d116c1
PostgreSQL index plugin handles retries for collisions between multiple writers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
261 LOG(INFO) << "Cannot rollback a non-existing transaction"; |
0 | 262 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
263 } | |
264 else | |
265 { | |
266 try | |
267 { | |
268 transaction_->Rollback(); | |
269 transaction_.reset(NULL); | |
270 } | |
271 catch (Orthanc::OrthancException& e) | |
272 { | |
273 CloseIfUnavailable(e.GetErrorCode()); | |
274 throw; | |
275 } | |
276 } | |
277 } | |
278 | |
279 | |
207
d9ef3f16e6a2
wrapping transactions in API v3
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
280 DatabaseManager::Transaction::Transaction(DatabaseManager& manager, |
d9ef3f16e6a2
wrapping transactions in API v3
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
281 TransactionType type) : |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
282 manager_(manager), |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
283 database_(manager.GetDatabase()), |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
235
diff
changeset
|
284 active_(true) |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
285 { |
207
d9ef3f16e6a2
wrapping transactions in API v3
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
286 manager_.StartTransaction(type); |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
287 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
288 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
289 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
290 DatabaseManager::Transaction::~Transaction() |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
291 { |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
235
diff
changeset
|
292 if (active_) |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
293 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
294 try |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
295 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
296 manager_.RollbackTransaction(); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
297 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
298 catch (Orthanc::OrthancException& e) |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
299 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
300 // Don't rethrow the exception as we are in a destructor |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
301 LOG(ERROR) << "Uncatched error during some transaction rollback: " << e.What(); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
302 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
303 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
304 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
305 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
306 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
307 void DatabaseManager::Transaction::Commit() |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
308 { |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
235
diff
changeset
|
309 if (active_) |
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
235
diff
changeset
|
310 { |
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
235
diff
changeset
|
311 manager_.CommitTransaction(); |
242
b97a537f4613
MySQL: Support of range reads for the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
312 active_ = false; |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
235
diff
changeset
|
313 } |
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
235
diff
changeset
|
314 else |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
315 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
316 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
317 } |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
235
diff
changeset
|
318 } |
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
235
diff
changeset
|
319 |
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
235
diff
changeset
|
320 |
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
235
diff
changeset
|
321 void DatabaseManager::Transaction::Rollback() |
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
235
diff
changeset
|
322 { |
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
235
diff
changeset
|
323 if (active_) |
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
235
diff
changeset
|
324 { |
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
235
diff
changeset
|
325 manager_.RollbackTransaction(); |
242
b97a537f4613
MySQL: Support of range reads for the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
241
diff
changeset
|
326 active_ = false; |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
235
diff
changeset
|
327 } |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
328 else |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
329 { |
241
a063bbf10a3e
simplification of class DatabaseManager::Transaction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
235
diff
changeset
|
330 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
331 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
332 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
333 |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
334 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
335 IResult& DatabaseManager::StatementBase::GetResult() const |
12
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
336 { |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
337 if (result_.get() == NULL) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
338 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
339 LOG(ERROR) << "Accessing the results of a statement without having executed it"; |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
340 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
341 } |
12
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
342 |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
343 return *result_; |
12
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
344 } |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
345 |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
346 |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
347 void DatabaseManager::StatementBase::SetQuery(Query* query) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
348 { |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
349 std::unique_ptr<Query> protection(query); |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
350 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
351 if (query_.get() != NULL) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
352 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
353 LOG(ERROR) << "Cannot set twice a query"; |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
354 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
355 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
356 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
357 if (query == NULL) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
358 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
359 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
360 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
361 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
362 query_.reset(protection.release()); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
363 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
364 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
365 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
366 void DatabaseManager::StatementBase::SetResult(IResult* result) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
367 { |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
368 std::unique_ptr<IResult> protection(result); |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
369 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
370 if (result_.get() != NULL) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
371 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
372 LOG(ERROR) << "Cannot execute twice a statement"; |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
373 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
374 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
375 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
376 if (result == NULL) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
377 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
378 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
379 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
380 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
381 result_.reset(protection.release()); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
382 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
383 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
384 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
385 DatabaseManager::StatementBase::StatementBase(DatabaseManager& manager) : |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
386 manager_(manager), |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
387 transaction_(manager_.GetTransaction()) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
388 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
389 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
390 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
391 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
392 DatabaseManager::StatementBase::~StatementBase() |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
393 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
394 manager_.ReleaseImplicitTransaction(); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
395 } |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
396 |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
397 |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
398 void DatabaseManager::StatementBase::SetReadOnly(bool readOnly) |
0 | 399 { |
400 if (query_.get() != NULL) | |
401 { | |
402 query_->SetReadOnly(readOnly); | |
403 } | |
404 } | |
405 | |
406 | |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
407 void DatabaseManager::StatementBase::SetParameterType(const std::string& parameter, |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
408 ValueType type) |
0 | 409 { |
410 if (query_.get() != NULL) | |
411 { | |
412 query_->SetType(parameter, type); | |
413 } | |
414 } | |
415 | |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
416 bool DatabaseManager::StatementBase::IsDone() const |
0 | 417 { |
418 try | |
419 { | |
420 return GetResult().IsDone(); | |
421 } | |
422 catch (Orthanc::OrthancException& e) | |
423 { | |
424 manager_.CloseIfUnavailable(e.GetErrorCode()); | |
425 throw; | |
426 } | |
427 } | |
428 | |
429 | |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
430 void DatabaseManager::StatementBase::Next() |
0 | 431 { |
432 try | |
433 { | |
434 GetResult().Next(); | |
435 } | |
436 catch (Orthanc::OrthancException& e) | |
437 { | |
438 manager_.CloseIfUnavailable(e.GetErrorCode()); | |
439 throw; | |
440 } | |
441 } | |
442 | |
443 | |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
444 size_t DatabaseManager::StatementBase::GetResultFieldsCount() const |
0 | 445 { |
446 try | |
447 { | |
448 return GetResult().GetFieldsCount(); | |
449 } | |
450 catch (Orthanc::OrthancException& e) | |
451 { | |
452 manager_.CloseIfUnavailable(e.GetErrorCode()); | |
453 throw; | |
454 } | |
455 } | |
456 | |
457 | |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
458 void DatabaseManager::StatementBase::SetResultFieldType(size_t field, |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
459 ValueType type) |
0 | 460 { |
461 try | |
462 { | |
463 if (!GetResult().IsDone()) | |
464 { | |
465 GetResult().SetExpectedType(field, type); | |
466 } | |
467 } | |
468 catch (Orthanc::OrthancException& e) | |
469 { | |
470 manager_.CloseIfUnavailable(e.GetErrorCode()); | |
471 throw; | |
472 } | |
473 } | |
474 | |
475 | |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
476 const IValue& DatabaseManager::StatementBase::GetResultField(size_t index) const |
0 | 477 { |
478 try | |
479 { | |
480 return GetResult().GetField(index); | |
481 } | |
482 catch (Orthanc::OrthancException& e) | |
483 { | |
484 manager_.CloseIfUnavailable(e.GetErrorCode()); | |
485 throw; | |
486 } | |
263
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
487 } |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
488 |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
489 |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
490 int64_t DatabaseManager::StatementBase::ReadInteger64(size_t field) const |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
491 { |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
492 if (IsDone()) |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
493 { |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
494 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
495 } |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
496 else |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
497 { |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
498 const IValue& value = GetResultField(field); |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
499 |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
500 switch (value.GetType()) |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
501 { |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
502 case ValueType_Integer64: |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
503 return dynamic_cast<const Integer64Value&>(value).GetValue(); |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
504 |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
505 default: |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
506 //LOG(ERROR) << value.Format(); |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
507 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
508 } |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
509 } |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
510 } |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
511 |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
512 |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
513 int32_t DatabaseManager::StatementBase::ReadInteger32(size_t field) const |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
514 { |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
515 if (IsDone()) |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
516 { |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
517 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
518 } |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
519 else |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
520 { |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
521 int64_t value = ReadInteger64(field); |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
522 |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
523 if (value != static_cast<int64_t>(static_cast<int32_t>(value))) |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
524 { |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
525 LOG(ERROR) << "Integer overflow"; |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
526 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
527 } |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
528 else |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
529 { |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
530 return static_cast<int32_t>(value); |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
531 } |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
532 } |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
533 } |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
534 |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
535 |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
536 std::string DatabaseManager::StatementBase::ReadString(size_t field) const |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
537 { |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
538 const IValue& value = GetResultField(field); |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
539 |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
540 switch (value.GetType()) |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
541 { |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
542 case ValueType_BinaryString: |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
543 return dynamic_cast<const BinaryStringValue&>(value).GetContent(); |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
544 |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
545 case ValueType_Utf8String: |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
546 return dynamic_cast<const Utf8StringValue&>(value).GetContent(); |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
547 |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
548 default: |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
549 //LOG(ERROR) << value.Format(); |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
550 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
551 } |
29d2b76516f6
fix mysql and postgresql builds
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
255
diff
changeset
|
552 } |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
553 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
554 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
555 DatabaseManager::CachedStatement::CachedStatement(const StatementLocation& location, |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
556 DatabaseManager& manager, |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
557 const std::string& sql) : |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
558 StatementBase(manager), |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
559 location_(location) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
560 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
561 statement_ = GetManager().LookupCachedStatement(location_); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
562 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
563 if (statement_ == NULL) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
564 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
565 SetQuery(new Query(sql)); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
566 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
567 else |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
568 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
569 LOG(TRACE) << "Reusing cached statement from " |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
570 << location_.GetFile() << ":" << location_.GetLine(); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
571 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
572 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
573 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
574 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
575 void DatabaseManager::CachedStatement::Execute(const Dictionary& parameters) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
576 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
577 try |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
578 { |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
579 std::unique_ptr<Query> query(ReleaseQuery()); |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
580 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
581 if (query.get() != NULL) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
582 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
583 // Register the newly-created statement |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
584 assert(statement_ == NULL); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
585 statement_ = &GetManager().CacheStatement(location_, *query); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
586 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
587 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
588 assert(statement_ != NULL); |
116 | 589 |
590 /* | |
591 TODO - Sample code to monitor the execution time of each | |
592 cached statement, and publish it as an Orthanc metrics | |
593 | |
594 #if HAS_ORTHANC_PLUGIN_METRICS == 1 | |
595 std::string name = (std::string(location_.GetFile()) + "_" + | |
596 boost::lexical_cast<std::string>(location_.GetLine())); | |
597 OrthancPlugins::MetricsTimer timer(name.c_str()); | |
598 #endif | |
599 */ | |
600 | |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
601 SetResult(GetTransaction().Execute(*statement_, parameters)); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
602 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
603 catch (Orthanc::OrthancException& e) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
604 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
605 GetManager().CloseIfUnavailable(e.GetErrorCode()); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
606 throw; |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
607 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
608 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
609 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
610 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
611 DatabaseManager::StandaloneStatement::StandaloneStatement(DatabaseManager& manager, |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
612 const std::string& sql) : |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
613 StatementBase(manager) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
614 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
615 SetQuery(new Query(sql)); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
616 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
617 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
618 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
619 DatabaseManager::StandaloneStatement::~StandaloneStatement() |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
620 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
621 // The result must be removed before the statement, cf. (*) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
622 ClearResult(); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
623 statement_.reset(); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
624 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
625 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
626 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
627 void DatabaseManager::StandaloneStatement::Execute(const Dictionary& parameters) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
628 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
629 try |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
630 { |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
631 std::unique_ptr<Query> query(ReleaseQuery()); |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
632 assert(query.get() != NULL); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
633 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
634 // The "statement_" object must be kept as long as the "IResult" |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
635 // is not destroyed, as the "IResult" can make calls to the |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
636 // statement (this is the case for SQLite and MySQL) - (*) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
637 statement_.reset(GetManager().GetDatabase().Compile(*query)); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
638 assert(statement_.get() != NULL); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
639 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
640 SetResult(GetTransaction().Execute(*statement_, parameters)); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
641 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
642 catch (Orthanc::OrthancException& e) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
643 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
644 GetManager().CloseIfUnavailable(e.GetErrorCode()); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
645 throw; |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
646 } |
0 | 647 } |
648 } |