annotate Resources/Orthanc/Databases/ISqlLookupFormatter.cpp @ 353:389c037387ea

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