Mercurial > hg > orthanc-databases
annotate Framework/Common/DatabaseManager.cpp @ 75:52c70007bb87 db-changes
new extension implemented for PostgreSQL: SetResourcesContent
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Sat, 05 Jan 2019 11:41:15 +0100 |
parents | e6c13ddd26d9 |
children | 569e17419eae |
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 | |
67 | 5 * Copyright (C) 2017-2019 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 | |
24 #include <Core/Logging.h> | |
25 #include <Core/OrthancException.h> | |
26 | |
27 #include <boost/thread.hpp> | |
28 | |
29 namespace OrthancDatabases | |
30 { | |
31 IDatabase& DatabaseManager::GetDatabase() | |
32 { | |
33 static const unsigned int MAX_CONNECTION_ATTEMPTS = 10; // TODO: Parameter | |
34 | |
35 unsigned int count = 0; | |
36 | |
37 while (database_.get() == NULL) | |
38 { | |
39 transaction_.reset(NULL); | |
40 | |
41 try | |
42 { | |
43 database_.reset(factory_->Open()); | |
44 } | |
45 catch (Orthanc::OrthancException& e) | |
46 { | |
47 if (e.GetErrorCode() == Orthanc::ErrorCode_DatabaseUnavailable) | |
48 { | |
49 count ++; | |
50 | |
51 if (count <= MAX_CONNECTION_ATTEMPTS) | |
52 { | |
53 LOG(WARNING) << "Database is currently unavailable, retrying..."; | |
54 boost::this_thread::sleep(boost::posix_time::seconds(1)); | |
55 continue; | |
56 } | |
57 else | |
58 { | |
59 LOG(ERROR) << "Timeout when connecting to the database, giving up"; | |
60 } | |
61 } | |
62 | |
63 throw; | |
64 } | |
65 } | |
66 | |
67 if (database_.get() == NULL || | |
68 database_->GetDialect() != dialect_) | |
69 { | |
70 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
71 } | |
72 else | |
73 { | |
74 return *database_; | |
75 } | |
76 } | |
77 | |
78 | |
79 void DatabaseManager::Close() | |
80 { | |
81 LOG(TRACE) << "Closing the connection to the database"; | |
82 | |
83 // Rollback active transaction, if any | |
84 transaction_.reset(NULL); | |
85 | |
86 // Delete all the cached statements (must occur before closing | |
87 // the database) | |
88 for (CachedStatements::iterator it = cachedStatements_.begin(); | |
89 it != cachedStatements_.end(); ++it) | |
90 { | |
91 assert(it->second != NULL); | |
92 delete it->second; | |
93 } | |
94 | |
95 cachedStatements_.clear(); | |
96 | |
97 // Close the database | |
98 database_.reset(NULL); | |
99 | |
100 LOG(TRACE) << "Connection to the database is closed"; | |
101 } | |
102 | |
103 | |
104 void DatabaseManager::CloseIfUnavailable(Orthanc::ErrorCode e) | |
105 { | |
106 if (e != Orthanc::ErrorCode_Success) | |
107 { | |
108 transaction_.reset(NULL); | |
109 } | |
110 | |
111 if (e == Orthanc::ErrorCode_DatabaseUnavailable) | |
112 { | |
113 LOG(ERROR) << "The database is not available, closing the connection"; | |
114 Close(); | |
115 } | |
116 } | |
117 | |
118 | |
119 IPrecompiledStatement* DatabaseManager::LookupCachedStatement(const StatementLocation& location) const | |
120 { | |
121 CachedStatements::const_iterator found = cachedStatements_.find(location); | |
122 | |
123 if (found == cachedStatements_.end()) | |
124 { | |
125 return NULL; | |
126 } | |
127 else | |
128 { | |
129 assert(found->second != NULL); | |
130 return found->second; | |
131 } | |
132 } | |
133 | |
134 | |
135 IPrecompiledStatement& DatabaseManager::CacheStatement(const StatementLocation& location, | |
136 const Query& query) | |
137 { | |
138 LOG(TRACE) << "Caching statement from " << location.GetFile() << ":" << location.GetLine(); | |
139 | |
140 std::auto_ptr<IPrecompiledStatement> statement(GetDatabase().Compile(query)); | |
141 | |
142 IPrecompiledStatement* tmp = statement.get(); | |
143 if (tmp == NULL) | |
144 { | |
145 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
146 } | |
147 | |
148 assert(cachedStatements_.find(location) == cachedStatements_.end()); | |
149 cachedStatements_[location] = statement.release(); | |
150 | |
151 return *tmp; | |
152 } | |
153 | |
154 | |
155 ITransaction& DatabaseManager::GetTransaction() | |
156 { | |
157 if (transaction_.get() == NULL) | |
158 { | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
159 LOG(TRACE) << "Automatically creating an implicit database transaction"; |
0 | 160 |
161 try | |
162 { | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
163 transaction_.reset(GetDatabase().CreateTransaction(true)); |
0 | 164 } |
165 catch (Orthanc::OrthancException& e) | |
166 { | |
167 CloseIfUnavailable(e.GetErrorCode()); | |
168 throw; | |
169 } | |
170 } | |
171 | |
172 assert(transaction_.get() != NULL); | |
173 return *transaction_; | |
174 } | |
175 | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
176 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
177 void DatabaseManager::ReleaseImplicitTransaction() |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
178 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
179 if (transaction_.get() != NULL && |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
180 transaction_->IsImplicit()) |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
181 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
182 LOG(TRACE) << "Committing an implicit database transaction"; |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
183 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
184 try |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
185 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
186 transaction_->Commit(); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
187 transaction_.reset(NULL); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
188 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
189 catch (Orthanc::OrthancException& e) |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
190 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
191 // 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
|
192 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
|
193 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
194 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
195 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
196 |
0 | 197 |
198 DatabaseManager::DatabaseManager(IDatabaseFactory* factory) : // Takes ownership | |
199 factory_(factory) | |
200 { | |
201 if (factory == NULL) | |
202 { | |
203 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
204 } | |
205 | |
206 dialect_ = factory->GetDialect(); | |
207 } | |
208 | |
209 | |
210 void DatabaseManager::StartTransaction() | |
211 { | |
212 boost::recursive_mutex::scoped_lock lock(mutex_); | |
213 | |
214 try | |
215 { | |
216 if (transaction_.get() != NULL) | |
217 { | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
218 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
|
219 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); |
0 | 220 } |
221 | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
222 transaction_.reset(GetDatabase().CreateTransaction(false)); |
0 | 223 } |
224 catch (Orthanc::OrthancException& e) | |
225 { | |
226 CloseIfUnavailable(e.GetErrorCode()); | |
227 throw; | |
228 } | |
229 } | |
230 | |
231 | |
232 void DatabaseManager::CommitTransaction() | |
233 { | |
234 boost::recursive_mutex::scoped_lock lock(mutex_); | |
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 boost::recursive_mutex::scoped_lock lock(mutex_); | |
260 | |
261 if (transaction_.get() == NULL) | |
262 { | |
263 LOG(ERROR) << "Cannot rollback a non-existing transaction"; | |
264 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
265 } | |
266 else | |
267 { | |
268 try | |
269 { | |
270 transaction_->Rollback(); | |
271 transaction_.reset(NULL); | |
272 } | |
273 catch (Orthanc::OrthancException& e) | |
274 { | |
275 CloseIfUnavailable(e.GetErrorCode()); | |
276 throw; | |
277 } | |
278 } | |
279 } | |
280 | |
281 | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
282 DatabaseManager::Transaction::Transaction(DatabaseManager& manager) : |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
283 lock_(manager.mutex_), |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
284 manager_(manager), |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
285 database_(manager.GetDatabase()), |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
286 committed_(false) |
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 manager_.StartTransaction(); |
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 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
291 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
292 DatabaseManager::Transaction::~Transaction() |
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 if (!committed_) |
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 try |
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 manager_.RollbackTransaction(); |
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 catch (Orthanc::OrthancException& e) |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
301 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
302 // 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
|
303 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
|
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 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
308 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
309 void DatabaseManager::Transaction::Commit() |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
310 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
311 if (committed_) |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
312 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
313 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
314 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
315 else |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
316 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
317 manager_.CommitTransaction(); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
318 committed_ = true; |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
319 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
320 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
321 |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
322 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
323 IResult& DatabaseManager::StatementBase::GetResult() const |
12
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
324 { |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
325 if (result_.get() == NULL) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
326 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
327 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
|
328 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
329 } |
12
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
330 |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
331 return *result_; |
12
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
332 } |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
333 |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
334 |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
335 void DatabaseManager::StatementBase::SetQuery(Query* query) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
336 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
337 std::auto_ptr<Query> protection(query); |
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 if (query_.get() != NULL) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
340 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
341 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
|
342 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
343 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
344 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
345 if (query == NULL) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
346 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
347 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
348 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
349 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
350 query_.reset(protection.release()); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
351 } |
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 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
354 void DatabaseManager::StatementBase::SetResult(IResult* result) |
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 std::auto_ptr<IResult> protection(result); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
357 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
358 if (result_.get() != NULL) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
359 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
360 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
|
361 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
362 } |
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 if (result == NULL) |
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 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
367 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
368 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
369 result_.reset(protection.release()); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
370 } |
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 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
373 DatabaseManager::StatementBase::StatementBase(DatabaseManager& manager) : |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
374 manager_(manager), |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
375 lock_(manager_.mutex_), |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
376 transaction_(manager_.GetTransaction()) |
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 } |
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 DatabaseManager::StatementBase::~StatementBase() |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
382 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
383 manager_.ReleaseImplicitTransaction(); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
384 } |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
385 |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
386 |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
387 void DatabaseManager::StatementBase::SetReadOnly(bool readOnly) |
0 | 388 { |
389 if (query_.get() != NULL) | |
390 { | |
391 query_->SetReadOnly(readOnly); | |
392 } | |
393 } | |
394 | |
395 | |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
396 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
|
397 ValueType type) |
0 | 398 { |
399 if (query_.get() != NULL) | |
400 { | |
401 query_->SetType(parameter, type); | |
402 } | |
403 } | |
404 | |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
405 bool DatabaseManager::StatementBase::IsDone() const |
0 | 406 { |
407 try | |
408 { | |
409 return GetResult().IsDone(); | |
410 } | |
411 catch (Orthanc::OrthancException& e) | |
412 { | |
413 manager_.CloseIfUnavailable(e.GetErrorCode()); | |
414 throw; | |
415 } | |
416 } | |
417 | |
418 | |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
419 void DatabaseManager::StatementBase::Next() |
0 | 420 { |
421 try | |
422 { | |
423 GetResult().Next(); | |
424 } | |
425 catch (Orthanc::OrthancException& e) | |
426 { | |
427 manager_.CloseIfUnavailable(e.GetErrorCode()); | |
428 throw; | |
429 } | |
430 } | |
431 | |
432 | |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
433 size_t DatabaseManager::StatementBase::GetResultFieldsCount() const |
0 | 434 { |
435 try | |
436 { | |
437 return GetResult().GetFieldsCount(); | |
438 } | |
439 catch (Orthanc::OrthancException& e) | |
440 { | |
441 manager_.CloseIfUnavailable(e.GetErrorCode()); | |
442 throw; | |
443 } | |
444 } | |
445 | |
446 | |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
447 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
|
448 ValueType type) |
0 | 449 { |
450 try | |
451 { | |
452 if (!GetResult().IsDone()) | |
453 { | |
454 GetResult().SetExpectedType(field, type); | |
455 } | |
456 } | |
457 catch (Orthanc::OrthancException& e) | |
458 { | |
459 manager_.CloseIfUnavailable(e.GetErrorCode()); | |
460 throw; | |
461 } | |
462 } | |
463 | |
464 | |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
465 const IValue& DatabaseManager::StatementBase::GetResultField(size_t index) const |
0 | 466 { |
467 try | |
468 { | |
469 return GetResult().GetField(index); | |
470 } | |
471 catch (Orthanc::OrthancException& e) | |
472 { | |
473 manager_.CloseIfUnavailable(e.GetErrorCode()); | |
474 throw; | |
475 } | |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
476 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
477 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
478 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
479 DatabaseManager::CachedStatement::CachedStatement(const StatementLocation& location, |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
480 DatabaseManager& manager, |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
481 const std::string& sql) : |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
482 StatementBase(manager), |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
483 location_(location) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
484 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
485 statement_ = GetManager().LookupCachedStatement(location_); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
486 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
487 if (statement_ == NULL) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
488 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
489 SetQuery(new Query(sql)); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
490 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
491 else |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
492 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
493 LOG(TRACE) << "Reusing cached statement from " |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
494 << location_.GetFile() << ":" << location_.GetLine(); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
495 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
496 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
497 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
498 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
499 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
|
500 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
501 try |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
502 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
503 std::auto_ptr<Query> query(ReleaseQuery()); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
504 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
505 if (query.get() != NULL) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
506 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
507 // Register the newly-created statement |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
508 assert(statement_ == NULL); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
509 statement_ = &GetManager().CacheStatement(location_, *query); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
510 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
511 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
512 assert(statement_ != NULL); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
513 SetResult(GetTransaction().Execute(*statement_, parameters)); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
514 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
515 catch (Orthanc::OrthancException& e) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
516 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
517 GetManager().CloseIfUnavailable(e.GetErrorCode()); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
518 throw; |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
519 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
520 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
521 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
522 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
523 DatabaseManager::StandaloneStatement::StandaloneStatement(DatabaseManager& manager, |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
524 const std::string& sql) : |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
525 StatementBase(manager) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
526 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
527 SetQuery(new Query(sql)); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
528 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
529 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
530 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
531 DatabaseManager::StandaloneStatement::~StandaloneStatement() |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
532 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
533 // 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
|
534 ClearResult(); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
535 statement_.reset(); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
536 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
537 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
538 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
539 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
|
540 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
541 try |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
542 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
543 std::auto_ptr<Query> query(ReleaseQuery()); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
544 assert(query.get() != NULL); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
545 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
546 // 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
|
547 // 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
|
548 // 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
|
549 statement_.reset(GetManager().GetDatabase().Compile(*query)); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
550 assert(statement_.get() != NULL); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
551 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
552 SetResult(GetTransaction().Execute(*statement_, parameters)); |
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 catch (Orthanc::OrthancException& e) |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
555 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
556 GetManager().CloseIfUnavailable(e.GetErrorCode()); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
557 throw; |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
558 } |
0 | 559 } |
560 } |