Mercurial > hg > orthanc-databases
annotate Framework/Common/DatabaseManager.h @ 140:4cd7e45b671e
upgrade to year 2020
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 31 Jan 2020 17:24:29 +0100 |
parents | a060f97a1f72 |
children | 063aa53b5917 |
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 | |
140
4cd7e45b671e
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
96
diff
changeset
|
5 * Copyright (C) 2017-2020 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 #pragma once | |
23 | |
24 #include "IDatabaseFactory.h" | |
25 #include "StatementLocation.h" | |
26 | |
27 #include <Core/Enumerations.h> | |
28 | |
29 #include <boost/thread/recursive_mutex.hpp> | |
30 #include <memory> | |
31 | |
32 namespace OrthancDatabases | |
33 { | |
34 class DatabaseManager : public boost::noncopyable | |
35 { | |
36 private: | |
37 typedef std::map<StatementLocation, IPrecompiledStatement*> CachedStatements; | |
38 | |
39 boost::recursive_mutex mutex_; | |
40 std::auto_ptr<IDatabaseFactory> factory_; | |
41 std::auto_ptr<IDatabase> database_; | |
42 std::auto_ptr<ITransaction> transaction_; | |
43 CachedStatements cachedStatements_; | |
44 Dialect dialect_; | |
45 | |
46 IDatabase& GetDatabase(); | |
47 | |
48 void CloseIfUnavailable(Orthanc::ErrorCode e); | |
49 | |
50 IPrecompiledStatement* LookupCachedStatement(const StatementLocation& location) const; | |
51 | |
52 IPrecompiledStatement& CacheStatement(const StatementLocation& location, | |
53 const Query& query); | |
54 | |
55 ITransaction& GetTransaction(); | |
14
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
12
diff
changeset
|
56 |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
57 void ReleaseImplicitTransaction(); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
58 |
0 | 59 public: |
29 | 60 explicit DatabaseManager(IDatabaseFactory* factory); // Takes ownership |
0 | 61 |
62 ~DatabaseManager() | |
63 { | |
64 Close(); | |
65 } | |
66 | |
67 Dialect GetDialect() const | |
68 { | |
69 return dialect_; | |
70 } | |
71 | |
72 void Open() | |
73 { | |
74 GetDatabase(); | |
75 } | |
76 | |
77 void Close(); | |
78 | |
79 void StartTransaction(); | |
80 | |
81 void CommitTransaction(); | |
82 | |
83 void RollbackTransaction(); | |
84 | |
12
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
85 |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
86 // This class is only used in the "StorageBackend" |
12
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
87 class Transaction : public boost::noncopyable |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
88 { |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
89 private: |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
90 boost::recursive_mutex::scoped_lock lock_; |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
91 DatabaseManager& manager_; |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
92 IDatabase& database_; |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
93 bool committed_; |
12
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
94 |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
95 public: |
29 | 96 explicit Transaction(DatabaseManager& manager); |
12
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
97 |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
98 ~Transaction(); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
99 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
100 void Commit(); |
12
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
101 |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
102 DatabaseManager& GetManager() |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
103 { |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
104 return manager_; |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
105 } |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
106 |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
107 IDatabase& GetDatabase() |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
108 { |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
109 return database_; |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
110 } |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
111 }; |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
112 |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
113 |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
114 class StatementBase : public boost::noncopyable |
0 | 115 { |
116 private: | |
29 | 117 DatabaseManager& manager_; |
0 | 118 boost::recursive_mutex::scoped_lock lock_; |
119 ITransaction& transaction_; | |
120 std::auto_ptr<Query> query_; | |
121 std::auto_ptr<IResult> result_; | |
122 | |
123 IResult& GetResult() const; | |
124 | |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
125 protected: |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
126 DatabaseManager& GetManager() const |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
127 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
128 return manager_; |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
129 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
130 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
131 ITransaction& GetTransaction() const |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
132 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
133 return transaction_; |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
134 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
135 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
136 void SetQuery(Query* query); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
137 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
138 void SetResult(IResult* result); |
0 | 139 |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
140 void ClearResult() |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
141 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
142 result_.reset(); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
143 } |
12
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
144 |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
145 Query* ReleaseQuery() |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
146 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
147 return query_.release(); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
148 } |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
18
diff
changeset
|
149 |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
150 public: |
96 | 151 explicit StatementBase(DatabaseManager& manager); |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
152 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
153 virtual ~StatementBase(); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
154 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
155 // Used only by SQLite |
18 | 156 IDatabase& GetDatabase() |
157 { | |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
158 return manager_.GetDatabase(); |
18 | 159 } |
160 | |
0 | 161 void SetReadOnly(bool readOnly); |
162 | |
163 void SetParameterType(const std::string& parameter, | |
164 ValueType type); | |
165 | |
166 bool IsDone() const; | |
167 | |
168 void Next(); | |
169 | |
170 size_t GetResultFieldsCount() const; | |
171 | |
172 void SetResultFieldType(size_t field, | |
173 ValueType type); | |
174 | |
175 const IValue& GetResultField(size_t index) const; | |
176 }; | |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
177 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
178 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
179 class CachedStatement : public StatementBase |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
180 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
181 private: |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
182 StatementLocation location_; |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
183 IPrecompiledStatement* statement_; |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
184 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
185 public: |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
186 CachedStatement(const StatementLocation& location, |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
187 DatabaseManager& manager, |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
188 const std::string& sql); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
189 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
190 void Execute() |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
191 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
192 Dictionary parameters; |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
193 Execute(parameters); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
194 } |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
195 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
196 void Execute(const Dictionary& parameters); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
197 }; |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
198 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
199 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
200 class StandaloneStatement : public StatementBase |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
201 { |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
202 private: |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
203 std::auto_ptr<IPrecompiledStatement> statement_; |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
204 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
205 public: |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
206 StandaloneStatement(DatabaseManager& manager, |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
207 const std::string& sql); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
208 |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
209 virtual ~StandaloneStatement(); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
210 |
75
52c70007bb87
new extension implemented for PostgreSQL: SetResourcesContent
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
70
diff
changeset
|
211 void Execute() |
52c70007bb87
new extension implemented for PostgreSQL: SetResourcesContent
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
70
diff
changeset
|
212 { |
52c70007bb87
new extension implemented for PostgreSQL: SetResourcesContent
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
70
diff
changeset
|
213 Dictionary parameters; |
52c70007bb87
new extension implemented for PostgreSQL: SetResourcesContent
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
70
diff
changeset
|
214 Execute(parameters); |
52c70007bb87
new extension implemented for PostgreSQL: SetResourcesContent
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
70
diff
changeset
|
215 } |
52c70007bb87
new extension implemented for PostgreSQL: SetResourcesContent
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
70
diff
changeset
|
216 |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
217 void Execute(const Dictionary& parameters); |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
218 }; |
0 | 219 }; |
220 } |