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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
67
714c5d2bee76 preparing for 2019
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 31
diff changeset
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 #include "DatabaseManager.h"
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include <Core/Logging.h>
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #include <Core/OrthancException.h>
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include <boost/thread.hpp>
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 namespace OrthancDatabases
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 IDatabase& DatabaseManager::GetDatabase()
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 static const unsigned int MAX_CONNECTION_ATTEMPTS = 10; // TODO: Parameter
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 unsigned int count = 0;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 while (database_.get() == NULL)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 transaction_.reset(NULL);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 try
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 database_.reset(factory_->Open());
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 catch (Orthanc::OrthancException& e)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 if (e.GetErrorCode() == Orthanc::ErrorCode_DatabaseUnavailable)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 count ++;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 if (count <= MAX_CONNECTION_ATTEMPTS)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 LOG(WARNING) << "Database is currently unavailable, retrying...";
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 boost::this_thread::sleep(boost::posix_time::seconds(1));
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 continue;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 else
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 LOG(ERROR) << "Timeout when connecting to the database, giving up";
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 throw;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 if (database_.get() == NULL ||
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 database_->GetDialect() != dialect_)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 else
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 return *database_;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 void DatabaseManager::Close()
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 LOG(TRACE) << "Closing the connection to the database";
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 // Rollback active transaction, if any
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 transaction_.reset(NULL);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 // Delete all the cached statements (must occur before closing
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 // the database)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 for (CachedStatements::iterator it = cachedStatements_.begin();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 it != cachedStatements_.end(); ++it)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 assert(it->second != NULL);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 delete it->second;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 cachedStatements_.clear();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 // Close the database
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 database_.reset(NULL);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 LOG(TRACE) << "Connection to the database is closed";
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 void DatabaseManager::CloseIfUnavailable(Orthanc::ErrorCode e)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 if (e != Orthanc::ErrorCode_Success)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 transaction_.reset(NULL);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 if (e == Orthanc::ErrorCode_DatabaseUnavailable)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 LOG(ERROR) << "The database is not available, closing the connection";
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 Close();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 IPrecompiledStatement* DatabaseManager::LookupCachedStatement(const StatementLocation& location) const
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 CachedStatements::const_iterator found = cachedStatements_.find(location);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 if (found == cachedStatements_.end())
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 return NULL;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 else
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 assert(found->second != NULL);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 return found->second;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 IPrecompiledStatement& DatabaseManager::CacheStatement(const StatementLocation& location,
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 const Query& query)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 LOG(TRACE) << "Caching statement from " << location.GetFile() << ":" << location.GetLine();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 std::auto_ptr<IPrecompiledStatement> statement(GetDatabase().Compile(query));
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 IPrecompiledStatement* tmp = statement.get();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 if (tmp == NULL)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 assert(cachedStatements_.find(location) == cachedStatements_.end());
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 cachedStatements_[location] = statement.release();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 return *tmp;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 ITransaction& DatabaseManager::GetTransaction()
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 if (transaction_.get() == NULL)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 try
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 catch (Orthanc::OrthancException& e)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 CloseIfUnavailable(e.GetErrorCode());
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 throw;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 assert(transaction_.get() != NULL);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 return *transaction_;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 DatabaseManager::DatabaseManager(IDatabaseFactory* factory) : // Takes ownership
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 factory_(factory)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 if (factory == NULL)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 dialect_ = factory->GetDialect();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 void DatabaseManager::StartTransaction()
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 boost::recursive_mutex::scoped_lock lock(mutex_);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 try
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 if (transaction_.get() != NULL)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 catch (Orthanc::OrthancException& e)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 CloseIfUnavailable(e.GetErrorCode());
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 throw;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 void DatabaseManager::CommitTransaction()
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 boost::recursive_mutex::scoped_lock lock(mutex_);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 if (transaction_.get() == NULL)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 LOG(ERROR) << "Cannot commit a non-existing transaction";
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 else
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 try
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 transaction_->Commit();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 transaction_.reset(NULL);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 catch (Orthanc::OrthancException& e)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 CloseIfUnavailable(e.GetErrorCode());
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 throw;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 void DatabaseManager::RollbackTransaction()
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 boost::recursive_mutex::scoped_lock lock(mutex_);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 if (transaction_.get() == NULL)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 LOG(ERROR) << "Cannot rollback a non-existing transaction";
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266 else
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 try
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 transaction_->Rollback();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 transaction_.reset(NULL);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273 catch (Orthanc::OrthancException& e)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275 CloseIfUnavailable(e.GetErrorCode());
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276 throw;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
388 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
389 if (query_.get() != NULL)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
390 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
391 query_->SetReadOnly(readOnly);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
392 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
393 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
394
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
398 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
399 if (query_.get() != NULL)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
400 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
401 query_->SetType(parameter, type);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
402 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
403 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
406 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
407 try
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
408 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
409 return GetResult().IsDone();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
410 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
411 catch (Orthanc::OrthancException& e)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
412 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
413 manager_.CloseIfUnavailable(e.GetErrorCode());
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
414 throw;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
415 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
416 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
417
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
420 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
421 try
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
422 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
423 GetResult().Next();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
424 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
425 catch (Orthanc::OrthancException& e)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
426 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
427 manager_.CloseIfUnavailable(e.GetErrorCode());
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
428 throw;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
429 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
430 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
431
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
434 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
435 try
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
436 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
437 return GetResult().GetFieldsCount();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
438 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
439 catch (Orthanc::OrthancException& e)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
440 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
441 manager_.CloseIfUnavailable(e.GetErrorCode());
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
442 throw;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
443 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
444 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
445
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
449 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
450 try
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
451 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
452 if (!GetResult().IsDone())
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
453 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
454 GetResult().SetExpectedType(field, type);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
455 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
456 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
457 catch (Orthanc::OrthancException& e)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
458 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
459 manager_.CloseIfUnavailable(e.GetErrorCode());
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
460 throw;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
461 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
462 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
463
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
466 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
467 try
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
468 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
469 return GetResult().GetField(index);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
470 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
471 catch (Orthanc::OrthancException& e)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
472 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
473 manager_.CloseIfUnavailable(e.GetErrorCode());
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
474 throw;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
559 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
560 }