Mercurial > hg > orthanc-databases
annotate Resources/Orthanc/Databases/ISqlLookupFormatter.cpp @ 330:8f17f23c9af7
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 10 Aug 2021 20:52:16 +0200 |
parents | f2e160b2dc3e |
children | 389c037387ea |
rev | line source |
---|---|
152 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
170
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
152 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * 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 * General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #if !defined(ORTHANC_BUILDING_SERVER_LIBRARY) | |
23 # error Macro ORTHANC_BUILDING_SERVER_LIBRARY must be defined | |
24 #endif | |
25 | |
26 #if ORTHANC_BUILDING_SERVER_LIBRARY == 1 | |
27 # include "../PrecompiledHeadersServer.h" | |
28 #endif | |
29 | |
30 #include "ISqlLookupFormatter.h" | |
31 | |
32 #if ORTHANC_BUILDING_SERVER_LIBRARY == 1 | |
33 # include "../../../OrthancFramework/Sources/OrthancException.h" | |
34 #else | |
35 # include <OrthancException.h> | |
36 #endif | |
37 | |
38 #include "DatabaseConstraint.h" | |
39 | |
170 | 40 #include <boost/lexical_cast.hpp> |
41 | |
42 | |
152 | 43 namespace Orthanc |
44 { | |
45 static std::string FormatLevel(ResourceType level) | |
46 { | |
47 switch (level) | |
48 { | |
49 case ResourceType_Patient: | |
50 return "patients"; | |
51 | |
52 case ResourceType_Study: | |
53 return "studies"; | |
54 | |
55 case ResourceType_Series: | |
56 return "series"; | |
57 | |
58 case ResourceType_Instance: | |
59 return "instances"; | |
60 | |
61 default: | |
62 throw OrthancException(ErrorCode_InternalError); | |
63 } | |
64 } | |
65 | |
66 | |
67 static bool FormatComparison(std::string& target, | |
68 ISqlLookupFormatter& formatter, | |
69 const DatabaseConstraint& constraint, | |
70 size_t index) | |
71 { | |
72 std::string tag = "t" + boost::lexical_cast<std::string>(index); | |
73 | |
74 std::string comparison; | |
75 | |
76 switch (constraint.GetConstraintType()) | |
77 { | |
78 case ConstraintType_Equal: | |
79 case ConstraintType_SmallerOrEqual: | |
80 case ConstraintType_GreaterOrEqual: | |
81 { | |
82 std::string op; | |
83 switch (constraint.GetConstraintType()) | |
84 { | |
85 case ConstraintType_Equal: | |
86 op = "="; | |
87 break; | |
88 | |
89 case ConstraintType_SmallerOrEqual: | |
90 op = "<="; | |
91 break; | |
92 | |
93 case ConstraintType_GreaterOrEqual: | |
94 op = ">="; | |
95 break; | |
96 | |
97 default: | |
98 throw OrthancException(ErrorCode_InternalError); | |
99 } | |
100 | |
101 std::string parameter = formatter.GenerateParameter(constraint.GetSingleValue()); | |
102 | |
103 if (constraint.IsCaseSensitive()) | |
104 { | |
105 comparison = tag + ".value " + op + " " + parameter; | |
106 } | |
107 else | |
108 { | |
109 comparison = "lower(" + tag + ".value) " + op + " lower(" + parameter + ")"; | |
110 } | |
111 | |
112 break; | |
113 } | |
114 | |
115 case ConstraintType_List: | |
116 { | |
117 for (size_t i = 0; i < constraint.GetValuesCount(); i++) | |
118 { | |
119 if (!comparison.empty()) | |
120 { | |
121 comparison += ", "; | |
122 } | |
123 | |
124 std::string parameter = formatter.GenerateParameter(constraint.GetValue(i)); | |
125 | |
126 if (constraint.IsCaseSensitive()) | |
127 { | |
128 comparison += parameter; | |
129 } | |
130 else | |
131 { | |
132 comparison += "lower(" + parameter + ")"; | |
133 } | |
134 } | |
135 | |
136 if (constraint.IsCaseSensitive()) | |
137 { | |
138 comparison = tag + ".value IN (" + comparison + ")"; | |
139 } | |
140 else | |
141 { | |
142 comparison = "lower(" + tag + ".value) IN (" + comparison + ")"; | |
143 } | |
144 | |
145 break; | |
146 } | |
147 | |
148 case ConstraintType_Wildcard: | |
149 { | |
150 const std::string value = constraint.GetSingleValue(); | |
151 | |
152 if (value == "*") | |
153 { | |
154 if (!constraint.IsMandatory()) | |
155 { | |
156 // Universal constraint on an optional tag, ignore it | |
157 return false; | |
158 } | |
159 } | |
160 else | |
161 { | |
162 std::string escaped; | |
163 escaped.reserve(value.size()); | |
164 | |
165 for (size_t i = 0; i < value.size(); i++) | |
166 { | |
167 if (value[i] == '*') | |
168 { | |
169 escaped += "%"; | |
170 } | |
171 else if (value[i] == '?') | |
172 { | |
173 escaped += "_"; | |
174 } | |
175 else if (value[i] == '%') | |
176 { | |
177 escaped += "\\%"; | |
178 } | |
179 else if (value[i] == '_') | |
180 { | |
181 escaped += "\\_"; | |
182 } | |
183 else if (value[i] == '\\') | |
184 { | |
185 escaped += "\\\\"; | |
186 } | |
187 else | |
188 { | |
189 escaped += value[i]; | |
190 } | |
191 } | |
192 | |
193 std::string parameter = formatter.GenerateParameter(escaped); | |
194 | |
195 if (constraint.IsCaseSensitive()) | |
196 { | |
197 comparison = (tag + ".value LIKE " + parameter + " " + | |
198 formatter.FormatWildcardEscape()); | |
199 } | |
200 else | |
201 { | |
202 comparison = ("lower(" + tag + ".value) LIKE lower(" + | |
203 parameter + ") " + formatter.FormatWildcardEscape()); | |
204 } | |
205 } | |
206 | |
207 break; | |
208 } | |
209 | |
210 default: | |
211 return false; | |
212 } | |
213 | |
214 if (constraint.IsMandatory()) | |
215 { | |
216 target = comparison; | |
217 } | |
218 else if (comparison.empty()) | |
219 { | |
220 target = tag + ".value IS NULL"; | |
221 } | |
222 else | |
223 { | |
224 target = tag + ".value IS NULL OR " + comparison; | |
225 } | |
226 | |
227 return true; | |
228 } | |
229 | |
230 | |
231 static void FormatJoin(std::string& target, | |
232 const DatabaseConstraint& constraint, | |
233 size_t index) | |
234 { | |
235 std::string tag = "t" + boost::lexical_cast<std::string>(index); | |
236 | |
237 if (constraint.IsMandatory()) | |
238 { | |
239 target = " INNER JOIN "; | |
240 } | |
241 else | |
242 { | |
243 target = " LEFT JOIN "; | |
244 } | |
245 | |
246 if (constraint.IsIdentifier()) | |
247 { | |
248 target += "DicomIdentifiers "; | |
249 } | |
250 else | |
251 { | |
252 target += "MainDicomTags "; | |
253 } | |
254 | |
255 target += (tag + " ON " + tag + ".id = " + FormatLevel(constraint.GetLevel()) + | |
256 ".internalId AND " + tag + ".tagGroup = " + | |
257 boost::lexical_cast<std::string>(constraint.GetTag().GetGroup()) + | |
258 " AND " + tag + ".tagElement = " + | |
259 boost::lexical_cast<std::string>(constraint.GetTag().GetElement())); | |
260 } | |
261 | |
262 | |
263 void ISqlLookupFormatter::Apply(std::string& sql, | |
264 ISqlLookupFormatter& formatter, | |
265 const std::vector<DatabaseConstraint>& lookup, | |
266 ResourceType queryLevel, | |
267 size_t limit) | |
268 { | |
269 assert(ResourceType_Patient < ResourceType_Study && | |
270 ResourceType_Study < ResourceType_Series && | |
271 ResourceType_Series < ResourceType_Instance); | |
272 | |
273 ResourceType upperLevel = queryLevel; | |
274 ResourceType lowerLevel = queryLevel; | |
275 | |
276 for (size_t i = 0; i < lookup.size(); i++) | |
277 { | |
278 ResourceType level = lookup[i].GetLevel(); | |
279 | |
280 if (level < upperLevel) | |
281 { | |
282 upperLevel = level; | |
283 } | |
284 | |
285 if (level > lowerLevel) | |
286 { | |
287 lowerLevel = level; | |
288 } | |
289 } | |
290 | |
291 assert(upperLevel <= queryLevel && | |
292 queryLevel <= lowerLevel); | |
293 | |
294 std::string joins, comparisons; | |
295 | |
296 size_t count = 0; | |
297 | |
298 for (size_t i = 0; i < lookup.size(); i++) | |
299 { | |
300 std::string comparison; | |
301 | |
302 if (FormatComparison(comparison, formatter, lookup[i], count)) | |
303 { | |
304 std::string join; | |
305 FormatJoin(join, lookup[i], count); | |
306 joins += join; | |
307 | |
308 if (!comparison.empty()) | |
309 { | |
310 comparisons += " AND " + comparison; | |
311 } | |
312 | |
313 count ++; | |
314 } | |
315 } | |
316 | |
317 sql = ("SELECT " + | |
318 FormatLevel(queryLevel) + ".publicId, " + | |
319 FormatLevel(queryLevel) + ".internalId" + | |
320 " FROM Resources AS " + FormatLevel(queryLevel)); | |
321 | |
322 for (int level = queryLevel - 1; level >= upperLevel; level--) | |
323 { | |
324 sql += (" INNER JOIN Resources " + | |
325 FormatLevel(static_cast<ResourceType>(level)) + " ON " + | |
326 FormatLevel(static_cast<ResourceType>(level)) + ".internalId=" + | |
327 FormatLevel(static_cast<ResourceType>(level + 1)) + ".parentId"); | |
328 } | |
329 | |
330 for (int level = queryLevel + 1; level <= lowerLevel; level++) | |
331 { | |
332 sql += (" INNER JOIN Resources " + | |
333 FormatLevel(static_cast<ResourceType>(level)) + " ON " + | |
334 FormatLevel(static_cast<ResourceType>(level - 1)) + ".internalId=" + | |
335 FormatLevel(static_cast<ResourceType>(level)) + ".parentId"); | |
336 } | |
337 | |
338 sql += (joins + " WHERE " + FormatLevel(queryLevel) + ".resourceType = " + | |
339 formatter.FormatResourceType(queryLevel) + comparisons); | |
340 | |
341 if (limit != 0) | |
342 { | |
343 sql += " LIMIT " + boost::lexical_cast<std::string>(limit); | |
344 } | |
345 } | |
346 } |