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