1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
|
/**
* Orthanc - A Lightweight, RESTful DICOM Store
* Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
* Department, University Hospital of Liege, Belgium
* Copyright (C) 2017-2023 Osimis S.A., Belgium
* Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
#if !defined(ORTHANC_BUILDING_SERVER_LIBRARY)
# error Macro ORTHANC_BUILDING_SERVER_LIBRARY must be defined
#endif
#if ORTHANC_BUILDING_SERVER_LIBRARY == 1
# include "../PrecompiledHeadersServer.h"
#endif
#include "ISqlLookupFormatter.h"
#if ORTHANC_BUILDING_SERVER_LIBRARY == 1
# include "../../../OrthancFramework/Sources/OrthancException.h"
#else
# include <OrthancException.h>
#endif
#include "DatabaseConstraint.h"
#include <boost/lexical_cast.hpp>
#include <list>
namespace Orthanc
{
static std::string FormatLevel(ResourceType level)
{
switch (level)
{
case ResourceType_Patient:
return "patients";
case ResourceType_Study:
return "studies";
case ResourceType_Series:
return "series";
case ResourceType_Instance:
return "instances";
default:
throw OrthancException(ErrorCode_InternalError);
}
}
static bool FormatComparison(std::string& target,
ISqlLookupFormatter& formatter,
const DatabaseConstraint& constraint,
size_t index,
bool escapeBrackets)
{
std::string tag = "t" + boost::lexical_cast<std::string>(index);
std::string comparison;
switch (constraint.GetConstraintType())
{
case ConstraintType_Equal:
case ConstraintType_SmallerOrEqual:
case ConstraintType_GreaterOrEqual:
{
std::string op;
switch (constraint.GetConstraintType())
{
case ConstraintType_Equal:
op = "=";
break;
case ConstraintType_SmallerOrEqual:
op = "<=";
break;
case ConstraintType_GreaterOrEqual:
op = ">=";
break;
default:
throw OrthancException(ErrorCode_InternalError);
}
std::string parameter = formatter.GenerateParameter(constraint.GetSingleValue());
if (constraint.IsCaseSensitive())
{
comparison = tag + ".value " + op + " " + parameter;
}
else
{
comparison = "lower(" + tag + ".value) " + op + " lower(" + parameter + ")";
}
break;
}
case ConstraintType_List:
{
for (size_t i = 0; i < constraint.GetValuesCount(); i++)
{
if (!comparison.empty())
{
comparison += ", ";
}
std::string parameter = formatter.GenerateParameter(constraint.GetValue(i));
if (constraint.IsCaseSensitive())
{
comparison += parameter;
}
else
{
comparison += "lower(" + parameter + ")";
}
}
if (constraint.IsCaseSensitive())
{
comparison = tag + ".value IN (" + comparison + ")";
}
else
{
comparison = "lower(" + tag + ".value) IN (" + comparison + ")";
}
break;
}
case ConstraintType_Wildcard:
{
const std::string value = constraint.GetSingleValue();
if (value == "*")
{
if (!constraint.IsMandatory())
{
// Universal constraint on an optional tag, ignore it
return false;
}
}
else
{
std::string escaped;
escaped.reserve(value.size());
for (size_t i = 0; i < value.size(); i++)
{
if (value[i] == '*')
{
escaped += "%";
}
else if (value[i] == '?')
{
escaped += "_";
}
else if (value[i] == '%')
{
escaped += "\\%";
}
else if (value[i] == '_')
{
escaped += "\\_";
}
else if (value[i] == '\\')
{
escaped += "\\\\";
}
else if (escapeBrackets && value[i] == '[')
{
escaped += "\\[";
}
else if (escapeBrackets && value[i] == ']')
{
escaped += "\\]";
}
else
{
escaped += value[i];
}
}
std::string parameter = formatter.GenerateParameter(escaped);
if (constraint.IsCaseSensitive())
{
comparison = (tag + ".value LIKE " + parameter + " " +
formatter.FormatWildcardEscape());
}
else
{
comparison = ("lower(" + tag + ".value) LIKE lower(" +
parameter + ") " + formatter.FormatWildcardEscape());
}
}
break;
}
default:
return false;
}
if (constraint.IsMandatory())
{
target = comparison;
}
else if (comparison.empty())
{
target = tag + ".value IS NULL";
}
else
{
target = tag + ".value IS NULL OR " + comparison;
}
return true;
}
static void FormatJoin(std::string& target,
const DatabaseConstraint& constraint,
size_t index)
{
std::string tag = "t" + boost::lexical_cast<std::string>(index);
if (constraint.IsMandatory())
{
target = " INNER JOIN ";
}
else
{
target = " LEFT JOIN ";
}
if (constraint.IsIdentifier())
{
target += "DicomIdentifiers ";
}
else
{
target += "MainDicomTags ";
}
target += (tag + " ON " + tag + ".id = " + FormatLevel(constraint.GetLevel()) +
".internalId AND " + tag + ".tagGroup = " +
boost::lexical_cast<std::string>(constraint.GetTag().GetGroup()) +
" AND " + tag + ".tagElement = " +
boost::lexical_cast<std::string>(constraint.GetTag().GetElement()));
}
static std::string Join(const std::list<std::string>& values,
const std::string& prefix,
const std::string& separator)
{
if (values.empty())
{
return "";
}
else
{
std::string s = prefix;
bool first = true;
for (std::list<std::string>::const_iterator it = values.begin(); it != values.end(); ++it)
{
if (first)
{
first = false;
}
else
{
s += separator;
}
s += *it;
}
return s;
}
}
void ISqlLookupFormatter::Apply(std::string& sql,
ISqlLookupFormatter& formatter,
const std::vector<DatabaseConstraint>& lookup,
ResourceType queryLevel,
const std::set<std::string>& labels,
LabelsConstraint labelsConstraint,
size_t limit)
{
assert(ResourceType_Patient < ResourceType_Study &&
ResourceType_Study < ResourceType_Series &&
ResourceType_Series < ResourceType_Instance);
ResourceType upperLevel = queryLevel;
ResourceType lowerLevel = queryLevel;
for (size_t i = 0; i < lookup.size(); i++)
{
ResourceType level = lookup[i].GetLevel();
if (level < upperLevel)
{
upperLevel = level;
}
if (level > lowerLevel)
{
lowerLevel = level;
}
}
assert(upperLevel <= queryLevel &&
queryLevel <= lowerLevel);
const bool escapeBrackets = formatter.IsEscapeBrackets();
std::string joins, comparisons;
size_t count = 0;
for (size_t i = 0; i < lookup.size(); i++)
{
std::string comparison;
if (FormatComparison(comparison, formatter, lookup[i], count, escapeBrackets))
{
std::string join;
FormatJoin(join, lookup[i], count);
joins += join;
if (!comparison.empty())
{
comparisons += " AND " + comparison;
}
count ++;
}
}
sql = ("SELECT " +
FormatLevel(queryLevel) + ".publicId, " +
FormatLevel(queryLevel) + ".internalId" +
" FROM Resources AS " + FormatLevel(queryLevel));
for (int level = queryLevel - 1; level >= upperLevel; level--)
{
sql += (" INNER JOIN Resources " +
FormatLevel(static_cast<ResourceType>(level)) + " ON " +
FormatLevel(static_cast<ResourceType>(level)) + ".internalId=" +
FormatLevel(static_cast<ResourceType>(level + 1)) + ".parentId");
}
for (int level = queryLevel + 1; level <= lowerLevel; level++)
{
sql += (" INNER JOIN Resources " +
FormatLevel(static_cast<ResourceType>(level)) + " ON " +
FormatLevel(static_cast<ResourceType>(level - 1)) + ".internalId=" +
FormatLevel(static_cast<ResourceType>(level)) + ".parentId");
}
std::list<std::string> where;
where.push_back(FormatLevel(queryLevel) + ".resourceType = " +
formatter.FormatResourceType(queryLevel) + comparisons);
if (!labels.empty())
{
/**
* "In SQL Server, NOT EXISTS and NOT IN predicates are the best
* way to search for missing values, as long as both columns in
* question are NOT NULL."
* https://explainextended.com/2009/09/15/not-in-vs-not-exists-vs-left-join-is-null-sql-server/
**/
std::list<std::string> formattedLabels;
for (std::set<std::string>::const_iterator it = labels.begin(); it != labels.end(); ++it)
{
formattedLabels.push_back(formatter.GenerateParameter(*it));
}
std::string condition;
switch (labelsConstraint)
{
case LabelsConstraint_Any:
condition = "> 0";
break;
case LabelsConstraint_All:
condition = "= " + boost::lexical_cast<std::string>(labels.size());
break;
case LabelsConstraint_None:
condition = "= 0";
break;
default:
throw OrthancException(ErrorCode_ParameterOutOfRange);
}
where.push_back("(SELECT COUNT(1) FROM Labels AS selectedLabels WHERE selectedLabels.id = " + FormatLevel(queryLevel) +
".internalId AND selectedLabels.label IN (" + Join(formattedLabels, "", ", ") + ")) " + condition);
}
sql += joins + Join(where, " WHERE ", " AND ");
if (limit != 0)
{
sql += " LIMIT " + boost::lexical_cast<std::string>(limit);
}
}
}
|