annotate OrthancServer/Sources/Search/ISqlLookupFormatter.cpp @ 5503:5910aa0c96ce

upgraded to curl 8.5.0
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 24 Jan 2024 17:31:12 +0100
parents 48b8dae6dc77
children f7adfb22e20e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
5485
48b8dae6dc77 upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5477
diff changeset
5 * Copyright (C) 2017-2024 Osimis S.A., Belgium
48b8dae6dc77 upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5477
diff changeset
6 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 *
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * This program is free software: you can redistribute it and/or
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * modify it under the terms of the GNU General Public License as
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * published by the Free Software Foundation, either version 3 of the
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * License, or (at your option) any later version.
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 *
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful, but
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * General Public License for more details.
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 *
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
4096
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
23 #if !defined(ORTHANC_BUILDING_SERVER_LIBRARY)
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
24 # error Macro ORTHANC_BUILDING_SERVER_LIBRARY must be defined
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
25 #endif
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
26
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
27 #if ORTHANC_BUILDING_SERVER_LIBRARY == 1
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
28 # include "../PrecompiledHeadersServer.h"
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
29 #endif
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
30
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 #include "ISqlLookupFormatter.h"
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
4096
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
33 #if ORTHANC_BUILDING_SERVER_LIBRARY == 1
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
34 # include "../../../OrthancFramework/Sources/OrthancException.h"
5422
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
35 # include "../../../OrthancFramework/Sources/Toolbox.h"
4096
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
36 #else
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
37 # include <OrthancException.h>
5422
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
38 # include <Toolbox.h>
4096
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
39 #endif
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
40
3100
f6de9d25b20d removing build dependency on plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3079
diff changeset
41 #include "DatabaseConstraint.h"
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
5477
78f68ad10b69 missing includes for assert()
Alain Mazy <am@osimis.io>
parents: 5460
diff changeset
43 #include <cassert>
4304
50b0c69b653a continued abi
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4096
diff changeset
44 #include <boost/lexical_cast.hpp>
5227
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
45 #include <list>
4304
50b0c69b653a continued abi
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4096
diff changeset
46
50b0c69b653a continued abi
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4096
diff changeset
47
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 namespace Orthanc
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 static std::string FormatLevel(ResourceType level)
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 switch (level)
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 case ResourceType_Patient:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 return "patients";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 case ResourceType_Study:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 return "studies";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 case ResourceType_Series:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 return "series";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 case ResourceType_Instance:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 return "instances";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 default:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 throw OrthancException(ErrorCode_InternalError);
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 static bool FormatComparison(std::string& target,
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 ISqlLookupFormatter& formatter,
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 const DatabaseConstraint& constraint,
4817
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
75 size_t index,
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
76 bool escapeBrackets)
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 std::string tag = "t" + boost::lexical_cast<std::string>(index);
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 std::string comparison;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 switch (constraint.GetConstraintType())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 case ConstraintType_Equal:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 case ConstraintType_SmallerOrEqual:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 case ConstraintType_GreaterOrEqual:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 std::string op;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 switch (constraint.GetConstraintType())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 case ConstraintType_Equal:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 op = "=";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 break;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 case ConstraintType_SmallerOrEqual:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 op = "<=";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 break;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 case ConstraintType_GreaterOrEqual:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 op = ">=";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 break;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 default:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 throw OrthancException(ErrorCode_InternalError);
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 std::string parameter = formatter.GenerateParameter(constraint.GetSingleValue());
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 if (constraint.IsCaseSensitive())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 comparison = tag + ".value " + op + " " + parameter;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 else
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 comparison = "lower(" + tag + ".value) " + op + " lower(" + parameter + ")";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 break;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 case ConstraintType_List:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 for (size_t i = 0; i < constraint.GetValuesCount(); i++)
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 if (!comparison.empty())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 comparison += ", ";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 std::string parameter = formatter.GenerateParameter(constraint.GetValue(i));
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 if (constraint.IsCaseSensitive())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 comparison += parameter;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 else
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 comparison += "lower(" + parameter + ")";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 if (constraint.IsCaseSensitive())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 comparison = tag + ".value IN (" + comparison + ")";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 else
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 comparison = "lower(" + tag + ".value) IN (" + comparison + ")";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 break;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 case ConstraintType_Wildcard:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 const std::string value = constraint.GetSingleValue();
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 if (value == "*")
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 if (!constraint.IsMandatory())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 // Universal constraint on an optional tag, ignore it
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 return false;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 else
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 std::string escaped;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 escaped.reserve(value.size());
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 for (size_t i = 0; i < value.size(); i++)
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 if (value[i] == '*')
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 escaped += "%";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 else if (value[i] == '?')
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 escaped += "_";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 else if (value[i] == '%')
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 escaped += "\\%";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 else if (value[i] == '_')
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 escaped += "\\_";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 else if (value[i] == '\\')
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 escaped += "\\\\";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 }
4817
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
193 else if (escapeBrackets && value[i] == '[')
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
194 {
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
195 escaped += "\\[";
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
196 }
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
197 else if (escapeBrackets && value[i] == ']')
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
198 {
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
199 escaped += "\\]";
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
200 }
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 else
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 escaped += value[i];
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 std::string parameter = formatter.GenerateParameter(escaped);
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 if (constraint.IsCaseSensitive())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 {
3079
65e2bfa953ef ISqlLookupFormatter::FormatWildcardEscape()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3078
diff changeset
211 comparison = (tag + ".value LIKE " + parameter + " " +
65e2bfa953ef ISqlLookupFormatter::FormatWildcardEscape()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3078
diff changeset
212 formatter.FormatWildcardEscape());
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 else
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 {
3079
65e2bfa953ef ISqlLookupFormatter::FormatWildcardEscape()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3078
diff changeset
216 comparison = ("lower(" + tag + ".value) LIKE lower(" +
65e2bfa953ef ISqlLookupFormatter::FormatWildcardEscape()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3078
diff changeset
217 parameter + ") " + formatter.FormatWildcardEscape());
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 break;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 default:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 return false;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 if (constraint.IsMandatory())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 target = comparison;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 else if (comparison.empty())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 target = tag + ".value IS NULL";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 else
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 target = tag + ".value IS NULL OR " + comparison;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 return true;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 static void FormatJoin(std::string& target,
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 const DatabaseConstraint& constraint,
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 size_t index)
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 std::string tag = "t" + boost::lexical_cast<std::string>(index);
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 if (constraint.IsMandatory())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 target = " INNER JOIN ";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 else
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 target = " LEFT JOIN ";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 if (constraint.IsIdentifier())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 target += "DicomIdentifiers ";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 else
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266 target += "MainDicomTags ";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 target += (tag + " ON " + tag + ".id = " + FormatLevel(constraint.GetLevel()) +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 ".internalId AND " + tag + ".tagGroup = " +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 boost::lexical_cast<std::string>(constraint.GetTag().GetGroup()) +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272 " AND " + tag + ".tagElement = " +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273 boost::lexical_cast<std::string>(constraint.GetTag().GetElement()));
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 }
5227
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
275
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
276
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
277 static std::string Join(const std::list<std::string>& values,
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
278 const std::string& prefix,
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
279 const std::string& separator)
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
280 {
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
281 if (values.empty())
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
282 {
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
283 return "";
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
284 }
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
285 else
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
286 {
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
287 std::string s = prefix;
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
288
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
289 bool first = true;
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
290 for (std::list<std::string>::const_iterator it = values.begin(); it != values.end(); ++it)
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
291 {
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
292 if (first)
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
293 {
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
294 first = false;
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
295 }
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
296 else
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
297 {
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
298 s += separator;
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
299 }
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
300
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
301 s += *it;
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
302 }
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
303
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
304 return s;
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
305 }
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
306 }
5422
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
307
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
308 static bool FormatComparison2(std::string& target,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
309 ISqlLookupFormatter& formatter,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
310 const DatabaseConstraint& constraint,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
311 bool escapeBrackets)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
312 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
313 std::string comparison;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
314 std::string tagFilter = ("tagGroup = " + boost::lexical_cast<std::string>(constraint.GetTag().GetGroup())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
315 + " AND tagElement = " + boost::lexical_cast<std::string>(constraint.GetTag().GetElement()));
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
316
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
317 switch (constraint.GetConstraintType())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
318 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
319 case ConstraintType_Equal:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
320 case ConstraintType_SmallerOrEqual:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
321 case ConstraintType_GreaterOrEqual:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
322 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
323 std::string op;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
324 switch (constraint.GetConstraintType())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
325 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
326 case ConstraintType_Equal:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
327 op = "=";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
328 break;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
329
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
330 case ConstraintType_SmallerOrEqual:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
331 op = "<=";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
332 break;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
333
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
334 case ConstraintType_GreaterOrEqual:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
335 op = ">=";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
336 break;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
337
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
338 default:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
339 throw OrthancException(ErrorCode_InternalError);
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
340 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
341
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
342 std::string parameter = formatter.GenerateParameter(constraint.GetSingleValue());
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
343
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
344 if (constraint.IsCaseSensitive())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
345 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
346 comparison = " AND value " + op + " " + parameter;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
347 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
348 else
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
349 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
350 comparison = " AND lower(value) " + op + " lower(" + parameter + ")";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
351 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
352
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
353 break;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
354 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
355
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
356 case ConstraintType_List:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
357 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
358 std::vector<std::string> comparisonValues;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
359 for (size_t i = 0; i < constraint.GetValuesCount(); i++)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
360 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
361 std::string parameter = formatter.GenerateParameter(constraint.GetValue(i));
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
362
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
363 if (constraint.IsCaseSensitive())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
364 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
365 comparisonValues.push_back(parameter);
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
366 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
367 else
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
368 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
369 comparisonValues.push_back("lower(" + parameter + ")");
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
370 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
371 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
372
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
373 std::string values;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
374 Toolbox::JoinStrings(values, comparisonValues, ", ");
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
375
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
376 if (constraint.IsCaseSensitive())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
377 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
378 comparison = " AND value IN (" + values + ")";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
379 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
380 else
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
381 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
382 comparison = " AND lower(value) IN (" + values + ")";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
383 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
384
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
385 break;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
386 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
387
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
388 case ConstraintType_Wildcard:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
389 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
390 const std::string value = constraint.GetSingleValue();
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
391
5422
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
392 if (value == "*")
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
393 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
394 if (!constraint.IsMandatory())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
395 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
396 // Universal constraint on an optional tag, ignore it
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
397 return false;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
398 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
399 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
400 else
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
401 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
402 std::string escaped;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
403 escaped.reserve(value.size());
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
404
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
405 for (size_t i = 0; i < value.size(); i++)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
406 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
407 if (value[i] == '*')
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
408 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
409 escaped += "%";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
410 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
411 else if (value[i] == '?')
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
412 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
413 escaped += "_";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
414 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
415 else if (value[i] == '%')
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
416 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
417 escaped += "\\%";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
418 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
419 else if (value[i] == '_')
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
420 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
421 escaped += "\\_";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
422 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
423 else if (value[i] == '\\')
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
424 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
425 escaped += "\\\\";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
426 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
427 else if (escapeBrackets && value[i] == '[')
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
428 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
429 escaped += "\\[";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
430 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
431 else if (escapeBrackets && value[i] == ']')
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
432 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
433 escaped += "\\]";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
434 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
435 else
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
436 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
437 escaped += value[i];
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
438 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
439 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
440
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
441 std::string parameter = formatter.GenerateParameter(escaped);
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
442
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
443 if (constraint.IsCaseSensitive())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
444 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
445 comparison = " AND value LIKE " + parameter + " " + formatter.FormatWildcardEscape();
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
446 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
447 else
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
448 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
449 comparison = " AND lower(value) LIKE lower(" + parameter + ") " + formatter.FormatWildcardEscape();
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
450 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
451 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
452
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
453 break;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
454 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
455
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
456 default:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
457 return false;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
458 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
459
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
460 if (constraint.IsMandatory())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
461 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
462 target = tagFilter + comparison;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
463 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
464 else if (comparison.empty())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
465 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
466 target = tagFilter + " AND value IS NULL";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
467 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
468 else
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
469 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
470 target = tagFilter + " AND value IS NULL OR " + comparison;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
471 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
472
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
473 return true;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
474 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
475
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
476
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
477 void ISqlLookupFormatter::GetLookupLevels(ResourceType& lowerLevel, ResourceType& upperLevel, const ResourceType& queryLevel, const std::vector<DatabaseConstraint>& lookup)
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
478 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
479 assert(ResourceType_Patient < ResourceType_Study &&
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
480 ResourceType_Study < ResourceType_Series &&
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
481 ResourceType_Series < ResourceType_Instance);
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
482
5422
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
483 lowerLevel = queryLevel;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
484 upperLevel = queryLevel;
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
485
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
486 for (size_t i = 0; i < lookup.size(); i++)
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
487 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
488 ResourceType level = lookup[i].GetLevel();
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
489
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
490 if (level < upperLevel)
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
491 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
492 upperLevel = level;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
493 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
494
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
495 if (level > lowerLevel)
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
496 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
497 lowerLevel = level;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
498 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
499 }
5422
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
500 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
501
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
502
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
503 void ISqlLookupFormatter::Apply(std::string& sql,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
504 ISqlLookupFormatter& formatter,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
505 const std::vector<DatabaseConstraint>& lookup,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
506 ResourceType queryLevel,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
507 const std::set<std::string>& labels,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
508 LabelsConstraint labelsConstraint,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
509 size_t limit)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
510 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
511 ResourceType lowerLevel, upperLevel;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
512 GetLookupLevels(lowerLevel, upperLevel, queryLevel, lookup);
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
513
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
514 assert(upperLevel <= queryLevel &&
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
515 queryLevel <= lowerLevel);
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
516
4817
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
517 const bool escapeBrackets = formatter.IsEscapeBrackets();
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
518
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
519 std::string joins, comparisons;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
520
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
521 size_t count = 0;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
522
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
523 for (size_t i = 0; i < lookup.size(); i++)
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
524 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
525 std::string comparison;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
526
4817
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
527 if (FormatComparison(comparison, formatter, lookup[i], count, escapeBrackets))
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
528 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
529 std::string join;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
530 FormatJoin(join, lookup[i], count);
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
531 joins += join;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
532
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
533 if (!comparison.empty())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
534 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
535 comparisons += " AND " + comparison;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
536 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
537
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
538 count ++;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
539 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
540 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
541
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
542 sql = ("SELECT " +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
543 FormatLevel(queryLevel) + ".publicId, " +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
544 FormatLevel(queryLevel) + ".internalId" +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
545 " FROM Resources AS " + FormatLevel(queryLevel));
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
546
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
547 for (int level = queryLevel - 1; level >= upperLevel; level--)
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
548 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
549 sql += (" INNER JOIN Resources " +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
550 FormatLevel(static_cast<ResourceType>(level)) + " ON " +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
551 FormatLevel(static_cast<ResourceType>(level)) + ".internalId=" +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
552 FormatLevel(static_cast<ResourceType>(level + 1)) + ".parentId");
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
553 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
554
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
555 for (int level = queryLevel + 1; level <= lowerLevel; level++)
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
556 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
557 sql += (" INNER JOIN Resources " +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
558 FormatLevel(static_cast<ResourceType>(level)) + " ON " +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
559 FormatLevel(static_cast<ResourceType>(level - 1)) + ".internalId=" +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
560 FormatLevel(static_cast<ResourceType>(level)) + ".parentId");
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
561 }
5227
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
562
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
563 std::list<std::string> where;
5256
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5248
diff changeset
564 where.push_back(FormatLevel(queryLevel) + ".resourceType = " +
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5248
diff changeset
565 formatter.FormatResourceType(queryLevel) + comparisons);
5227
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
566
5248
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
567 if (!labels.empty())
5227
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
568 {
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
569 /**
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
570 * "In SQL Server, NOT EXISTS and NOT IN predicates are the best
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
571 * way to search for missing values, as long as both columns in
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
572 * question are NOT NULL."
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
573 * https://explainextended.com/2009/09/15/not-in-vs-not-exists-vs-left-join-is-null-sql-server/
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
574 **/
5248
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
575
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
576 std::list<std::string> formattedLabels;
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
577 for (std::set<std::string>::const_iterator it = labels.begin(); it != labels.end(); ++it)
5227
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
578 {
5248
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
579 formattedLabels.push_back(formatter.GenerateParameter(*it));
5227
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
580 }
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
581
5248
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
582 std::string condition;
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
583 switch (labelsConstraint)
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
584 {
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
585 case LabelsConstraint_Any:
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
586 condition = "> 0";
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
587 break;
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
588
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
589 case LabelsConstraint_All:
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
590 condition = "= " + boost::lexical_cast<std::string>(labels.size());
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
591 break;
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
592
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
593 case LabelsConstraint_None:
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
594 condition = "= 0";
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
595 break;
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
596
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
597 default:
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
598 throw OrthancException(ErrorCode_ParameterOutOfRange);
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
599 }
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
600
5256
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5248
diff changeset
601 where.push_back("(SELECT COUNT(1) FROM Labels AS selectedLabels WHERE selectedLabels.id = " + FormatLevel(queryLevel) +
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5248
diff changeset
602 ".internalId AND selectedLabels.label IN (" + Join(formattedLabels, "", ", ") + ")) " + condition);
5227
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
603 }
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
604
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
605 sql += joins + Join(where, " WHERE ", " AND ");
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
606
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
607 if (limit != 0)
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
608 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
609 sql += " LIMIT " + boost::lexical_cast<std::string>(limit);
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
610 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
611 }
5422
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
612
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
613
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
614 void ISqlLookupFormatter::ApplySingleLevel(std::string& sql,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
615 ISqlLookupFormatter& formatter,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
616 const std::vector<DatabaseConstraint>& lookup,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
617 ResourceType queryLevel,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
618 const std::set<std::string>& labels,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
619 LabelsConstraint labelsConstraint,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
620 size_t limit
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
621 )
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
622 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
623 ResourceType lowerLevel, upperLevel;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
624 GetLookupLevels(lowerLevel, upperLevel, queryLevel, lookup);
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
625
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
626 assert(upperLevel == queryLevel &&
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
627 queryLevel == lowerLevel);
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
628
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
629 const bool escapeBrackets = formatter.IsEscapeBrackets();
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
630
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
631 std::vector<std::string> mainDicomTagsComparisons, dicomIdentifiersComparisons;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
632
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
633 for (size_t i = 0; i < lookup.size(); i++)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
634 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
635 std::string comparison;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
636
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
637 if (FormatComparison2(comparison, formatter, lookup[i], escapeBrackets))
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
638 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
639 if (!comparison.empty())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
640 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
641 if (lookup[i].IsIdentifier())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
642 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
643 dicomIdentifiersComparisons.push_back(comparison);
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
644 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
645 else
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
646 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
647 mainDicomTagsComparisons.push_back(comparison);
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
648 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
649 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
650 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
651 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
652
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
653 sql = ("SELECT publicId, internalId "
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
654 "FROM Resources "
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
655 "WHERE resourceType = " + formatter.FormatResourceType(queryLevel)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
656 + " ");
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
657
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
658 if (dicomIdentifiersComparisons.size() > 0)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
659 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
660 for (std::vector<std::string>::const_iterator it = dicomIdentifiersComparisons.begin(); it < dicomIdentifiersComparisons.end(); ++it)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
661 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
662 sql += (" AND internalId IN (SELECT id FROM DicomIdentifiers WHERE " + *it + ") ");
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
663 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
664 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
665
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
666 if (mainDicomTagsComparisons.size() > 0)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
667 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
668 for (std::vector<std::string>::const_iterator it = mainDicomTagsComparisons.begin(); it < mainDicomTagsComparisons.end(); ++it)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
669 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
670 sql += (" AND internalId IN (SELECT id FROM MainDicomTags WHERE " + *it + ") ");
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
671 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
672 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
673
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
674 if (!labels.empty())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
675 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
676 /**
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
677 * "In SQL Server, NOT EXISTS and NOT IN predicates are the best
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
678 * way to search for missing values, as long as both columns in
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
679 * question are NOT NULL."
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
680 * https://explainextended.com/2009/09/15/not-in-vs-not-exists-vs-left-join-is-null-sql-server/
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
681 **/
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
682
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
683 std::list<std::string> formattedLabels;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
684 for (std::set<std::string>::const_iterator it = labels.begin(); it != labels.end(); ++it)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
685 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
686 formattedLabels.push_back(formatter.GenerateParameter(*it));
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
687 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
688
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
689 std::string condition;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
690 std::string inOrNotIn;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
691 switch (labelsConstraint)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
692 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
693 case LabelsConstraint_Any:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
694 condition = "> 0";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
695 inOrNotIn = "IN";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
696 break;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
697
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
698 case LabelsConstraint_All:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
699 condition = "= " + boost::lexical_cast<std::string>(labels.size());
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
700 inOrNotIn = "IN";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
701 break;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
702
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
703 case LabelsConstraint_None:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
704 condition = "> 0";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
705 inOrNotIn = "NOT IN";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
706 break;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
707
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
708 default:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
709 throw OrthancException(ErrorCode_ParameterOutOfRange);
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
710 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
711
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
712 sql += (" AND internalId " + inOrNotIn + " (SELECT id"
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
713 " FROM (SELECT id, COUNT(1) AS labelsCount "
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
714 "FROM Labels "
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
715 "WHERE label IN (" + Join(formattedLabels, "", ", ") + ") GROUP BY id"
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
716 ") AS temp "
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
717 " WHERE labelsCount " + condition + ")");
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
718 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
719
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
720 if (limit != 0)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
721 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
722 sql += " LIMIT " + boost::lexical_cast<std::string>(limit);
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
723 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
724 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
725
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
726 }