annotate OrthancServer/Sources/Search/ISqlLookupFormatter.cpp @ 5663:3765085693e5 large-queries

merge default -> large-queries
author Alain Mazy <am@orthanc.team>
date Thu, 04 Jul 2024 07:40:58 +0200
parents f7adfb22e20e
children 68fc5af30c03
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
5640
f7adfb22e20e updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5485
diff changeset
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium
f7adfb22e20e updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5485
diff changeset
6 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium
5485
48b8dae6dc77 upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5477
diff changeset
7 * 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
8 *
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * 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
10 * 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
11 * 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
12 * 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
13 *
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * 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
15 * 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
16 * 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
17 * 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
18 *
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * 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
20 * 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
21 **/
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
4096
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
24 #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
25 # 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
26 #endif
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
27
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
28 #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
29 # include "../PrecompiledHeadersServer.h"
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
30 #endif
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
31
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 #include "ISqlLookupFormatter.h"
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
4096
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
34 #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
35 # include "../../../OrthancFramework/Sources/OrthancException.h"
5422
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
36 # 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
37 #else
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
38 # include <OrthancException.h>
5422
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
39 # include <Toolbox.h>
4096
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
40 #endif
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
41
3100
f6de9d25b20d removing build dependency on plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3079
diff changeset
42 #include "DatabaseConstraint.h"
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43
5477
78f68ad10b69 missing includes for assert()
Alain Mazy <am@osimis.io>
parents: 5460
diff changeset
44 #include <cassert>
4304
50b0c69b653a continued abi
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4096
diff changeset
45 #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
46 #include <list>
4304
50b0c69b653a continued abi
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4096
diff changeset
47
50b0c69b653a continued abi
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4096
diff changeset
48
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 namespace Orthanc
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 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
52 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 switch (level)
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 case ResourceType_Patient:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 return "patients";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 case ResourceType_Study:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 return "studies";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 case ResourceType_Series:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 return "series";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 case ResourceType_Instance:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 return "instances";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 default:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 throw OrthancException(ErrorCode_InternalError);
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 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
74 ISqlLookupFormatter& formatter,
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 const DatabaseConstraint& constraint,
4817
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
76 size_t index,
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
77 bool escapeBrackets)
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 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
80
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 std::string comparison;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 switch (constraint.GetConstraintType())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 case ConstraintType_Equal:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 case ConstraintType_SmallerOrEqual:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 case ConstraintType_GreaterOrEqual:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 std::string op;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 switch (constraint.GetConstraintType())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 case ConstraintType_Equal:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 op = "=";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 break;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 case ConstraintType_SmallerOrEqual:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 op = "<=";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 break;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 case ConstraintType_GreaterOrEqual:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 op = ">=";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 break;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 default:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 throw OrthancException(ErrorCode_InternalError);
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 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
109
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 if (constraint.IsCaseSensitive())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 comparison = tag + ".value " + op + " " + parameter;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 else
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 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
117 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 break;
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 case ConstraintType_List:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 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
125 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 if (!comparison.empty())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 comparison += ", ";
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 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
132
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 if (constraint.IsCaseSensitive())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 comparison += parameter;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 else
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 comparison += "lower(" + parameter + ")";
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 if (constraint.IsCaseSensitive())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 comparison = tag + ".value IN (" + comparison + ")";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 else
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 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
150 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 break;
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 case ConstraintType_Wildcard:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 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
158
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 if (value == "*")
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 if (!constraint.IsMandatory())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 // 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
164 return false;
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 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 else
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 std::string escaped;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 escaped.reserve(value.size());
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 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
173 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 if (value[i] == '*')
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 escaped += "%";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 else if (value[i] == '?')
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 escaped += "_";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 else if (value[i] == '%')
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 escaped += "\\%";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 else if (value[i] == '_')
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 escaped += "\\_";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 else if (value[i] == '\\')
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 escaped += "\\\\";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 }
4817
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
194 else if (escapeBrackets && value[i] == '[')
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
195 {
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
196 escaped += "\\[";
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
197 }
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
198 else if (escapeBrackets && value[i] == ']')
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
199 {
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
200 escaped += "\\]";
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
201 }
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 else
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 escaped += value[i];
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 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
209
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 if (constraint.IsCaseSensitive())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 {
3079
65e2bfa953ef ISqlLookupFormatter::FormatWildcardEscape()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3078
diff changeset
212 comparison = (tag + ".value LIKE " + parameter + " " +
65e2bfa953ef ISqlLookupFormatter::FormatWildcardEscape()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3078
diff changeset
213 formatter.FormatWildcardEscape());
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 else
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 {
3079
65e2bfa953ef ISqlLookupFormatter::FormatWildcardEscape()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3078
diff changeset
217 comparison = ("lower(" + tag + ".value) LIKE lower(" +
65e2bfa953ef ISqlLookupFormatter::FormatWildcardEscape()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3078
diff changeset
218 parameter + ") " + formatter.FormatWildcardEscape());
3058
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 break;
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 default:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 return false;
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 if (constraint.IsMandatory())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 target = comparison;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 else if (comparison.empty())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 target = tag + ".value IS NULL";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 else
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 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
240 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 return true;
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 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
247 const DatabaseConstraint& constraint,
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 size_t index)
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 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
251
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 if (constraint.IsMandatory())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 target = " INNER JOIN ";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 else
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 target = " LEFT JOIN ";
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 if (constraint.IsIdentifier())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 target += "DicomIdentifiers ";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 else
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 target += "MainDicomTags ";
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 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
271 ".internalId AND " + tag + ".tagGroup = " +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272 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
273 " AND " + tag + ".tagElement = " +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 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
275 }
5227
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
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
278 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
279 const std::string& prefix,
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
280 const std::string& separator)
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
281 {
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
282 if (values.empty())
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
283 {
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
284 return "";
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
285 }
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
286 else
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
287 {
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
288 std::string s = prefix;
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
289
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
290 bool first = true;
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
291 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
292 {
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
293 if (first)
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
294 {
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
295 first = false;
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
296 }
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
297 else
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
298 {
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
299 s += separator;
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
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
302 s += *it;
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
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
305 return s;
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
306 }
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
307 }
5422
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
308
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
309 static bool FormatComparison2(std::string& target,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
310 ISqlLookupFormatter& formatter,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
311 const DatabaseConstraint& constraint,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
312 bool escapeBrackets)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
313 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
314 std::string comparison;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
315 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
316 + " 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
317
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
318 switch (constraint.GetConstraintType())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
319 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
320 case ConstraintType_Equal:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
321 case ConstraintType_SmallerOrEqual:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
322 case ConstraintType_GreaterOrEqual:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
323 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
324 std::string op;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
325 switch (constraint.GetConstraintType())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
326 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
327 case ConstraintType_Equal:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
328 op = "=";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
329 break;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
330
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
331 case ConstraintType_SmallerOrEqual:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
332 op = "<=";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
333 break;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
334
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
335 case ConstraintType_GreaterOrEqual:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
336 op = ">=";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
337 break;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
338
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
339 default:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
340 throw OrthancException(ErrorCode_InternalError);
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
343 std::string parameter = formatter.GenerateParameter(constraint.GetSingleValue());
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
344
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
345 if (constraint.IsCaseSensitive())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
346 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
347 comparison = " AND value " + op + " " + parameter;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
348 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
349 else
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
350 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
351 comparison = " AND lower(value) " + op + " lower(" + parameter + ")";
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
354 break;
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
357 case ConstraintType_List:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
358 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
359 std::vector<std::string> comparisonValues;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
360 for (size_t i = 0; i < constraint.GetValuesCount(); i++)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
361 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
362 std::string parameter = formatter.GenerateParameter(constraint.GetValue(i));
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
363
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
364 if (constraint.IsCaseSensitive())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
365 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
366 comparisonValues.push_back(parameter);
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
367 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
368 else
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
369 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
370 comparisonValues.push_back("lower(" + parameter + ")");
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
374 std::string values;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
375 Toolbox::JoinStrings(values, comparisonValues, ", ");
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
376
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
377 if (constraint.IsCaseSensitive())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
378 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
379 comparison = " AND value IN (" + values + ")";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
380 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
381 else
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
382 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
383 comparison = " AND lower(value) IN (" + values + ")";
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
386 break;
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
389 case ConstraintType_Wildcard:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
390 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
391 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
392
5422
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
393 if (value == "*")
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
394 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
395 if (!constraint.IsMandatory())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
396 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
397 // Universal constraint on an optional tag, ignore it
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
398 return false;
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 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
401 else
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
402 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
403 std::string escaped;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
404 escaped.reserve(value.size());
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
405
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
406 for (size_t i = 0; i < value.size(); i++)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
407 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
408 if (value[i] == '*')
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
409 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
410 escaped += "%";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
411 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
412 else if (value[i] == '?')
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
413 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
414 escaped += "_";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
415 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
416 else if (value[i] == '%')
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
417 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
418 escaped += "\\%";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
419 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
420 else if (value[i] == '_')
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
421 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
422 escaped += "\\_";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
423 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
424 else if (value[i] == '\\')
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
425 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
426 escaped += "\\\\";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
427 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
428 else if (escapeBrackets && value[i] == '[')
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
429 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
430 escaped += "\\[";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
431 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
432 else if (escapeBrackets && value[i] == ']')
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
433 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
434 escaped += "\\]";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
435 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
436 else
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
437 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
438 escaped += value[i];
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
442 std::string parameter = formatter.GenerateParameter(escaped);
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
443
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
444 if (constraint.IsCaseSensitive())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
445 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
446 comparison = " AND value LIKE " + parameter + " " + formatter.FormatWildcardEscape();
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
447 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
448 else
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
449 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
450 comparison = " AND lower(value) LIKE lower(" + parameter + ") " + formatter.FormatWildcardEscape();
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
454 break;
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
457 default:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
458 return false;
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
461 if (constraint.IsMandatory())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
462 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
463 target = tagFilter + comparison;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
464 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
465 else if (comparison.empty())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
466 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
467 target = tagFilter + " AND value IS NULL";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
468 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
469 else
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
470 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
471 target = tagFilter + " AND value IS NULL OR " + comparison;
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
474 return true;
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
478 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
479 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
480 assert(ResourceType_Patient < ResourceType_Study &&
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
481 ResourceType_Study < ResourceType_Series &&
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
482 ResourceType_Series < ResourceType_Instance);
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
483
5422
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
484 lowerLevel = queryLevel;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
485 upperLevel = queryLevel;
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
486
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
487 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
488 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
489 ResourceType level = lookup[i].GetLevel();
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
490
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
491 if (level < upperLevel)
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
492 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
493 upperLevel = level;
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
496 if (level > lowerLevel)
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
497 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
498 lowerLevel = level;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
499 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
500 }
5422
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
504 void ISqlLookupFormatter::Apply(std::string& sql,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
505 ISqlLookupFormatter& formatter,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
506 const std::vector<DatabaseConstraint>& lookup,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
507 ResourceType queryLevel,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
508 const std::set<std::string>& labels,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
509 LabelsConstraint labelsConstraint,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
510 size_t limit)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
511 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
512 ResourceType lowerLevel, upperLevel;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
513 GetLookupLevels(lowerLevel, upperLevel, queryLevel, lookup);
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
514
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
515 assert(upperLevel <= queryLevel &&
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
516 queryLevel <= lowerLevel);
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
517
4817
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
518 const bool escapeBrackets = formatter.IsEscapeBrackets();
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
519
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
520 std::string joins, comparisons;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
521
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
522 size_t count = 0;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
523
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
524 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
525 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
526 std::string comparison;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
527
4817
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
528 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
529 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
530 std::string join;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
531 FormatJoin(join, lookup[i], count);
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
532 joins += join;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
533
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
534 if (!comparison.empty())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
535 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
536 comparisons += " AND " + comparison;
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
539 count ++;
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
543 sql = ("SELECT " +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
544 FormatLevel(queryLevel) + ".publicId, " +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
545 FormatLevel(queryLevel) + ".internalId" +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
546 " FROM Resources AS " + FormatLevel(queryLevel));
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
547
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
548 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
549 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
550 sql += (" INNER JOIN Resources " +
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)) + " ON " +
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)) + ".internalId=" +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
553 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
554 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
555
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
556 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
557 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
558 sql += (" INNER JOIN Resources " +
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)) + " ON " +
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 - 1)) + ".internalId=" +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
561 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
562 }
5227
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
563
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
564 std::list<std::string> where;
5256
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5248
diff changeset
565 where.push_back(FormatLevel(queryLevel) + ".resourceType = " +
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5248
diff changeset
566 formatter.FormatResourceType(queryLevel) + comparisons);
5227
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
567
5248
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
568 if (!labels.empty())
5227
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 /**
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
571 * "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
572 * 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
573 * question are NOT NULL."
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
574 * 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
575 **/
5248
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
576
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
577 std::list<std::string> formattedLabels;
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
578 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
579 {
5248
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
580 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
581 }
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
582
5248
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
583 std::string condition;
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
584 switch (labelsConstraint)
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
585 {
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
586 case LabelsConstraint_Any:
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
587 condition = "> 0";
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
588 break;
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
589
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
590 case LabelsConstraint_All:
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
591 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
592 break;
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
593
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
594 case LabelsConstraint_None:
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
595 condition = "= 0";
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
596 break;
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
597
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
598 default:
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
599 throw OrthancException(ErrorCode_ParameterOutOfRange);
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
600 }
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
601
5256
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5248
diff changeset
602 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
603 ".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
604 }
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
605
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
606 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
607
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
608 if (limit != 0)
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
609 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
610 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
611 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
612 }
5422
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
615 void ISqlLookupFormatter::ApplySingleLevel(std::string& sql,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
616 ISqlLookupFormatter& formatter,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
617 const std::vector<DatabaseConstraint>& lookup,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
618 ResourceType queryLevel,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
619 const std::set<std::string>& labels,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
620 LabelsConstraint labelsConstraint,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
621 size_t limit
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 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
624 ResourceType lowerLevel, upperLevel;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
625 GetLookupLevels(lowerLevel, upperLevel, queryLevel, lookup);
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
626
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
627 assert(upperLevel == queryLevel &&
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
628 queryLevel == lowerLevel);
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
629
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
630 const bool escapeBrackets = formatter.IsEscapeBrackets();
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
631
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
632 std::vector<std::string> mainDicomTagsComparisons, dicomIdentifiersComparisons;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
633
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
634 for (size_t i = 0; i < lookup.size(); i++)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
635 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
636 std::string comparison;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
637
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
638 if (FormatComparison2(comparison, formatter, lookup[i], escapeBrackets))
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
639 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
640 if (!comparison.empty())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
641 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
642 if (lookup[i].IsIdentifier())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
643 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
644 dicomIdentifiersComparisons.push_back(comparison);
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
645 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
646 else
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
647 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
648 mainDicomTagsComparisons.push_back(comparison);
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
654 sql = ("SELECT publicId, internalId "
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
655 "FROM Resources "
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
656 "WHERE resourceType = " + formatter.FormatResourceType(queryLevel)
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
659 if (dicomIdentifiersComparisons.size() > 0)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
660 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
661 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
662 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
663 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
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
667 if (mainDicomTagsComparisons.size() > 0)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
668 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
669 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
670 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
671 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
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
675 if (!labels.empty())
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 /**
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
678 * "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
679 * 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
680 * question are NOT NULL."
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
681 * 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
682 **/
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
683
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
684 std::list<std::string> formattedLabels;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
685 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
686 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
687 formattedLabels.push_back(formatter.GenerateParameter(*it));
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
690 std::string condition;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
691 std::string inOrNotIn;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
692 switch (labelsConstraint)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
693 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
694 case LabelsConstraint_Any:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
695 condition = "> 0";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
696 inOrNotIn = "IN";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
697 break;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
698
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
699 case LabelsConstraint_All:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
700 condition = "= " + boost::lexical_cast<std::string>(labels.size());
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
701 inOrNotIn = "IN";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
702 break;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
703
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
704 case LabelsConstraint_None:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
705 condition = "> 0";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
706 inOrNotIn = "NOT IN";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
707 break;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
708
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
709 default:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
710 throw OrthancException(ErrorCode_ParameterOutOfRange);
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
713 sql += (" AND internalId " + inOrNotIn + " (SELECT id"
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
714 " FROM (SELECT id, COUNT(1) AS labelsCount "
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
715 "FROM Labels "
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
716 "WHERE label IN (" + Join(formattedLabels, "", ", ") + ") GROUP BY id"
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
717 ") AS temp "
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
718 " WHERE labelsCount " + condition + ")");
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
721 if (limit != 0)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
722 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
723 sql += " LIMIT " + boost::lexical_cast<std::string>(limit);
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 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
726
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
727 }