annotate OrthancServer/Sources/Search/ISqlLookupFormatter.cpp @ 5460:1474fd6ea6c6

CppCheck
author Alain Mazy <am@osimis.io>
date Mon, 11 Dec 2023 12:03:48 +0100
parents 7f37233359aa
children 78f68ad10b69
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
5185
0ea402b4d901 upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4892
diff changeset
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium
0ea402b4d901 upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4892
diff changeset
6 * Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 *
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * This program is free software: you can redistribute it and/or
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * modify it under the terms of the GNU General Public License as
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * published by the Free Software Foundation, either version 3 of the
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * License, or (at your option) any later version.
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 *
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful, but
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * General Public License for more details.
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 *
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
4096
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
23 #if !defined(ORTHANC_BUILDING_SERVER_LIBRARY)
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
24 # error Macro ORTHANC_BUILDING_SERVER_LIBRARY must be defined
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
25 #endif
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
26
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
27 #if ORTHANC_BUILDING_SERVER_LIBRARY == 1
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
28 # include "../PrecompiledHeadersServer.h"
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
29 #endif
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
30
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 #include "ISqlLookupFormatter.h"
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
4096
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
33 #if ORTHANC_BUILDING_SERVER_LIBRARY == 1
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
34 # include "../../../OrthancFramework/Sources/OrthancException.h"
5422
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
35 # include "../../../OrthancFramework/Sources/Toolbox.h"
4096
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
36 #else
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
37 # include <OrthancException.h>
5422
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
38 # include <Toolbox.h>
4096
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
39 #endif
d633e5bb7ba3 reusing of ISqlLookupFormatter and DatabaseConstraint in orthanc-databases project
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4045
diff changeset
40
3100
f6de9d25b20d removing build dependency on plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3079
diff changeset
41 #include "DatabaseConstraint.h"
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
4304
50b0c69b653a continued abi
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4096
diff changeset
43 #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
44 #include <list>
4304
50b0c69b653a continued abi
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4096
diff changeset
45
50b0c69b653a continued abi
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4096
diff changeset
46
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 namespace Orthanc
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 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
50 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 switch (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 case ResourceType_Patient:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 return "patients";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 case ResourceType_Study:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 return "studies";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 case ResourceType_Series:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 return "series";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 case ResourceType_Instance:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 return "instances";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 default:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 throw OrthancException(ErrorCode_InternalError);
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 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
72 ISqlLookupFormatter& formatter,
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 const DatabaseConstraint& constraint,
4817
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
74 size_t index,
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
75 bool escapeBrackets)
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 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
78
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 std::string comparison;
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 switch (constraint.GetConstraintType())
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 case ConstraintType_Equal:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 case ConstraintType_SmallerOrEqual:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 case ConstraintType_GreaterOrEqual:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 std::string op;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 switch (constraint.GetConstraintType())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 case ConstraintType_Equal:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 op = "=";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 break;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 case ConstraintType_SmallerOrEqual:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 op = "<=";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 break;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 case ConstraintType_GreaterOrEqual:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 op = ">=";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 break;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 default:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 throw OrthancException(ErrorCode_InternalError);
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 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
107
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 if (constraint.IsCaseSensitive())
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 comparison = tag + ".value " + op + " " + parameter;
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 else
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 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
115 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 break;
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 case ConstraintType_List:
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 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
123 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 if (!comparison.empty())
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 comparison += ", ";
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 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
130
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 if (constraint.IsCaseSensitive())
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 comparison += parameter;
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 else
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 comparison += "lower(" + parameter + ")";
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 }
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 if (constraint.IsCaseSensitive())
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 comparison = tag + ".value IN (" + comparison + ")";
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 else
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 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
148 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 break;
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 case ConstraintType_Wildcard:
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 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
156
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 if (value == "*")
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 (!constraint.IsMandatory())
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 // 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
162 return false;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 else
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 std::string escaped;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 escaped.reserve(value.size());
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 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
171 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 if (value[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 escaped += "%";
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 else if (value[i] == '?')
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 escaped += "_";
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 else if (value[i] == '%')
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 escaped += "\\%";
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 else if (value[i] == '_')
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 escaped += "\\_";
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 else if (value[i] == '\\')
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 escaped += "\\\\";
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 }
4817
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
192 else if (escapeBrackets && value[i] == '[')
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
193 {
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
194 escaped += "\\[";
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 else if (escapeBrackets && value[i] == ']')
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 escaped += "\\]";
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
199 }
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 else
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 escaped += value[i];
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 }
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 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
207
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 if (constraint.IsCaseSensitive())
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 {
3079
65e2bfa953ef ISqlLookupFormatter::FormatWildcardEscape()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3078
diff changeset
210 comparison = (tag + ".value LIKE " + parameter + " " +
65e2bfa953ef ISqlLookupFormatter::FormatWildcardEscape()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3078
diff changeset
211 formatter.FormatWildcardEscape());
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 else
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 {
3079
65e2bfa953ef ISqlLookupFormatter::FormatWildcardEscape()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3078
diff changeset
215 comparison = ("lower(" + tag + ".value) LIKE lower(" +
65e2bfa953ef ISqlLookupFormatter::FormatWildcardEscape()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3078
diff changeset
216 parameter + ") " + formatter.FormatWildcardEscape());
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 break;
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 default:
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 return false;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 if (constraint.IsMandatory())
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 target = comparison;
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 else if (comparison.empty())
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 target = tag + ".value IS NULL";
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 else
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 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
238 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 return true;
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
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 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
245 const DatabaseConstraint& constraint,
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 size_t index)
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 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
249
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 if (constraint.IsMandatory())
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 target = " INNER JOIN ";
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 else
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 target = " LEFT JOIN ";
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 if (constraint.IsIdentifier())
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 target += "DicomIdentifiers ";
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 else
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 target += "MainDicomTags ";
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 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
269 ".internalId AND " + tag + ".tagGroup = " +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 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
271 " AND " + tag + ".tagElement = " +
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().GetElement()));
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273 }
5227
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
274
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
275
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
276 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
277 const std::string& prefix,
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
278 const std::string& separator)
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
279 {
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
280 if (values.empty())
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 return "";
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 else
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 std::string s = prefix;
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 bool first = true;
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
289 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
290 {
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
291 if (first)
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 first = false;
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 else
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 s += separator;
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
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
300 s += *it;
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
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
303 return s;
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 }
5422
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
306
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
307 static bool FormatComparison2(std::string& target,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
308 ISqlLookupFormatter& formatter,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
309 const DatabaseConstraint& constraint,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
310 bool escapeBrackets)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
311 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
312 std::string comparison;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
313 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
314 + " 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
315
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
316 switch (constraint.GetConstraintType())
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 case ConstraintType_Equal:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
319 case ConstraintType_SmallerOrEqual:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
320 case ConstraintType_GreaterOrEqual:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
321 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
322 std::string op;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
323 switch (constraint.GetConstraintType())
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
324 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
325 case ConstraintType_Equal:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
326 op = "=";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
327 break;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
328
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
329 case ConstraintType_SmallerOrEqual:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
330 op = "<=";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
331 break;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
332
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
333 case ConstraintType_GreaterOrEqual:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
334 op = ">=";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
335 break;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
336
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
337 default:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
338 throw OrthancException(ErrorCode_InternalError);
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
339 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
340
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
341 std::string parameter = formatter.GenerateParameter(constraint.GetSingleValue());
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 if (constraint.IsCaseSensitive())
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 comparison = " AND value " + op + " " + parameter;
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 else
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 comparison = " AND lower(value) " + op + " lower(" + parameter + ")";
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
352 break;
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
355 case ConstraintType_List:
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 std::vector<std::string> comparisonValues;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
358 for (size_t i = 0; i < constraint.GetValuesCount(); i++)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
359 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
360 std::string parameter = formatter.GenerateParameter(constraint.GetValue(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 if (constraint.IsCaseSensitive())
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 comparisonValues.push_back(parameter);
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 else
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 comparisonValues.push_back("lower(" + parameter + ")");
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 }
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 std::string values;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
373 Toolbox::JoinStrings(values, comparisonValues, ", ");
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
374
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
375 if (constraint.IsCaseSensitive())
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 comparison = " AND value IN (" + values + ")";
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 else
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 comparison = " AND lower(value) IN (" + values + ")";
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
384 break;
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
387 case ConstraintType_Wildcard:
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 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
390
5422
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
391 if (value == "*")
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
392 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
393 if (!constraint.IsMandatory())
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 // Universal constraint on an optional tag, ignore it
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
396 return false;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
397 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
398 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
399 else
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 std::string escaped;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
402 escaped.reserve(value.size());
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
403
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
404 for (size_t i = 0; i < value.size(); i++)
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 if (value[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 escaped += "%";
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 else if (value[i] == '?')
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 escaped += "_";
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 else if (value[i] == '%')
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 escaped += "\\%";
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 else if (value[i] == '_')
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 escaped += "\\_";
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 else if (value[i] == '\\')
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 escaped += "\\\\";
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 else if (escapeBrackets && value[i] == '[')
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 escaped += "\\[";
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 else if (escapeBrackets && value[i] == ']')
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 escaped += "\\]";
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 else
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 escaped += value[i];
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 }
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 std::string parameter = formatter.GenerateParameter(escaped);
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 if (constraint.IsCaseSensitive())
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 comparison = " AND value LIKE " + parameter + " " + formatter.FormatWildcardEscape();
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 else
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 comparison = " AND lower(value) LIKE lower(" + parameter + ") " + formatter.FormatWildcardEscape();
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 }
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 break;
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
455 default:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
456 return false;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
457 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
458
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
459 if (constraint.IsMandatory())
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 target = tagFilter + comparison;
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 else if (comparison.empty())
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 target = tagFilter + " AND value IS NULL";
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 else
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 target = tagFilter + " AND value IS NULL OR " + comparison;
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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
472 return true;
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
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 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
477 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
478 assert(ResourceType_Patient < ResourceType_Study &&
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
479 ResourceType_Study < ResourceType_Series &&
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
480 ResourceType_Series < ResourceType_Instance);
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
481
5422
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
482 lowerLevel = queryLevel;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
483 upperLevel = queryLevel;
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
484
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
485 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
486 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
487 ResourceType level = lookup[i].GetLevel();
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 if (level < upperLevel)
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 upperLevel = level;
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
494 if (level > lowerLevel)
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 lowerLevel = level;
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 }
5422
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
499 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
500
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
501
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
502 void ISqlLookupFormatter::Apply(std::string& sql,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
503 ISqlLookupFormatter& formatter,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
504 const std::vector<DatabaseConstraint>& lookup,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
505 ResourceType queryLevel,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
506 const std::set<std::string>& labels,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
507 LabelsConstraint labelsConstraint,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
508 size_t limit)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
509 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
510 ResourceType lowerLevel, upperLevel;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
511 GetLookupLevels(lowerLevel, upperLevel, queryLevel, lookup);
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
512
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
513 assert(upperLevel <= queryLevel &&
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
514 queryLevel <= lowerLevel);
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
515
4817
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
516 const bool escapeBrackets = formatter.IsEscapeBrackets();
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
517
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
518 std::string joins, comparisons;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
519
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
520 size_t count = 0;
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 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
523 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
524 std::string comparison;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
525
4817
b8fcd331b4b3 added ISqlLookupFormatter::IsEscapeBrackets()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
526 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
527 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
528 std::string join;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
529 FormatJoin(join, lookup[i], count);
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
530 joins += join;
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
531
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
532 if (!comparison.empty())
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 comparisons += " AND " + comparison;
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
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
537 count ++;
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 }
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 sql = ("SELECT " +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
542 FormatLevel(queryLevel) + ".publicId, " +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
543 FormatLevel(queryLevel) + ".internalId" +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
544 " FROM Resources AS " + FormatLevel(queryLevel));
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
545
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
546 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
547 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
548 sql += (" INNER JOIN Resources " +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
549 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
550 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
551 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
552 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
553
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
554 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
555 {
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
556 sql += (" INNER JOIN Resources " +
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
557 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
558 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
559 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
560 }
5227
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
561
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
562 std::list<std::string> where;
5256
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5248
diff changeset
563 where.push_back(FormatLevel(queryLevel) + ".resourceType = " +
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5248
diff changeset
564 formatter.FormatResourceType(queryLevel) + comparisons);
5227
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
565
5248
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
566 if (!labels.empty())
5227
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
567 {
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
568 /**
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
569 * "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
570 * 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
571 * question are NOT NULL."
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
572 * 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
573 **/
5248
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
574
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
575 std::list<std::string> formattedLabels;
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
576 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
577 {
5248
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
578 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
579 }
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
580
5248
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
581 std::string condition;
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
582 switch (labelsConstraint)
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
583 {
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
584 case LabelsConstraint_Any:
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
585 condition = "> 0";
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
586 break;
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
587
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
588 case LabelsConstraint_All:
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
589 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
590 break;
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
591
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
592 case LabelsConstraint_None:
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
593 condition = "= 0";
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
594 break;
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
595
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
596 default:
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
597 throw OrthancException(ErrorCode_ParameterOutOfRange);
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
598 }
a7d95f951f8a replaced "WithLabels" and "WithoutLabels", by "Labels" and "LabelsConstraint"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5227
diff changeset
599
5256
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5248
diff changeset
600 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
601 ".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
602 }
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
603
988dab8deb1c "/tools/find" accepts the "WithLabels" and "WithoutLabels" arguments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
604 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
605
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
606 if (limit != 0)
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 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
609 }
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
610 }
5422
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
611
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
612
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
613 void ISqlLookupFormatter::ApplySingleLevel(std::string& sql,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
614 ISqlLookupFormatter& formatter,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
615 const std::vector<DatabaseConstraint>& lookup,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
616 ResourceType queryLevel,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
617 const std::set<std::string>& labels,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
618 LabelsConstraint labelsConstraint,
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
619 size_t limit
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
620 )
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
621 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
622 ResourceType lowerLevel, upperLevel;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
623 GetLookupLevels(lowerLevel, upperLevel, queryLevel, lookup);
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
624
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
625 assert(upperLevel == queryLevel &&
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
626 queryLevel == lowerLevel);
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
627
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
628 const bool escapeBrackets = formatter.IsEscapeBrackets();
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 std::vector<std::string> mainDicomTagsComparisons, dicomIdentifiersComparisons;
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 for (size_t i = 0; i < lookup.size(); i++)
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 std::string comparison;
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 if (FormatComparison2(comparison, formatter, lookup[i], escapeBrackets))
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 (!comparison.empty())
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 (lookup[i].IsIdentifier())
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 dicomIdentifiersComparisons.push_back(comparison);
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 else
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 mainDicomTagsComparisons.push_back(comparison);
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 }
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 sql = ("SELECT publicId, internalId "
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
653 "FROM Resources "
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
654 "WHERE resourceType = " + formatter.FormatResourceType(queryLevel)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
655 + " ");
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
656
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
657 if (dicomIdentifiersComparisons.size() > 0)
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 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
660 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
661 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
662 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
663 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
664
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
665 if (mainDicomTagsComparisons.size() > 0)
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 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
668 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
669 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
670 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
671 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
672
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
673 if (!labels.empty())
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 /**
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
676 * "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
677 * 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
678 * question are NOT NULL."
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
679 * 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
680 **/
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
681
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
682 std::list<std::string> formattedLabels;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
683 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
684 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
685 formattedLabels.push_back(formatter.GenerateParameter(*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
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
688 std::string condition;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
689 std::string inOrNotIn;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
690 switch (labelsConstraint)
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
691 {
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
692 case LabelsConstraint_Any:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
693 condition = "> 0";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
694 inOrNotIn = "IN";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
695 break;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
696
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
697 case LabelsConstraint_All:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
698 condition = "= " + boost::lexical_cast<std::string>(labels.size());
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
699 inOrNotIn = "IN";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
700 break;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
701
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
702 case LabelsConstraint_None:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
703 condition = "> 0";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
704 inOrNotIn = "NOT IN";
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
705 break;
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
706
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
707 default:
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
708 throw OrthancException(ErrorCode_ParameterOutOfRange);
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
709 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
710
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
711 sql += (" AND internalId " + inOrNotIn + " (SELECT id"
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
712 " FROM (SELECT id, COUNT(1) AS labelsCount "
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
713 "FROM Labels "
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
714 "WHERE label IN (" + Join(formattedLabels, "", ", ") + ") GROUP BY id"
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
715 ") AS temp "
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
716 " WHERE labelsCount " + condition + ")");
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
717 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
718
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
719 if (limit != 0)
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 sql += " LIMIT " + boost::lexical_cast<std::string>(limit);
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 }
7f37233359aa sync code from DB plugins
Alain Mazy <am@osimis.io>
parents: 5256
diff changeset
724
3058
6faf575ba9cc refactoring: class ISqlLookupFormatter to be used in orthanc-databases
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
725 }