annotate Resources/Orthanc/Databases/ISqlLookupFormatter.cpp @ 152:063aa53b5917

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