Mercurial > hg > orthanc-databases
annotate Framework/Common/GenericFormatter.cpp @ 322:7e9622c64c40
compatibility with Orthanc framework < 1.9.2
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 20 Jul 2021 15:52:15 +0200 |
parents | dd4b0edd1661 |
children | 16aac0287485 |
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 | |
193
3236894320d6
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
0 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "GenericFormatter.h" | |
23 | |
152 | 24 #include <OrthancException.h> |
0 | 25 |
26 #include <boost/lexical_cast.hpp> | |
27 | |
28 namespace OrthancDatabases | |
29 { | |
304
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
30 Dialect GenericFormatter::GetDialect() const |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
31 { |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
32 if (autoincrementDialect_ != namedDialect_) |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
33 { |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
34 // The two dialects do not match because of a previous call to |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
35 // SetAutoincrementDialect() or SetNamedDialect() |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
36 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
37 } |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
38 else |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
39 { |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
40 return namedDialect_; |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
41 } |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
42 } |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
43 |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
44 |
0 | 45 void GenericFormatter::Format(std::string& target, |
46 const std::string& source, | |
47 ValueType type) | |
48 { | |
49 if (source.empty()) | |
50 { | |
302
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
51 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
52 } |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
53 else if (source == "AUTOINCREMENT") |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
54 { |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
55 if (GetParametersCount() != 0) |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
56 { |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
57 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls, |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
58 "The AUTOINCREMENT argument must always be the first"); |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
59 } |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
60 |
303
495b6f325e35
GenericFormatter::SetNamedDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
302
diff
changeset
|
61 switch (autoincrementDialect_) |
0 | 62 { |
63 case Dialect_PostgreSQL: | |
302
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
64 target = "DEFAULT, "; |
0 | 65 break; |
66 | |
67 case Dialect_MySQL: | |
68 case Dialect_SQLite: | |
302
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
69 target = "NULL, "; |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
70 break; |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
71 |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
72 case Dialect_MSSQL: |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
73 target.clear(); // The IDENTITY field must not be filled in MSSQL |
0 | 74 break; |
75 | |
76 default: | |
77 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
78 } | |
79 } | |
80 else | |
81 { | |
303
495b6f325e35
GenericFormatter::SetNamedDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
302
diff
changeset
|
82 switch (namedDialect_) |
0 | 83 { |
84 case Dialect_PostgreSQL: | |
85 target = "$" + boost::lexical_cast<std::string>(parametersName_.size() + 1); | |
86 break; | |
87 | |
88 case Dialect_MySQL: | |
89 case Dialect_SQLite: | |
297 | 90 case Dialect_MSSQL: |
0 | 91 target = "?"; |
92 break; | |
93 | |
94 default: | |
95 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
96 } | |
97 | |
98 parametersName_.push_back(source); | |
99 parametersType_.push_back(type); | |
100 } | |
101 } | |
102 | |
103 | |
104 const std::string& GenericFormatter::GetParameterName(size_t index) const | |
105 { | |
106 if (index >= parametersName_.size()) | |
107 { | |
108 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
109 } | |
110 else | |
111 { | |
112 return parametersName_[index]; | |
113 } | |
114 } | |
115 | |
116 | |
117 ValueType GenericFormatter::GetParameterType(size_t index) const | |
118 { | |
119 if (index >= parametersType_.size()) | |
120 { | |
121 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
122 } | |
123 else | |
124 { | |
125 return parametersType_[index]; | |
126 } | |
127 } | |
128 } |