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