Mercurial > hg > orthanc-databases
annotate Framework/Common/GenericFormatter.cpp @ 395:a7a029043670 db-protobuf
integration mainline->db-protobuf
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 05 Apr 2023 14:53:57 +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 "GenericFormatter.h" | |
24 | |
152 | 25 #include <OrthancException.h> |
0 | 26 |
27 #include <boost/lexical_cast.hpp> | |
28 | |
29 namespace OrthancDatabases | |
30 { | |
304
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
31 Dialect GenericFormatter::GetDialect() const |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
32 { |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
33 if (autoincrementDialect_ != namedDialect_) |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
34 { |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
35 // 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
|
36 // SetAutoincrementDialect() or SetNamedDialect() |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
37 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
38 } |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
39 else |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
40 { |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
41 return namedDialect_; |
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 |
dd4b0edd1661
GenericFormatter::GetDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
303
diff
changeset
|
45 |
0 | 46 void GenericFormatter::Format(std::string& target, |
47 const std::string& source, | |
48 ValueType type) | |
49 { | |
50 if (source.empty()) | |
51 { | |
302
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
52 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
53 } |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
54 else if (source == "AUTOINCREMENT") |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
55 { |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
56 if (GetParametersCount() != 0) |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
57 { |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
58 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls, |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
59 "The AUTOINCREMENT argument must always be the first"); |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
60 } |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
61 |
303
495b6f325e35
GenericFormatter::SetNamedDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
302
diff
changeset
|
62 switch (autoincrementDialect_) |
0 | 63 { |
64 case Dialect_PostgreSQL: | |
302
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
65 target = "DEFAULT, "; |
0 | 66 break; |
67 | |
68 case Dialect_MySQL: | |
69 case Dialect_SQLite: | |
302
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
70 target = "NULL, "; |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
71 break; |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
72 |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
73 case Dialect_MSSQL: |
3a52e27a2d80
in GenericFormatter, replaced ${} by ${AUTOINCREMENT}
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
299
diff
changeset
|
74 target.clear(); // The IDENTITY field must not be filled in MSSQL |
0 | 75 break; |
76 | |
77 default: | |
78 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
79 } | |
80 } | |
81 else | |
82 { | |
303
495b6f325e35
GenericFormatter::SetNamedDialect()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
302
diff
changeset
|
83 switch (namedDialect_) |
0 | 84 { |
85 case Dialect_PostgreSQL: | |
86 target = "$" + boost::lexical_cast<std::string>(parametersName_.size() + 1); | |
87 break; | |
88 | |
89 case Dialect_MySQL: | |
90 case Dialect_SQLite: | |
297 | 91 case Dialect_MSSQL: |
0 | 92 target = "?"; |
93 break; | |
94 | |
95 default: | |
96 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
97 } | |
98 | |
99 parametersName_.push_back(source); | |
100 parametersType_.push_back(type); | |
101 } | |
102 } | |
103 | |
104 | |
105 const std::string& GenericFormatter::GetParameterName(size_t index) const | |
106 { | |
107 if (index >= parametersName_.size()) | |
108 { | |
109 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
110 } | |
111 else | |
112 { | |
113 return parametersName_[index]; | |
114 } | |
115 } | |
116 | |
117 | |
118 ValueType GenericFormatter::GetParameterType(size_t index) const | |
119 { | |
120 if (index >= parametersType_.size()) | |
121 { | |
122 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
123 } | |
124 else | |
125 { | |
126 return parametersType_[index]; | |
127 } | |
128 } | |
129 } |