annotate Framework/Common/IResult.cpp @ 353:389c037387ea

Escaping '[' & ']' (change necessary only for MSSQL but this does not impact PG and MySQL plugins that were already matching angle brackets correctly)
author Alain Mazy <am@osimis.io>
date Mon, 15 Nov 2021 12:22:44 +0100
parents 8f655f308de9
children 16aac0287485
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
298
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 #include "IResult.h"
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "Utf8StringValue.h"
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include <Compatibility.h>
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include <OrthancException.h>
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 #include <cassert>
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 #include <list>
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 #include <vector>
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 #include <ostream>
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 namespace OrthancDatabases
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 {
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 static void PrintSeparators(std::ostream& stream,
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 char c,
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 size_t count)
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 {
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 for (size_t i = 0; i < count; i++)
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 {
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 stream << c;
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 }
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 }
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 static void PrintHeader(std::ostream& stream,
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 const std::vector<size_t>& maxWidth)
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 {
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 for (size_t i = 0; i < maxWidth.size(); i++)
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 {
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 stream << '+';
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 PrintSeparators(stream, '-', maxWidth[i] + 2);
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 }
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 stream << '+' << std::endl;
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 }
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 void IResult::Print(std::ostream& stream,
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 IResult& result)
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 {
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 typedef std::list< std::vector<std::string> > Table;
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 Table table;
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 const size_t columns = result.GetFieldsCount();
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 std::vector<size_t> maxWidth(columns);
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 while (!result.IsDone())
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 {
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 table.push_back(std::vector<std::string>(columns));
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 for (size_t i = 0; i < columns; i++)
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 {
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 std::string value;
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 try
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 {
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 std::unique_ptr<IValue> converted(
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 result.GetField(i).Convert(ValueType_Utf8String));
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 value = dynamic_cast<Utf8StringValue&>(*converted).GetContent();
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 }
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 catch (Orthanc::OrthancException&)
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 {
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 value = "?";
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 }
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 if (value.size() > maxWidth[i])
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 {
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 maxWidth[i] = value.size();
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 }
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 table.back() [i] = value;
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 }
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 result.Next();
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 }
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 PrintHeader(stream, maxWidth);
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 for (Table::const_iterator it = table.begin(); it != table.end(); ++it)
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 {
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 assert(it->size() == maxWidth.size());
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 for (size_t i = 0; i < it->size(); i++)
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 {
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 const std::string& value = (*it) [i];
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 stream << "| " << value << ' ';
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 for (size_t j = value.size(); j < maxWidth[i]; j++)
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 {
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 stream << ' ';
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 }
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 }
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 stream << '|' << std::endl;
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 }
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 PrintHeader(stream, maxWidth);
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 }
8f655f308de9 added IResult::Print()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 }