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