Mercurial > hg > orthanc-databases
annotate Framework/Common/Query.cpp @ 397:c4f0f8087564 db-protobuf
sync
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 06 Apr 2023 19:07:19 +0200 |
parents | 3d6886f3e5b3 |
children | ecd0b719cff5 |
rev | line source |
---|---|
0 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
389
3d6886f3e5b3
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
359
diff
changeset
|
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium |
3d6886f3e5b3
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
359
diff
changeset
|
6 * Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
0 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
9 * modify it under the terms of the GNU Affero General Public License | |
10 * as published by the Free Software Foundation, either version 3 of | |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * Affero General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU Affero General Public License | |
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 **/ | |
21 | |
22 | |
23 #include "Query.h" | |
24 | |
152 | 25 #include <Logging.h> |
26 #include <OrthancException.h> | |
0 | 27 |
28 #include <boost/regex.hpp> | |
29 | |
30 namespace OrthancDatabases | |
31 { | |
32 class Query::Token : public boost::noncopyable | |
33 { | |
34 private: | |
35 bool isParameter_; | |
36 std::string content_; | |
37 | |
38 public: | |
39 Token(bool isParameter, | |
40 const std::string& content) : | |
41 isParameter_(isParameter), | |
42 content_(content) | |
43 { | |
44 } | |
45 | |
46 bool IsParameter() const | |
47 { | |
48 return isParameter_; | |
49 } | |
50 | |
51 const std::string& GetContent() const | |
52 { | |
53 return content_; | |
54 } | |
55 }; | |
56 | |
57 | |
58 void Query::Setup(const std::string& sql) | |
59 { | |
60 boost::regex regex("\\$\\{(.*?)\\}"); | |
61 | |
62 std::string::const_iterator last = sql.begin(); | |
63 boost::sregex_token_iterator it(sql.begin(), sql.end(), regex, 0); | |
64 boost::sregex_token_iterator end; | |
65 | |
66 while (it != end) | |
67 { | |
68 if (last != it->first) | |
69 { | |
70 tokens_.push_back(new Token(false, std::string(last, it->first))); | |
71 } | |
72 | |
73 std::string parameter = *it; | |
74 assert(parameter.size() >= 3); | |
75 parameter = parameter.substr(2, parameter.size() - 3); | |
76 | |
77 tokens_.push_back(new Token(true, parameter)); | |
78 parameters_[parameter] = ValueType_Null; | |
79 | |
80 last = it->second; | |
81 | |
82 ++it; | |
83 } | |
84 | |
85 if (last != sql.end()) | |
86 { | |
87 tokens_.push_back(new Token(false, std::string(last, sql.end()))); | |
88 } | |
89 } | |
90 | |
91 | |
92 Query::Query(const std::string& sql) : | |
93 readOnly_(false) | |
94 { | |
95 Setup(sql); | |
96 } | |
97 | |
98 | |
99 Query::Query(const std::string& sql, | |
100 bool readOnly) : | |
101 readOnly_(readOnly) | |
102 { | |
103 Setup(sql); | |
104 } | |
105 | |
106 | |
107 Query::~Query() | |
108 { | |
109 for (size_t i = 0; i < tokens_.size(); i++) | |
110 { | |
111 assert(tokens_[i] != NULL); | |
112 delete tokens_[i]; | |
113 } | |
114 } | |
115 | |
116 | |
117 bool Query::HasParameter(const std::string& parameter) const | |
118 { | |
119 return parameters_.find(parameter) != parameters_.end(); | |
120 } | |
121 | |
122 | |
123 ValueType Query::GetType(const std::string& parameter) const | |
124 { | |
125 Parameters::const_iterator found = parameters_.find(parameter); | |
126 | |
127 if (found == parameters_.end()) | |
128 { | |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
129 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem, |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
130 "Inexistent parameter in a SQL query: " + parameter); |
0 | 131 } |
132 else | |
133 { | |
134 return found->second; | |
135 } | |
136 } | |
137 | |
138 | |
139 void Query::SetType(const std::string& parameter, | |
140 ValueType type) | |
141 { | |
142 Parameters::iterator found = parameters_.find(parameter); | |
143 | |
144 if (found == parameters_.end()) | |
145 { | |
70
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
146 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem, |
e6c13ddd26d9
all integration tests passing with LookupResources extension
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
147 "Inexistent parameter in a SQL query: " + parameter); |
0 | 148 } |
149 else | |
150 { | |
151 found->second = type; | |
152 } | |
153 } | |
154 | |
155 | |
156 void Query::Format(std::string& result, | |
157 IParameterFormatter& formatter) const | |
158 { | |
159 result.clear(); | |
160 | |
161 for (size_t i = 0; i < tokens_.size(); i++) | |
162 { | |
163 assert(tokens_[i] != NULL); | |
164 | |
165 const std::string& content = tokens_[i]->GetContent(); | |
166 | |
167 if (tokens_[i]->IsParameter()) | |
168 { | |
169 std::string parameter; | |
170 formatter.Format(parameter, content, GetType(content)); | |
171 result += parameter; | |
172 } | |
173 else | |
174 { | |
175 result += content; | |
176 } | |
177 } | |
178 } | |
179 } |