Mercurial > hg > orthanc-databases
comparison Framework/Plugins/GlobalProperties.cpp @ 0:7cea966b6829
initial commit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 04 Jul 2018 08:16:29 +0200 |
parents | |
children | 714c5d2bee76 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7cea966b6829 |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "GlobalProperties.h" | |
23 | |
24 #include "../Common/Utf8StringValue.h" | |
25 | |
26 #include <Core/Logging.h> | |
27 #include <Core/OrthancException.h> | |
28 | |
29 #include <boost/lexical_cast.hpp> | |
30 | |
31 namespace OrthancDatabases | |
32 { | |
33 bool LookupGlobalProperty(std::string& target, | |
34 IDatabase& db, | |
35 ITransaction& transaction, | |
36 Orthanc::GlobalProperty property) | |
37 { | |
38 Query query("SELECT value FROM GlobalProperties WHERE property=${property}", true); | |
39 query.SetType("property", ValueType_Integer64); | |
40 | |
41 std::auto_ptr<IPrecompiledStatement> statement(db.Compile(query)); | |
42 | |
43 Dictionary args; | |
44 args.SetIntegerValue("property", property); | |
45 | |
46 std::auto_ptr<IResult> result(transaction.Execute(*statement, args)); | |
47 | |
48 if (result->IsDone()) | |
49 { | |
50 return false; | |
51 } | |
52 | |
53 result->SetExpectedType(0, ValueType_Utf8String); | |
54 | |
55 ValueType type = result->GetField(0).GetType(); | |
56 | |
57 if (type == ValueType_Null) | |
58 { | |
59 return false; | |
60 } | |
61 else if (type != ValueType_Utf8String) | |
62 { | |
63 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); | |
64 } | |
65 else | |
66 { | |
67 target = dynamic_cast<const Utf8StringValue&>(result->GetField(0)).GetContent(); | |
68 return true; | |
69 } | |
70 } | |
71 | |
72 | |
73 bool LookupGlobalProperty(std::string& target /* out */, | |
74 DatabaseManager& manager, | |
75 Orthanc::GlobalProperty property) | |
76 { | |
77 DatabaseManager::CachedStatement statement( | |
78 STATEMENT_FROM_HERE, manager, | |
79 "SELECT value FROM GlobalProperties WHERE property=${property}"); | |
80 | |
81 statement.SetReadOnly(true); | |
82 statement.SetParameterType("property", ValueType_Integer64); | |
83 | |
84 Dictionary args; | |
85 args.SetIntegerValue("property", property); | |
86 | |
87 statement.Execute(args); | |
88 statement.SetResultFieldType(0, ValueType_Utf8String); | |
89 | |
90 if (statement.IsDone()) | |
91 { | |
92 return false; | |
93 } | |
94 | |
95 ValueType type = statement.GetResultField(0).GetType(); | |
96 | |
97 if (type == ValueType_Null) | |
98 { | |
99 return false; | |
100 } | |
101 else if (type != ValueType_Utf8String) | |
102 { | |
103 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); | |
104 } | |
105 else | |
106 { | |
107 target = dynamic_cast<const Utf8StringValue&>(statement.GetResultField(0)).GetContent(); | |
108 return true; | |
109 } | |
110 } | |
111 | |
112 | |
113 void SetGlobalProperty(IDatabase& db, | |
114 ITransaction& transaction, | |
115 Orthanc::GlobalProperty property, | |
116 const std::string& utf8) | |
117 { | |
118 if (db.GetDialect() == Dialect_SQLite) | |
119 { | |
120 Query query("INSERT OR REPLACE INTO GlobalProperties VALUES (${property}, ${value})", false); | |
121 query.SetType("property", ValueType_Integer64); | |
122 query.SetType("value", ValueType_Utf8String); | |
123 | |
124 std::auto_ptr<IPrecompiledStatement> statement(db.Compile(query)); | |
125 | |
126 Dictionary args; | |
127 args.SetIntegerValue("property", static_cast<int>(property)); | |
128 args.SetUtf8Value("value", utf8); | |
129 | |
130 transaction.ExecuteWithoutResult(*statement, args); | |
131 } | |
132 else | |
133 { | |
134 { | |
135 Query query("DELETE FROM GlobalProperties WHERE property=${property}", false); | |
136 query.SetType("property", ValueType_Integer64); | |
137 | |
138 std::auto_ptr<IPrecompiledStatement> statement(db.Compile(query)); | |
139 | |
140 Dictionary args; | |
141 args.SetIntegerValue("property", static_cast<int>(property)); | |
142 | |
143 transaction.ExecuteWithoutResult(*statement, args); | |
144 } | |
145 | |
146 { | |
147 Query query("INSERT INTO GlobalProperties VALUES (${property}, ${value})", false); | |
148 query.SetType("property", ValueType_Integer64); | |
149 query.SetType("value", ValueType_Utf8String); | |
150 | |
151 std::auto_ptr<IPrecompiledStatement> statement(db.Compile(query)); | |
152 | |
153 Dictionary args; | |
154 args.SetIntegerValue("property", static_cast<int>(property)); | |
155 args.SetUtf8Value("value", utf8); | |
156 | |
157 transaction.ExecuteWithoutResult(*statement, args); | |
158 } | |
159 } | |
160 } | |
161 | |
162 | |
163 void SetGlobalProperty(DatabaseManager& manager, | |
164 Orthanc::GlobalProperty property, | |
165 const std::string& utf8) | |
166 { | |
167 if (manager.GetDialect() == Dialect_SQLite) | |
168 { | |
169 DatabaseManager::CachedStatement statement( | |
170 STATEMENT_FROM_HERE, manager, | |
171 "INSERT OR REPLACE INTO GlobalProperties VALUES (${property}, ${value})"); | |
172 | |
173 statement.SetParameterType("property", ValueType_Integer64); | |
174 statement.SetParameterType("value", ValueType_Utf8String); | |
175 | |
176 Dictionary args; | |
177 args.SetIntegerValue("property", static_cast<int>(property)); | |
178 args.SetUtf8Value("value", utf8); | |
179 | |
180 statement.Execute(args); | |
181 } | |
182 else | |
183 { | |
184 { | |
185 DatabaseManager::CachedStatement statement( | |
186 STATEMENT_FROM_HERE, manager, | |
187 "DELETE FROM GlobalProperties WHERE property=${property}"); | |
188 | |
189 statement.SetParameterType("property", ValueType_Integer64); | |
190 | |
191 Dictionary args; | |
192 args.SetIntegerValue("property", property); | |
193 | |
194 statement.Execute(args); | |
195 } | |
196 | |
197 { | |
198 DatabaseManager::CachedStatement statement( | |
199 STATEMENT_FROM_HERE, manager, | |
200 "INSERT INTO GlobalProperties VALUES (${property}, ${value})"); | |
201 | |
202 statement.SetParameterType("property", ValueType_Integer64); | |
203 statement.SetParameterType("value", ValueType_Utf8String); | |
204 | |
205 Dictionary args; | |
206 args.SetIntegerValue("property", static_cast<int>(property)); | |
207 args.SetUtf8Value("value", utf8); | |
208 | |
209 statement.Execute(args); | |
210 } | |
211 } | |
212 } | |
213 | |
214 | |
215 bool LookupGlobalIntegerProperty(int& target, | |
216 IDatabase& db, | |
217 ITransaction& transaction, | |
218 Orthanc::GlobalProperty property) | |
219 { | |
220 std::string value; | |
221 | |
222 if (LookupGlobalProperty(value, db, transaction, property)) | |
223 { | |
224 try | |
225 { | |
226 target = boost::lexical_cast<int>(value); | |
227 return true; | |
228 } | |
229 catch (boost::bad_lexical_cast&) | |
230 { | |
231 LOG(ERROR) << "Corrupted PostgreSQL database"; | |
232 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); | |
233 } | |
234 } | |
235 else | |
236 { | |
237 return false; | |
238 } | |
239 } | |
240 | |
241 | |
242 void SetGlobalIntegerProperty(IDatabase& db, | |
243 ITransaction& transaction, | |
244 Orthanc::GlobalProperty property, | |
245 int value) | |
246 { | |
247 SetGlobalProperty(db, transaction, property, boost::lexical_cast<std::string>(value)); | |
248 } | |
249 } |